annotate gcc/ada/libgnat/a-cohase.ads @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
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 -- A D A . C O N T A I N E R S . H A S H E D _ S E T S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 -- Copyright (C) 2004-2019, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- This specification is derived from the Ada Reference Manual for use with --
kono
parents:
diff changeset
12 -- GNAT. The copyright notice above, and the license provisions that follow --
kono
parents:
diff changeset
13 -- apply solely to the contents of the part following the private keyword. --
kono
parents:
diff changeset
14 -- --
kono
parents:
diff changeset
15 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
16 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
23 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
24 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
25 -- --
kono
parents:
diff changeset
26 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
27 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
29 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
30 -- --
kono
parents:
diff changeset
31 -- This unit was originally developed by Matthew J Heaney. --
kono
parents:
diff changeset
32 ------------------------------------------------------------------------------
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 with Ada.Iterator_Interfaces;
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 private with Ada.Containers.Hash_Tables;
kono
parents:
diff changeset
37 with Ada.Containers.Helpers;
kono
parents:
diff changeset
38 private with Ada.Finalization;
kono
parents:
diff changeset
39 private with Ada.Streams;
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 generic
kono
parents:
diff changeset
42 type Element_Type is private;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 with function Hash (Element : Element_Type) return Hash_Type;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 with function Equivalent_Elements
kono
parents:
diff changeset
47 (Left, Right : Element_Type) return Boolean;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 with function "=" (Left, Right : Element_Type) return Boolean is <>;
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 package Ada.Containers.Hashed_Sets is
kono
parents:
diff changeset
52 pragma Annotate (CodePeer, Skip_Analysis);
kono
parents:
diff changeset
53 pragma Preelaborate;
kono
parents:
diff changeset
54 pragma Remote_Types;
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 type Set is tagged private
kono
parents:
diff changeset
57 with
kono
parents:
diff changeset
58 Constant_Indexing => Constant_Reference,
kono
parents:
diff changeset
59 Default_Iterator => Iterate,
kono
parents:
diff changeset
60 Iterator_Element => Element_Type;
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 pragma Preelaborable_Initialization (Set);
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 type Cursor is private;
kono
parents:
diff changeset
65 pragma Preelaborable_Initialization (Cursor);
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 Empty_Set : constant Set;
kono
parents:
diff changeset
68 -- Set objects declared without an initialization expression are
kono
parents:
diff changeset
69 -- initialized to the value Empty_Set.
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 No_Element : constant Cursor;
kono
parents:
diff changeset
72 -- Cursor objects declared without an initialization expression are
kono
parents:
diff changeset
73 -- initialized to the value No_Element.
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 function Has_Element (Position : Cursor) return Boolean;
kono
parents:
diff changeset
76 -- Equivalent to Position /= No_Element
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 package Set_Iterator_Interfaces is new
kono
parents:
diff changeset
79 Ada.Iterator_Interfaces (Cursor, Has_Element);
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 function "=" (Left, Right : Set) return Boolean;
kono
parents:
diff changeset
82 -- For each element in Left, set equality attempts to find the equal
kono
parents:
diff changeset
83 -- element in Right; if a search fails, then set equality immediately
kono
parents:
diff changeset
84 -- returns False. The search works by calling Hash to find the bucket in
kono
parents:
diff changeset
85 -- the Right set that corresponds to the Left element. If the bucket is
kono
parents:
diff changeset
86 -- non-empty, the search calls the generic formal element equality operator
kono
parents:
diff changeset
87 -- to compare the element (in Left) to the element of each node in the
kono
parents:
diff changeset
88 -- bucket (in Right); the search terminates when a matching node in the
kono
parents:
diff changeset
89 -- bucket is found, or the nodes in the bucket are exhausted. (Note that
kono
parents:
diff changeset
90 -- element equality is called here, not Equivalent_Elements. Set equality
kono
parents:
diff changeset
91 -- is the only operation in which element equality is used. Compare set
kono
parents:
diff changeset
92 -- equality to Equivalent_Sets, which does call Equivalent_Elements.)
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 function Equivalent_Sets (Left, Right : Set) return Boolean;
kono
parents:
diff changeset
95 -- Similar to set equality, with the difference that the element in Left is
kono
parents:
diff changeset
96 -- compared to the elements in Right using the generic formal
kono
parents:
diff changeset
97 -- Equivalent_Elements operation instead of element equality.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 function To_Set (New_Item : Element_Type) return Set;
kono
parents:
diff changeset
100 -- Constructs a singleton set comprising New_Element. To_Set calls Hash to
kono
parents:
diff changeset
101 -- determine the bucket for New_Item.
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 function Capacity (Container : Set) return Count_Type;
kono
parents:
diff changeset
104 -- Returns the current capacity of the set. Capacity is the maximum length
kono
parents:
diff changeset
105 -- before which rehashing in guaranteed not to occur.
kono
parents:
diff changeset
106
kono
parents:
diff changeset
107 procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
kono
parents:
diff changeset
108 -- Adjusts the current capacity, by allocating a new buckets array. If the
kono
parents:
diff changeset
109 -- requested capacity is less than the current capacity, then the capacity
kono
parents:
diff changeset
110 -- is contracted (to a value not less than the current length). If the
kono
parents:
diff changeset
111 -- requested capacity is greater than the current capacity, then the
kono
parents:
diff changeset
112 -- capacity is expanded (to a value not less than what is requested). In
kono
parents:
diff changeset
113 -- either case, the nodes are rehashed from the old buckets array onto the
kono
parents:
diff changeset
114 -- new buckets array (Hash is called once for each existing element in
kono
parents:
diff changeset
115 -- order to compute the new index), and then the old buckets array is
kono
parents:
diff changeset
116 -- deallocated.
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 function Length (Container : Set) return Count_Type;
kono
parents:
diff changeset
119 -- Returns the number of items in the set
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 function Is_Empty (Container : Set) return Boolean;
kono
parents:
diff changeset
122 -- Equivalent to Length (Container) = 0
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 procedure Clear (Container : in out Set);
kono
parents:
diff changeset
125 -- Removes all of the items from the set
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 function Element (Position : Cursor) return Element_Type;
kono
parents:
diff changeset
128 -- Returns the element of the node designated by the cursor
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 procedure Replace_Element
kono
parents:
diff changeset
131 (Container : in out Set;
kono
parents:
diff changeset
132 Position : Cursor;
kono
parents:
diff changeset
133 New_Item : Element_Type);
kono
parents:
diff changeset
134 -- If New_Item is equivalent (as determined by calling Equivalent_Elements)
kono
parents:
diff changeset
135 -- to the element of the node designated by Position, then New_Element is
kono
parents:
diff changeset
136 -- assigned to that element. Otherwise, it calls Hash to determine the
kono
parents:
diff changeset
137 -- bucket for New_Item. If the bucket is not empty, then it calls
kono
parents:
diff changeset
138 -- Equivalent_Elements for each node in that bucket to determine whether
kono
parents:
diff changeset
139 -- New_Item is equivalent to an element in that bucket. If
kono
parents:
diff changeset
140 -- Equivalent_Elements returns True then Program_Error is raised (because
kono
parents:
diff changeset
141 -- an element may appear only once in the set); otherwise, New_Item is
kono
parents:
diff changeset
142 -- assigned to the node designated by Position, and the node is moved to
kono
parents:
diff changeset
143 -- its new bucket.
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 procedure Query_Element
kono
parents:
diff changeset
146 (Position : Cursor;
kono
parents:
diff changeset
147 Process : not null access procedure (Element : Element_Type));
kono
parents:
diff changeset
148 -- Calls Process with the element (having only a constant view) of the node
kono
parents:
diff changeset
149 -- designed by the cursor.
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 type Constant_Reference_Type
kono
parents:
diff changeset
152 (Element : not null access constant Element_Type) is private
kono
parents:
diff changeset
153 with Implicit_Dereference => Element;
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 function Constant_Reference
kono
parents:
diff changeset
156 (Container : aliased Set;
kono
parents:
diff changeset
157 Position : Cursor) return Constant_Reference_Type;
kono
parents:
diff changeset
158 pragma Inline (Constant_Reference);
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 procedure Assign (Target : in out Set; Source : Set);
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 function Copy (Source : Set; Capacity : Count_Type := 0) return Set;
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 procedure Move (Target : in out Set; Source : in out Set);
kono
parents:
diff changeset
165 -- Clears Target (if it's not empty), and then moves (not copies) the
kono
parents:
diff changeset
166 -- buckets array and nodes from Source to Target.
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 procedure Insert
kono
parents:
diff changeset
169 (Container : in out Set;
kono
parents:
diff changeset
170 New_Item : Element_Type;
kono
parents:
diff changeset
171 Position : out Cursor;
kono
parents:
diff changeset
172 Inserted : out Boolean);
kono
parents:
diff changeset
173 -- Conditionally inserts New_Item into the set. If New_Item is already in
kono
parents:
diff changeset
174 -- the set, then Inserted returns False and Position designates the node
kono
parents:
diff changeset
175 -- containing the existing element (which is not modified). If New_Item is
kono
parents:
diff changeset
176 -- not already in the set, then Inserted returns True and Position
kono
parents:
diff changeset
177 -- designates the newly-inserted node containing New_Item. The search for
kono
parents:
diff changeset
178 -- an existing element works as follows. Hash is called to determine
kono
parents:
diff changeset
179 -- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
kono
parents:
diff changeset
180 -- is called to compare New_Item to the element of each node in that
kono
parents:
diff changeset
181 -- bucket. If the bucket is empty, or there were no equivalent elements in
kono
parents:
diff changeset
182 -- the bucket, the search "fails" and the New_Item is inserted in the set
kono
parents:
diff changeset
183 -- (and Inserted returns True); otherwise, the search "succeeds" (and
kono
parents:
diff changeset
184 -- Inserted returns False).
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 procedure Insert (Container : in out Set; New_Item : Element_Type);
kono
parents:
diff changeset
187 -- Attempts to insert New_Item into the set, performing the usual insertion
kono
parents:
diff changeset
188 -- search (which involves calling both Hash and Equivalent_Elements); if
kono
parents:
diff changeset
189 -- the search succeeds (New_Item is equivalent to an element already in the
kono
parents:
diff changeset
190 -- set, and so was not inserted), then this operation raises
kono
parents:
diff changeset
191 -- Constraint_Error. (This version of Insert is similar to Replace, but
kono
parents:
diff changeset
192 -- having the opposite exception behavior. It is intended for use when you
kono
parents:
diff changeset
193 -- want to assert that the item is not already in the set.)
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 procedure Include (Container : in out Set; New_Item : Element_Type);
kono
parents:
diff changeset
196 -- Attempts to insert New_Item into the set. If an element equivalent to
kono
parents:
diff changeset
197 -- New_Item is already in the set (the insertion search succeeded, and
kono
parents:
diff changeset
198 -- hence New_Item was not inserted), then the value of New_Item is assigned
kono
parents:
diff changeset
199 -- to the existing element. (This insertion operation only raises an
kono
parents:
diff changeset
200 -- exception if cursor tampering occurs. It is intended for use when you
kono
parents:
diff changeset
201 -- want to insert the item in the set, and you don't care whether an
kono
parents:
diff changeset
202 -- equivalent element is already present.)
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 procedure Replace (Container : in out Set; New_Item : Element_Type);
kono
parents:
diff changeset
205 -- Searches for New_Item in the set; if the search fails (because an
kono
parents:
diff changeset
206 -- equivalent element was not in the set), then it raises
kono
parents:
diff changeset
207 -- Constraint_Error. Otherwise, the existing element is assigned the value
kono
parents:
diff changeset
208 -- New_Item. (This is similar to Insert, but with the opposite exception
kono
parents:
diff changeset
209 -- behavior. It is intended for use when you want to assert that the item
kono
parents:
diff changeset
210 -- is already in the set.)
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 procedure Exclude (Container : in out Set; Item : Element_Type);
kono
parents:
diff changeset
213 -- Searches for Item in the set, and if found, removes its node from the
kono
parents:
diff changeset
214 -- set and then deallocates it. The search works as follows. The operation
kono
parents:
diff changeset
215 -- calls Hash to determine the item's bucket; if the bucket is not empty,
kono
parents:
diff changeset
216 -- it calls Equivalent_Elements to compare Item to the element of each node
kono
parents:
diff changeset
217 -- in the bucket. (This is the deletion analog of Include. It is intended
kono
parents:
diff changeset
218 -- for use when you want to remove the item from the set, but don't care
kono
parents:
diff changeset
219 -- whether the item is already in the set.)
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 procedure Delete (Container : in out Set; Item : Element_Type);
kono
parents:
diff changeset
222 -- Searches for Item in the set (which involves calling both Hash and
kono
parents:
diff changeset
223 -- Equivalent_Elements). If the search fails, then the operation raises
kono
parents:
diff changeset
224 -- Constraint_Error. Otherwise it removes the node from the set and then
kono
parents:
diff changeset
225 -- deallocates it. (This is the deletion analog of non-conditional
kono
parents:
diff changeset
226 -- Insert. It is intended for use when you want to assert that the item is
kono
parents:
diff changeset
227 -- already in the set.)
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 procedure Delete (Container : in out Set; Position : in out Cursor);
kono
parents:
diff changeset
230 -- Removes the node designated by Position from the set, and then
kono
parents:
diff changeset
231 -- deallocates the node. The operation calls Hash to determine the bucket,
kono
parents:
diff changeset
232 -- and then compares Position to each node in the bucket until there's a
kono
parents:
diff changeset
233 -- match (it does not call Equivalent_Elements).
kono
parents:
diff changeset
234
kono
parents:
diff changeset
235 procedure Union (Target : in out Set; Source : Set);
kono
parents:
diff changeset
236 -- The operation first calls Reserve_Capacity if the current capacity is
kono
parents:
diff changeset
237 -- less than the sum of the lengths of Source and Target. It then iterates
kono
parents:
diff changeset
238 -- over the Source set, and conditionally inserts each element into Target.
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 function Union (Left, Right : Set) return Set;
kono
parents:
diff changeset
241 -- The operation first copies the Left set to the result, and then iterates
kono
parents:
diff changeset
242 -- over the Right set to conditionally insert each element into the result.
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 function "or" (Left, Right : Set) return Set renames Union;
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 procedure Intersection (Target : in out Set; Source : Set);
kono
parents:
diff changeset
247 -- Iterates over the Target set (calling First and Next), calling Find to
kono
parents:
diff changeset
248 -- determine whether the element is in Source. If an equivalent element is
kono
parents:
diff changeset
249 -- not found in Source, the element is deleted from Target.
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 function Intersection (Left, Right : Set) return Set;
kono
parents:
diff changeset
252 -- Iterates over the Left set, calling Find to determine whether the
kono
parents:
diff changeset
253 -- element is in Right. If an equivalent element is found, it is inserted
kono
parents:
diff changeset
254 -- into the result set.
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 function "and" (Left, Right : Set) return Set renames Intersection;
kono
parents:
diff changeset
257
kono
parents:
diff changeset
258 procedure Difference (Target : in out Set; Source : Set);
kono
parents:
diff changeset
259 -- Iterates over the Source (calling First and Next), calling Find to
kono
parents:
diff changeset
260 -- determine whether the element is in Target. If an equivalent element is
kono
parents:
diff changeset
261 -- found, it is deleted from Target.
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 function Difference (Left, Right : Set) return Set;
kono
parents:
diff changeset
264 -- Iterates over the Left set, calling Find to determine whether the
kono
parents:
diff changeset
265 -- element is in the Right set. If an equivalent element is not found, the
kono
parents:
diff changeset
266 -- element is inserted into the result set.
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 function "-" (Left, Right : Set) return Set renames Difference;
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 procedure Symmetric_Difference (Target : in out Set; Source : Set);
kono
parents:
diff changeset
271 -- The operation first calls Reserve_Capacity if the current capacity is
kono
parents:
diff changeset
272 -- less than the sum of the lengths of Source and Target. It then iterates
kono
parents:
diff changeset
273 -- over the Source set, searching for the element in Target (calling Hash
kono
parents:
diff changeset
274 -- and Equivalent_Elements). If an equivalent element is found, it is
kono
parents:
diff changeset
275 -- removed from Target; otherwise it is inserted into Target.
kono
parents:
diff changeset
276
kono
parents:
diff changeset
277 function Symmetric_Difference (Left, Right : Set) return Set;
kono
parents:
diff changeset
278 -- The operation first iterates over the Left set. It calls Find to
kono
parents:
diff changeset
279 -- determine whether the element is in the Right set. If no equivalent
kono
parents:
diff changeset
280 -- element is found, the element from Left is inserted into the result. The
kono
parents:
diff changeset
281 -- operation then iterates over the Right set, to determine whether the
kono
parents:
diff changeset
282 -- element is in the Left set. If no equivalent element is found, the Right
kono
parents:
diff changeset
283 -- element is inserted into the result.
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 function "xor" (Left, Right : Set) return Set
kono
parents:
diff changeset
286 renames Symmetric_Difference;
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 function Overlap (Left, Right : Set) return Boolean;
kono
parents:
diff changeset
289 -- Iterates over the Left set (calling First and Next), calling Find to
kono
parents:
diff changeset
290 -- determine whether the element is in the Right set. If an equivalent
kono
parents:
diff changeset
291 -- element is found, the operation immediately returns True. The operation
kono
parents:
diff changeset
292 -- returns False if the iteration over Left terminates without finding any
kono
parents:
diff changeset
293 -- equivalent element in Right.
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
kono
parents:
diff changeset
296 -- Iterates over Subset (calling First and Next), calling Find to determine
kono
parents:
diff changeset
297 -- whether the element is in Of_Set. If no equivalent element is found in
kono
parents:
diff changeset
298 -- Of_Set, the operation immediately returns False. The operation returns
kono
parents:
diff changeset
299 -- True if the iteration over Subset terminates without finding an element
kono
parents:
diff changeset
300 -- not in Of_Set (that is, every element in Subset is equivalent to an
kono
parents:
diff changeset
301 -- element in Of_Set).
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 function First (Container : Set) return Cursor;
kono
parents:
diff changeset
304 -- Returns a cursor that designates the first non-empty bucket, by
kono
parents:
diff changeset
305 -- searching from the beginning of the buckets array.
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 function Next (Position : Cursor) return Cursor;
kono
parents:
diff changeset
308 -- Returns a cursor that designates the node that follows the current one
kono
parents:
diff changeset
309 -- designated by Position. If Position designates the last node in its
kono
parents:
diff changeset
310 -- bucket, the operation calls Hash to compute the index of this bucket,
kono
parents:
diff changeset
311 -- and searches the buckets array for the first non-empty bucket, starting
kono
parents:
diff changeset
312 -- from that index; otherwise, it simply follows the link to the next node
kono
parents:
diff changeset
313 -- in the same bucket.
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 procedure Next (Position : in out Cursor);
kono
parents:
diff changeset
316 -- Equivalent to Position := Next (Position)
kono
parents:
diff changeset
317
kono
parents:
diff changeset
318 function Find
kono
parents:
diff changeset
319 (Container : Set;
kono
parents:
diff changeset
320 Item : Element_Type) return Cursor;
kono
parents:
diff changeset
321 -- Searches for Item in the set. Find calls Hash to determine the item's
kono
parents:
diff changeset
322 -- bucket; if the bucket is not empty, it calls Equivalent_Elements to
kono
parents:
diff changeset
323 -- compare Item to each element in the bucket. If the search succeeds, Find
kono
parents:
diff changeset
324 -- returns a cursor designating the node containing the equivalent element;
kono
parents:
diff changeset
325 -- otherwise, it returns No_Element.
kono
parents:
diff changeset
326
kono
parents:
diff changeset
327 function Contains (Container : Set; Item : Element_Type) return Boolean;
kono
parents:
diff changeset
328 -- Equivalent to Find (Container, Item) /= No_Element
kono
parents:
diff changeset
329
kono
parents:
diff changeset
330 function Equivalent_Elements (Left, Right : Cursor) return Boolean;
kono
parents:
diff changeset
331 -- Returns the result of calling Equivalent_Elements with the elements of
kono
parents:
diff changeset
332 -- the nodes designated by cursors Left and Right.
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 function Equivalent_Elements
kono
parents:
diff changeset
335 (Left : Cursor;
kono
parents:
diff changeset
336 Right : Element_Type) return Boolean;
kono
parents:
diff changeset
337 -- Returns the result of calling Equivalent_Elements with element of the
kono
parents:
diff changeset
338 -- node designated by Left and element Right.
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 function Equivalent_Elements
kono
parents:
diff changeset
341 (Left : Element_Type;
kono
parents:
diff changeset
342 Right : Cursor) return Boolean;
kono
parents:
diff changeset
343 -- Returns the result of calling Equivalent_Elements with element Left and
kono
parents:
diff changeset
344 -- the element of the node designated by Right.
kono
parents:
diff changeset
345
kono
parents:
diff changeset
346 procedure Iterate
kono
parents:
diff changeset
347 (Container : Set;
kono
parents:
diff changeset
348 Process : not null access procedure (Position : Cursor));
kono
parents:
diff changeset
349 -- Calls Process for each node in the set
kono
parents:
diff changeset
350
kono
parents:
diff changeset
351 function Iterate
kono
parents:
diff changeset
352 (Container : Set) return Set_Iterator_Interfaces.Forward_Iterator'Class;
kono
parents:
diff changeset
353
kono
parents:
diff changeset
354 generic
kono
parents:
diff changeset
355 type Key_Type (<>) is private;
kono
parents:
diff changeset
356
kono
parents:
diff changeset
357 with function Key (Element : Element_Type) return Key_Type;
kono
parents:
diff changeset
358
kono
parents:
diff changeset
359 with function Hash (Key : Key_Type) return Hash_Type;
kono
parents:
diff changeset
360
kono
parents:
diff changeset
361 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 package Generic_Keys is
kono
parents:
diff changeset
364
kono
parents:
diff changeset
365 function Key (Position : Cursor) return Key_Type;
kono
parents:
diff changeset
366 -- Applies generic formal operation Key to the element of the node
kono
parents:
diff changeset
367 -- designated by Position.
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 function Element (Container : Set; Key : Key_Type) return Element_Type;
kono
parents:
diff changeset
370 -- Searches (as per the key-based Find) for the node containing Key, and
kono
parents:
diff changeset
371 -- returns the associated element.
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 procedure Replace
kono
parents:
diff changeset
374 (Container : in out Set;
kono
parents:
diff changeset
375 Key : Key_Type;
kono
parents:
diff changeset
376 New_Item : Element_Type);
kono
parents:
diff changeset
377 -- Searches (as per the key-based Find) for the node containing Key, and
kono
parents:
diff changeset
378 -- then replaces the element of that node (as per the element-based
kono
parents:
diff changeset
379 -- Replace_Element).
kono
parents:
diff changeset
380
kono
parents:
diff changeset
381 procedure Exclude (Container : in out Set; Key : Key_Type);
kono
parents:
diff changeset
382 -- Searches for Key in the set, and if found, removes its node from the
kono
parents:
diff changeset
383 -- set and then deallocates it. The search works by first calling Hash
kono
parents:
diff changeset
384 -- (on Key) to determine the bucket; if the bucket is not empty, it
kono
parents:
diff changeset
385 -- calls Equivalent_Keys to compare parameter Key to the value of
kono
parents:
diff changeset
386 -- generic formal operation Key applied to element of each node in the
kono
parents:
diff changeset
387 -- bucket.
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 procedure Delete (Container : in out Set; Key : Key_Type);
kono
parents:
diff changeset
390 -- Deletes the node containing Key as per Exclude, with the difference
kono
parents:
diff changeset
391 -- that Constraint_Error is raised if Key is not found.
kono
parents:
diff changeset
392
kono
parents:
diff changeset
393 function Find (Container : Set; Key : Key_Type) return Cursor;
kono
parents:
diff changeset
394 -- Searches for the node containing Key, and returns a cursor
kono
parents:
diff changeset
395 -- designating the node. The search works by first calling Hash (on Key)
kono
parents:
diff changeset
396 -- to determine the bucket. If the bucket is not empty, the search
kono
parents:
diff changeset
397 -- compares Key to the element of each node in the bucket, and returns
kono
parents:
diff changeset
398 -- the matching node. The comparison itself works by applying the
kono
parents:
diff changeset
399 -- generic formal Key operation to the element of the node, and then
kono
parents:
diff changeset
400 -- calling generic formal operation Equivalent_Keys.
kono
parents:
diff changeset
401
kono
parents:
diff changeset
402 function Contains (Container : Set; Key : Key_Type) return Boolean;
kono
parents:
diff changeset
403 -- Equivalent to Find (Container, Key) /= No_Element
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 procedure Update_Element_Preserving_Key
kono
parents:
diff changeset
406 (Container : in out Set;
kono
parents:
diff changeset
407 Position : Cursor;
kono
parents:
diff changeset
408 Process : not null access
kono
parents:
diff changeset
409 procedure (Element : in out Element_Type));
kono
parents:
diff changeset
410 -- Calls Process with the element of the node designated by Position,
kono
parents:
diff changeset
411 -- but with the restriction that the key-value of the element is not
kono
parents:
diff changeset
412 -- modified. The operation first makes a copy of the value returned by
kono
parents:
diff changeset
413 -- applying generic formal operation Key on the element of the node, and
kono
parents:
diff changeset
414 -- then calls Process with the element. The operation verifies that the
kono
parents:
diff changeset
415 -- key-part has not been modified by calling generic formal operation
kono
parents:
diff changeset
416 -- Equivalent_Keys to compare the saved key-value to the value returned
kono
parents:
diff changeset
417 -- by applying generic formal operation Key to the post-Process value of
kono
parents:
diff changeset
418 -- element. If the key values compare equal then the operation
kono
parents:
diff changeset
419 -- completes. Otherwise, the node is removed from the set and
kono
parents:
diff changeset
420 -- Program_Error is raised.
kono
parents:
diff changeset
421
kono
parents:
diff changeset
422 type Reference_Type (Element : not null access Element_Type) is private
kono
parents:
diff changeset
423 with Implicit_Dereference => Element;
kono
parents:
diff changeset
424
kono
parents:
diff changeset
425 function Reference_Preserving_Key
kono
parents:
diff changeset
426 (Container : aliased in out Set;
kono
parents:
diff changeset
427 Position : Cursor) return Reference_Type;
kono
parents:
diff changeset
428
kono
parents:
diff changeset
429 function Constant_Reference
kono
parents:
diff changeset
430 (Container : aliased Set;
kono
parents:
diff changeset
431 Key : Key_Type) return Constant_Reference_Type;
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 function Reference_Preserving_Key
kono
parents:
diff changeset
434 (Container : aliased in out Set;
kono
parents:
diff changeset
435 Key : Key_Type) return Reference_Type;
kono
parents:
diff changeset
436
kono
parents:
diff changeset
437 private
kono
parents:
diff changeset
438 use Ada.Streams;
kono
parents:
diff changeset
439 type Set_Access is access all Set;
kono
parents:
diff changeset
440 for Set_Access'Storage_Size use 0;
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 -- Key_Preserving references must carry information to allow removal
kono
parents:
diff changeset
443 -- of elements whose value may have been altered improperly, i.e. have
kono
parents:
diff changeset
444 -- been given values incompatible with the hash-code of the previous
kono
parents:
diff changeset
445 -- value, and are thus in the wrong bucket. (RM 18.7 (96.6/3))
kono
parents:
diff changeset
446
kono
parents:
diff changeset
447 -- We cannot store the key directly because it is an unconstrained type.
kono
parents:
diff changeset
448 -- To avoid using additional dynamic allocation we store the old cursor
kono
parents:
diff changeset
449 -- which simplifies possible removal. This is not possible for some
kono
parents:
diff changeset
450 -- other set types.
kono
parents:
diff changeset
451
kono
parents:
diff changeset
452 -- The mechanism is different for Update_Element_Preserving_Key, as
kono
parents:
diff changeset
453 -- in that case the check that buckets have not changed is performed
kono
parents:
diff changeset
454 -- at the time of the update, not when the reference is finalized.
kono
parents:
diff changeset
455
kono
parents:
diff changeset
456 package Impl is new Helpers.Generic_Implementation;
kono
parents:
diff changeset
457
kono
parents:
diff changeset
458 type Reference_Control_Type is
kono
parents:
diff changeset
459 new Impl.Reference_Control_Type with
kono
parents:
diff changeset
460 record
kono
parents:
diff changeset
461 Container : Set_Access;
kono
parents:
diff changeset
462 Index : Hash_Type;
kono
parents:
diff changeset
463 Old_Pos : Cursor;
kono
parents:
diff changeset
464 Old_Hash : Hash_Type;
kono
parents:
diff changeset
465 end record;
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 overriding procedure Finalize (Control : in out Reference_Control_Type);
kono
parents:
diff changeset
468 pragma Inline (Finalize);
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 type Reference_Type (Element : not null access Element_Type) is record
kono
parents:
diff changeset
471 Control : Reference_Control_Type;
kono
parents:
diff changeset
472 end record;
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 procedure Read
kono
parents:
diff changeset
475 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
476 Item : out Reference_Type);
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 for Reference_Type'Read use Read;
kono
parents:
diff changeset
479
kono
parents:
diff changeset
480 procedure Write
kono
parents:
diff changeset
481 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
482 Item : Reference_Type);
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 for Reference_Type'Write use Write;
kono
parents:
diff changeset
485 end Generic_Keys;
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 private
kono
parents:
diff changeset
488 pragma Inline (Next);
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 type Node_Type;
kono
parents:
diff changeset
491 type Node_Access is access Node_Type;
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 type Node_Type is limited record
kono
parents:
diff changeset
494 Element : aliased Element_Type;
kono
parents:
diff changeset
495 Next : Node_Access;
kono
parents:
diff changeset
496 end record;
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 package HT_Types is
kono
parents:
diff changeset
499 new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
kono
parents:
diff changeset
500
kono
parents:
diff changeset
501 type Set is new Ada.Finalization.Controlled with record
kono
parents:
diff changeset
502 HT : HT_Types.Hash_Table_Type;
kono
parents:
diff changeset
503 end record;
kono
parents:
diff changeset
504
kono
parents:
diff changeset
505 overriding procedure Adjust (Container : in out Set);
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 overriding procedure Finalize (Container : in out Set);
kono
parents:
diff changeset
508
kono
parents:
diff changeset
509 use HT_Types, HT_Types.Implementation;
kono
parents:
diff changeset
510 use Ada.Finalization;
kono
parents:
diff changeset
511 use Ada.Streams;
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 procedure Write
kono
parents:
diff changeset
514 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
515 Container : Set);
kono
parents:
diff changeset
516
kono
parents:
diff changeset
517 for Set'Write use Write;
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 procedure Read
kono
parents:
diff changeset
520 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
521 Container : out Set);
kono
parents:
diff changeset
522
kono
parents:
diff changeset
523 for Set'Read use Read;
kono
parents:
diff changeset
524
kono
parents:
diff changeset
525 type Set_Access is access all Set;
kono
parents:
diff changeset
526 for Set_Access'Storage_Size use 0;
kono
parents:
diff changeset
527
kono
parents:
diff changeset
528 type Cursor is record
kono
parents:
diff changeset
529 Container : Set_Access;
kono
parents:
diff changeset
530 Node : Node_Access;
kono
parents:
diff changeset
531 Position : Hash_Type := Hash_Type'Last;
kono
parents:
diff changeset
532 end record;
kono
parents:
diff changeset
533
kono
parents:
diff changeset
534 procedure Write
kono
parents:
diff changeset
535 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
536 Item : Cursor);
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 for Cursor'Write use Write;
kono
parents:
diff changeset
539
kono
parents:
diff changeset
540 procedure Read
kono
parents:
diff changeset
541 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
542 Item : out Cursor);
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 for Cursor'Read use Read;
kono
parents:
diff changeset
545
kono
parents:
diff changeset
546 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
kono
parents:
diff changeset
547 -- It is necessary to rename this here, so that the compiler can find it
kono
parents:
diff changeset
548
kono
parents:
diff changeset
549 type Constant_Reference_Type
kono
parents:
diff changeset
550 (Element : not null access constant Element_Type) is
kono
parents:
diff changeset
551 record
kono
parents:
diff changeset
552 Control : Reference_Control_Type :=
kono
parents:
diff changeset
553 raise Program_Error with "uninitialized reference";
kono
parents:
diff changeset
554 -- The RM says, "The default initialization of an object of
kono
parents:
diff changeset
555 -- type Constant_Reference_Type or Reference_Type propagates
kono
parents:
diff changeset
556 -- Program_Error."
kono
parents:
diff changeset
557 end record;
kono
parents:
diff changeset
558
kono
parents:
diff changeset
559 procedure Read
kono
parents:
diff changeset
560 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
561 Item : out Constant_Reference_Type);
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 for Constant_Reference_Type'Read use Read;
kono
parents:
diff changeset
564
kono
parents:
diff changeset
565 procedure Write
kono
parents:
diff changeset
566 (Stream : not null access Root_Stream_Type'Class;
kono
parents:
diff changeset
567 Item : Constant_Reference_Type);
kono
parents:
diff changeset
568
kono
parents:
diff changeset
569 for Constant_Reference_Type'Write use Write;
kono
parents:
diff changeset
570
kono
parents:
diff changeset
571 -- Three operations are used to optimize in the expansion of "for ... of"
kono
parents:
diff changeset
572 -- loops: the Next(Cursor) procedure in the visible part, and the following
kono
parents:
diff changeset
573 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
kono
parents:
diff changeset
574 -- details.
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 function Pseudo_Reference
kono
parents:
diff changeset
577 (Container : aliased Set'Class) return Reference_Control_Type;
kono
parents:
diff changeset
578 pragma Inline (Pseudo_Reference);
kono
parents:
diff changeset
579 -- Creates an object of type Reference_Control_Type pointing to the
kono
parents:
diff changeset
580 -- container, and increments the Lock. Finalization of this object will
kono
parents:
diff changeset
581 -- decrement the Lock.
kono
parents:
diff changeset
582
kono
parents:
diff changeset
583 type Element_Access is access all Element_Type with
kono
parents:
diff changeset
584 Storage_Size => 0;
kono
parents:
diff changeset
585
kono
parents:
diff changeset
586 function Get_Element_Access
kono
parents:
diff changeset
587 (Position : Cursor) return not null Element_Access;
kono
parents:
diff changeset
588 -- Returns a pointer to the element designated by Position.
kono
parents:
diff changeset
589
kono
parents:
diff changeset
590 Empty_Set : constant Set := (Controlled with others => <>);
kono
parents:
diff changeset
591
kono
parents:
diff changeset
592 No_Element : constant Cursor :=
kono
parents:
diff changeset
593 (Container => null, Node => null, Position => Hash_Type'Last);
kono
parents:
diff changeset
594
kono
parents:
diff changeset
595 type Iterator is new Limited_Controlled and
kono
parents:
diff changeset
596 Set_Iterator_Interfaces.Forward_Iterator with
kono
parents:
diff changeset
597 record
kono
parents:
diff changeset
598 Container : Set_Access;
kono
parents:
diff changeset
599 end record
kono
parents:
diff changeset
600 with Disable_Controlled => not T_Check;
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 overriding function First (Object : Iterator) return Cursor;
kono
parents:
diff changeset
603
kono
parents:
diff changeset
604 overriding function Next
kono
parents:
diff changeset
605 (Object : Iterator;
kono
parents:
diff changeset
606 Position : Cursor) return Cursor;
kono
parents:
diff changeset
607 overriding procedure Finalize (Object : in out Iterator);
kono
parents:
diff changeset
608
kono
parents:
diff changeset
609 end Ada.Containers.Hashed_Sets;