annotate gcc/ada/libgnat/a-chtgop.ads @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT LIBRARY COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- ADA.CONTAINERS.HASH_TABLES.GENERIC_OPERATIONS --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2004-2017, Free Software Foundation, Inc. --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- This unit was originally developed by Matthew J Heaney. --
kono
parents:
diff changeset
28 ------------------------------------------------------------------------------
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 -- Hash_Table_Type is used to implement hashed containers. This package
kono
parents:
diff changeset
31 -- declares hash-table operations that do not depend on keys.
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 with Ada.Streams;
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 generic
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 with package HT_Types is
kono
parents:
diff changeset
38 new Generic_Hash_Table_Types (<>);
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 use HT_Types, HT_Types.Implementation;
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 with function Hash_Node (Node : Node_Access) return Hash_Type;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 with function Next (Node : Node_Access) return Node_Access;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 with procedure Set_Next
kono
parents:
diff changeset
47 (Node : Node_Access;
kono
parents:
diff changeset
48 Next : Node_Access);
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 with function Copy_Node (Source : Node_Access) return Node_Access;
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 with procedure Free (X : in out Node_Access);
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 package Ada.Containers.Hash_Tables.Generic_Operations is
kono
parents:
diff changeset
55 pragma Preelaborate;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 procedure Free_Hash_Table (Buckets : in out Buckets_Access);
kono
parents:
diff changeset
58 -- First frees the nodes in all non-null buckets of Buckets, and then frees
kono
parents:
diff changeset
59 -- the Buckets array itself.
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 function Index
kono
parents:
diff changeset
62 (Buckets : Buckets_Type;
kono
parents:
diff changeset
63 Node : Node_Access) return Hash_Type;
kono
parents:
diff changeset
64 pragma Inline (Index);
kono
parents:
diff changeset
65 -- Uses the hash value of Node to compute its Buckets array index
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 function Index
kono
parents:
diff changeset
68 (Hash_Table : Hash_Table_Type;
kono
parents:
diff changeset
69 Node : Node_Access) return Hash_Type;
kono
parents:
diff changeset
70 pragma Inline (Index);
kono
parents:
diff changeset
71 -- Uses the hash value of Node to compute its Hash_Table buckets array
kono
parents:
diff changeset
72 -- index.
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 function Checked_Index
kono
parents:
diff changeset
75 (Hash_Table : aliased in out Hash_Table_Type;
kono
parents:
diff changeset
76 Buckets : Buckets_Type;
kono
parents:
diff changeset
77 Node : Node_Access) return Hash_Type;
kono
parents:
diff changeset
78 -- Calls Index, but also locks and unlocks the container, per AI05-0022, in
kono
parents:
diff changeset
79 -- order to detect element tampering by the generic actual Hash function.
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 function Checked_Index
kono
parents:
diff changeset
82 (Hash_Table : aliased in out Hash_Table_Type;
kono
parents:
diff changeset
83 Node : Node_Access) return Hash_Type;
kono
parents:
diff changeset
84 -- Calls Checked_Index using Hash_Table's buckets array.
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 procedure Adjust (HT : in out Hash_Table_Type);
kono
parents:
diff changeset
87 -- Used to implement controlled Adjust. It is assumed that HT has the value
kono
parents:
diff changeset
88 -- of the bit-wise copy that immediately follows controlled Finalize.
kono
parents:
diff changeset
89 -- Adjust first allocates a new buckets array for HT (having the same
kono
parents:
diff changeset
90 -- length as the source), and then allocates a copy of each node of source.
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 procedure Finalize (HT : in out Hash_Table_Type);
kono
parents:
diff changeset
93 -- Used to implement controlled Finalize. It first calls Clear to
kono
parents:
diff changeset
94 -- deallocate any remaining nodes, and then deallocates the buckets array.
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 generic
kono
parents:
diff changeset
97 with function Find
kono
parents:
diff changeset
98 (HT : Hash_Table_Type;
kono
parents:
diff changeset
99 Key : Node_Access) return Boolean;
kono
parents:
diff changeset
100 function Generic_Equal
kono
parents:
diff changeset
101 (L, R : Hash_Table_Type) return Boolean;
kono
parents:
diff changeset
102 -- Used to implement hashed container equality. For each node in hash table
kono
parents:
diff changeset
103 -- L, it calls Find to search for an equivalent item in hash table R. If
kono
parents:
diff changeset
104 -- Find returns False for any node then Generic_Equal terminates
kono
parents:
diff changeset
105 -- immediately and returns False. Otherwise if Find returns True for every
kono
parents:
diff changeset
106 -- node then Generic_Equal returns True.
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 procedure Clear (HT : in out Hash_Table_Type);
kono
parents:
diff changeset
109 -- Deallocates each node in hash table HT. (Note that it only deallocates
kono
parents:
diff changeset
110 -- the nodes, not the buckets array.) Program_Error is raised if the hash
kono
parents:
diff changeset
111 -- table is busy.
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 procedure Move (Target, Source : in out Hash_Table_Type);
kono
parents:
diff changeset
114 -- Moves (not copies) the buckets array and nodes from Source to
kono
parents:
diff changeset
115 -- Target. Program_Error is raised if Source is busy. The Target is first
kono
parents:
diff changeset
116 -- cleared to deallocate its nodes (implying that Program_Error is also
kono
parents:
diff changeset
117 -- raised if Target is busy). Source is empty following the move.
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 function Capacity (HT : Hash_Table_Type) return Count_Type;
kono
parents:
diff changeset
120 -- Returns the length of the buckets array
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 procedure Reserve_Capacity
kono
parents:
diff changeset
123 (HT : in out Hash_Table_Type;
kono
parents:
diff changeset
124 N : Count_Type);
kono
parents:
diff changeset
125 -- If N is greater than the current capacity, then it expands the buckets
kono
parents:
diff changeset
126 -- array to at least the value N. If N is less than the current capacity,
kono
parents:
diff changeset
127 -- then it contracts the buckets array. In either case existing nodes are
kono
parents:
diff changeset
128 -- rehashed onto the new buckets array, and the old buckets array is
kono
parents:
diff changeset
129 -- deallocated. Program_Error is raised if the hash table is busy.
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 procedure Delete_Node_At_Index
kono
parents:
diff changeset
132 (HT : in out Hash_Table_Type;
kono
parents:
diff changeset
133 Indx : Hash_Type;
kono
parents:
diff changeset
134 X : in out Node_Access);
kono
parents:
diff changeset
135 -- Delete a node whose bucket position is known. Used to remove a node
kono
parents:
diff changeset
136 -- whose element has been modified through a key_preserving reference.
kono
parents:
diff changeset
137 -- We cannot use the value of the element precisely because the current
kono
parents:
diff changeset
138 -- value does not correspond to the hash code that determines the bucket.
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 procedure Delete_Node_Sans_Free
kono
parents:
diff changeset
141 (HT : in out Hash_Table_Type;
kono
parents:
diff changeset
142 X : Node_Access);
kono
parents:
diff changeset
143 -- Removes node X from the hash table without deallocating the node
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 function First
kono
parents:
diff changeset
146 (HT : Hash_Table_Type) return Node_Access;
kono
parents:
diff changeset
147 function First
kono
parents:
diff changeset
148 (HT : Hash_Table_Type;
kono
parents:
diff changeset
149 Position : out Hash_Type) return Node_Access;
kono
parents:
diff changeset
150 -- Returns the head of the list in the first (lowest-index) non-empty
kono
parents:
diff changeset
151 -- bucket. Position will be the index of the bucket of the first node.
kono
parents:
diff changeset
152 -- It is provided so that clients can implement efficient iterators.
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 function Next
kono
parents:
diff changeset
155 (HT : aliased in out Hash_Table_Type;
kono
parents:
diff changeset
156 Node : Node_Access) return Node_Access;
kono
parents:
diff changeset
157 function Next
kono
parents:
diff changeset
158 (HT : aliased in out Hash_Table_Type;
kono
parents:
diff changeset
159 Node : Node_Access;
kono
parents:
diff changeset
160 Position : in out Hash_Type) return Node_Access;
kono
parents:
diff changeset
161 -- Returns the node that immediately follows Node. This corresponds to
kono
parents:
diff changeset
162 -- either the next node in the same bucket, or (if Node is the last node in
kono
parents:
diff changeset
163 -- its bucket) the head of the list in the first non-empty bucket that
kono
parents:
diff changeset
164 -- follows.
kono
parents:
diff changeset
165 --
kono
parents:
diff changeset
166 -- If Node_Position is supplied, then it will be used as a starting point
kono
parents:
diff changeset
167 -- for iteration (Node_Position must be the index of Node's buckets). If it
kono
parents:
diff changeset
168 -- is not supplied, it will be recomputed. It is provided so that clients
kono
parents:
diff changeset
169 -- can implement efficient iterators.
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 generic
kono
parents:
diff changeset
172 with procedure Process (Node : Node_Access; Position : Hash_Type);
kono
parents:
diff changeset
173 procedure Generic_Iteration_With_Position (HT : Hash_Table_Type);
kono
parents:
diff changeset
174 -- Calls Process for each node in hash table HT
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 generic
kono
parents:
diff changeset
177 with procedure Process (Node : Node_Access);
kono
parents:
diff changeset
178 procedure Generic_Iteration (HT : Hash_Table_Type);
kono
parents:
diff changeset
179 -- Calls Process for each node in hash table HT
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 generic
kono
parents:
diff changeset
182 use Ada.Streams;
kono
parents:
diff changeset
183 with procedure Write
kono
parents:
diff changeset
184 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
185 Node : Node_Access);
kono
parents:
diff changeset
186 procedure Generic_Write
kono
parents:
diff changeset
187 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
188 HT : Hash_Table_Type);
kono
parents:
diff changeset
189 -- Used to implement the streaming attribute for hashed containers. It
kono
parents:
diff changeset
190 -- calls Write for each node to write its value into Stream.
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 generic
kono
parents:
diff changeset
193 use Ada.Streams;
kono
parents:
diff changeset
194 with function New_Node
kono
parents:
diff changeset
195 (Stream : not null access Root_Stream_Type'Class)
kono
parents:
diff changeset
196 return Node_Access;
kono
parents:
diff changeset
197 procedure Generic_Read
kono
parents:
diff changeset
198 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
199 HT : out Hash_Table_Type);
kono
parents:
diff changeset
200 -- Used to implement the streaming attribute for hashed containers. It
kono
parents:
diff changeset
201 -- first clears hash table HT, then populates the hash table by calling
kono
parents:
diff changeset
202 -- New_Node for each item in Stream.
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 function New_Buckets (Length : Hash_Type) return Buckets_Access;
kono
parents:
diff changeset
205 pragma Inline (New_Buckets);
kono
parents:
diff changeset
206 -- Allocate a new Buckets_Type array with bounds 0 .. Length - 1
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 procedure Free_Buckets (Buckets : in out Buckets_Access);
kono
parents:
diff changeset
209 pragma Inline (Free_Buckets);
kono
parents:
diff changeset
210 -- Unchecked_Deallocate Buckets
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 -- Note: New_Buckets and Free_Buckets are needed because Buckets_Access has
kono
parents:
diff changeset
213 -- an empty pool.
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 end Ada.Containers.Hash_Tables.Generic_Operations;