fixing pr42337
[official-gcc.git] / gcc / ada / a-cihase.ads
bloba174c7d980916c6d62f7f198f4223b014d7f9781
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_SETS --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2004-2009, Free Software Foundation, Inc. --
10 -- --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the contents of the part following the private keyword. --
14 -- --
15 -- GNAT is free software; you can redistribute it and/or modify it under --
16 -- terms of the GNU General Public License as published by the Free Soft- --
17 -- ware Foundation; either version 3, or (at your option) any later ver- --
18 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE. --
21 -- --
22 -- As a special exception under Section 7 of GPL version 3, you are granted --
23 -- additional permissions described in the GCC Runtime Library Exception, --
24 -- version 3.1, as published by the Free Software Foundation. --
25 -- --
26 -- You should have received a copy of the GNU General Public License and --
27 -- a copy of the GCC Runtime Library Exception along with this program; --
28 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
29 -- <http://www.gnu.org/licenses/>. --
30 -- --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 private with Ada.Containers.Hash_Tables;
35 private with Ada.Streams;
36 private with Ada.Finalization;
38 generic
39 type Element_Type (<>) is private;
41 with function Hash (Element : Element_Type) return Hash_Type;
43 with function Equivalent_Elements (Left, Right : Element_Type)
44 return Boolean;
46 with function "=" (Left, Right : Element_Type) return Boolean is <>;
48 package Ada.Containers.Indefinite_Hashed_Sets is
49 pragma Preelaborate;
50 pragma Remote_Types;
52 type Set is tagged private;
53 pragma Preelaborable_Initialization (Set);
55 type Cursor is private;
56 pragma Preelaborable_Initialization (Cursor);
58 Empty_Set : constant Set;
59 -- Set objects declared without an initialization expression are
60 -- initialized to the value Empty_Set.
62 No_Element : constant Cursor;
63 -- Cursor objects declared without an initialization expression are
64 -- initialized to the value No_Element.
66 function "=" (Left, Right : Set) return Boolean;
67 -- For each element in Left, set equality attempts to find the equal
68 -- element in Right; if a search fails, then set equality immediately
69 -- returns False. The search works by calling Hash to find the bucket in
70 -- the Right set that corresponds to the Left element. If the bucket is
71 -- non-empty, the search calls the generic formal element equality operator
72 -- to compare the element (in Left) to the element of each node in the
73 -- bucket (in Right); the search terminates when a matching node in the
74 -- bucket is found, or the nodes in the bucket are exhausted. (Note that
75 -- element equality is called here, not Equivalent_Elements. Set equality
76 -- is the only operation in which element equality is used. Compare set
77 -- equality to Equivalent_Sets, which does call Equivalent_Elements.)
79 function Equivalent_Sets (Left, Right : Set) return Boolean;
80 -- Similar to set equality, with the difference that the element in Left is
81 -- compared to the elements in Right using the generic formal
82 -- Equivalent_Elements operation instead of element equality.
84 function To_Set (New_Item : Element_Type) return Set;
85 -- Constructs a singleton set comprising New_Element. To_Set calls Hash to
86 -- determine the bucket for New_Item.
88 function Capacity (Container : Set) return Count_Type;
89 -- Returns the current capacity of the set. Capacity is the maximum length
90 -- before which rehashing in guaranteed not to occur.
92 procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
93 -- Adjusts the current capacity, by allocating a new buckets array. If the
94 -- requested capacity is less than the current capacity, then the capacity
95 -- is contracted (to a value not less than the current length). If the
96 -- requested capacity is greater than the current capacity, then the
97 -- capacity is expanded (to a value not less than what is requested). In
98 -- either case, the nodes are rehashed from the old buckets array onto the
99 -- new buckets array (Hash is called once for each existing element in
100 -- order to compute the new index), and then the old buckets array is
101 -- deallocated.
103 function Length (Container : Set) return Count_Type;
104 -- Returns the number of items in the set
106 function Is_Empty (Container : Set) return Boolean;
107 -- Equivalent to Length (Container) = 0
109 procedure Clear (Container : in out Set);
110 -- Removes all of the items from the set
112 function Element (Position : Cursor) return Element_Type;
113 -- Returns the element of the node designated by the cursor
115 procedure Replace_Element
116 (Container : in out Set;
117 Position : Cursor;
118 New_Item : Element_Type);
119 -- If New_Item is equivalent (as determined by calling Equivalent_Elements)
120 -- to the element of the node designated by Position, then New_Element is
121 -- assigned to that element. Otherwise, it calls Hash to determine the
122 -- bucket for New_Item. If the bucket is not empty, then it calls
123 -- Equivalent_Elements for each node in that bucket to determine whether
124 -- New_Item is equivalent to an element in that bucket. If
125 -- Equivalent_Elements returns True then Program_Error is raised (because
126 -- an element may appear only once in the set); otherwise, New_Item is
127 -- assigned to the node designated by Position, and the node is moved to
128 -- its new bucket.
130 procedure Query_Element
131 (Position : Cursor;
132 Process : not null access procedure (Element : Element_Type));
133 -- Calls Process with the element (having only a constant view) of the node
134 -- designed by the cursor.
136 procedure Move (Target : in out Set; Source : in out Set);
137 -- Clears Target (if it's not empty), and then moves (not copies) the
138 -- buckets array and nodes from Source to Target.
140 procedure Insert
141 (Container : in out Set;
142 New_Item : Element_Type;
143 Position : out Cursor;
144 Inserted : out Boolean);
145 -- Conditionally inserts New_Item into the set. If New_Item is already in
146 -- the set, then Inserted returns False and Position designates the node
147 -- containing the existing element (which is not modified). If New_Item is
148 -- not already in the set, then Inserted returns True and Position
149 -- designates the newly-inserted node containing New_Item. The search for
150 -- an existing element works as follows. Hash is called to determine
151 -- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
152 -- is called to compare New_Item to the element of each node in that
153 -- bucket. If the bucket is empty, or there were no equivalent elements in
154 -- the bucket, the search "fails" and the New_Item is inserted in the set
155 -- (and Inserted returns True); otherwise, the search "succeeds" (and
156 -- Inserted returns False).
158 procedure Insert (Container : in out Set; New_Item : Element_Type);
159 -- Attempts to insert New_Item into the set, performing the usual insertion
160 -- search (which involves calling both Hash and Equivalent_Elements); if
161 -- the search succeeds (New_Item is equivalent to an element already in the
162 -- set, and so was not inserted), then this operation raises
163 -- Constraint_Error. (This version of Insert is similar to Replace, but
164 -- having the opposite exception behavior. It is intended for use when you
165 -- want to assert that the item is not already in the set.)
167 procedure Include (Container : in out Set; New_Item : Element_Type);
168 -- Attempts to insert New_Item into the set. If an element equivalent to
169 -- New_Item is already in the set (the insertion search succeeded, and
170 -- hence New_Item was not inserted), then the value of New_Item is assigned
171 -- to the existing element. (This insertion operation only raises an
172 -- exception if cursor tampering occurs. It is intended for use when you
173 -- want to insert the item in the set, and you don't care whether an
174 -- equivalent element is already present.)
176 procedure Replace (Container : in out Set; New_Item : Element_Type);
177 -- Searches for New_Item in the set; if the search fails (because an
178 -- equivalent element was not in the set), then it raises
179 -- Constraint_Error. Otherwise, the existing element is assigned the value
180 -- New_Item. (This is similar to Insert, but with the opposite exception
181 -- behavior. It is intended for use when you want to assert that the item
182 -- is already in the set.)
184 procedure Exclude (Container : in out Set; Item : Element_Type);
185 -- Searches for Item in the set, and if found, removes its node from the
186 -- set and then deallocates it. The search works as follows. The operation
187 -- calls Hash to determine the item's bucket; if the bucket is not empty,
188 -- it calls Equivalent_Elements to compare Item to the element of each node
189 -- in the bucket. (This is the deletion analog of Include. It is intended
190 -- for use when you want to remove the item from the set, but don't care
191 -- whether the item is already in the set.)
193 procedure Delete (Container : in out Set; Item : Element_Type);
194 -- Searches for Item in the set (which involves calling both Hash and
195 -- Equivalent_Elements). If the search fails, then the operation raises
196 -- Constraint_Error. Otherwise it removes the node from the set and then
197 -- deallocates it. (This is the deletion analog of non-conditional
198 -- Insert. It is intended for use when you want to assert that the item is
199 -- already in the set.)
201 procedure Delete (Container : in out Set; Position : in out Cursor);
202 -- Removes the node designated by Position from the set, and then
203 -- deallocates the node. The operation calls Hash to determine the bucket,
204 -- and then compares Position to each node in the bucket until there's a
205 -- match (it does not call Equivalent_Elements).
207 procedure Union (Target : in out Set; Source : Set);
208 -- The operation first calls Reserve_Capacity if the current capacity is
209 -- less than the sum of the lengths of Source and Target. It then iterates
210 -- over the Source set, and conditionally inserts each element into Target.
212 function Union (Left, Right : Set) return Set;
213 -- The operation first copies the Left set to the result, and then iterates
214 -- over the Right set to conditionally insert each element into the result.
216 function "or" (Left, Right : Set) return Set renames Union;
218 procedure Intersection (Target : in out Set; Source : Set);
219 -- Iterates over the Target set (calling First and Next), calling Find to
220 -- determine whether the element is in Source. If an equivalent element is
221 -- not found in Source, the element is deleted from Target.
223 function Intersection (Left, Right : Set) return Set;
224 -- Iterates over the Left set, calling Find to determine whether the
225 -- element is in Right. If an equivalent element is found, it is inserted
226 -- into the result set.
228 function "and" (Left, Right : Set) return Set renames Intersection;
230 procedure Difference (Target : in out Set; Source : Set);
231 -- Iterates over the Source (calling First and Next), calling Find to
232 -- determine whether the element is in Target. If an equivalent element is
233 -- found, it is deleted from Target.
235 function Difference (Left, Right : Set) return Set;
236 -- Iterates over the Left set, calling Find to determine whether the
237 -- element is in the Right set. If an equivalent element is not found, the
238 -- element is inserted into the result set.
240 function "-" (Left, Right : Set) return Set renames Difference;
242 procedure Symmetric_Difference (Target : in out Set; Source : Set);
243 -- The operation first calls Reserve_Capacity if the current capacity is
244 -- less than the sum of the lengths of Source and Target. It then iterates
245 -- over the Source set, searching for the element in Target (calling Hash
246 -- and Equivalent_Elements). If an equivalent element is found, it is
247 -- removed from Target; otherwise it is inserted into Target.
249 function Symmetric_Difference (Left, Right : Set) return Set;
250 -- The operation first iterates over the Left set. It calls Find to
251 -- determine whether the element is in the Right set. If no equivalent
252 -- element is found, the element from Left is inserted into the result. The
253 -- operation then iterates over the Right set, to determine whether the
254 -- element is in the Left set. If no equivalent element is found, the Right
255 -- element is inserted into the result.
257 function "xor" (Left, Right : Set) return Set
258 renames Symmetric_Difference;
260 function Overlap (Left, Right : Set) return Boolean;
261 -- Iterates over the Left set (calling First and Next), calling Find to
262 -- determine whether the element is in the Right set. If an equivalent
263 -- element is found, the operation immediately returns True. The operation
264 -- returns False if the iteration over Left terminates without finding any
265 -- equivalent element in Right.
267 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
268 -- Iterates over Subset (calling First and Next), calling Find to determine
269 -- whether the element is in Of_Set. If no equivalent element is found in
270 -- Of_Set, the operation immediately returns False. The operation returns
271 -- True if the iteration over Subset terminates without finding an element
272 -- not in Of_Set (that is, every element in Subset is equivalent to an
273 -- element in Of_Set).
275 function First (Container : Set) return Cursor;
276 -- Returns a cursor that designates the first non-empty bucket, by
277 -- searching from the beginning of the buckets array.
279 function Next (Position : Cursor) return Cursor;
280 -- Returns a cursor that designates the node that follows the current one
281 -- designated by Position. If Position designates the last node in its
282 -- bucket, the operation calls Hash to compute the index of this bucket,
283 -- and searches the buckets array for the first non-empty bucket, starting
284 -- from that index; otherwise, it simply follows the link to the next node
285 -- in the same bucket.
287 procedure Next (Position : in out Cursor);
288 -- Equivalent to Position := Next (Position)
290 function Find (Container : Set; Item : Element_Type) return Cursor;
291 -- Searches for Item in the set. Find calls Hash to determine the item's
292 -- bucket; if the bucket is not empty, it calls Equivalent_Elements to
293 -- compare Item to each element in the bucket. If the search succeeds, Find
294 -- returns a cursor designating the node containing the equivalent element;
295 -- otherwise, it returns No_Element.
297 function Contains (Container : Set; Item : Element_Type) return Boolean;
298 -- Equivalent to Find (Container, Item) /= No_Element
300 function Has_Element (Position : Cursor) return Boolean;
301 -- Equivalent to Position /= No_Element
303 function Equivalent_Elements (Left, Right : Cursor) return Boolean;
304 -- Returns the result of calling Equivalent_Elements with the elements of
305 -- the nodes designated by cursors Left and Right.
307 function Equivalent_Elements
308 (Left : Cursor;
309 Right : Element_Type) return Boolean;
310 -- Returns the result of calling Equivalent_Elements with element of the
311 -- node designated by Left and element Right.
313 function Equivalent_Elements
314 (Left : Element_Type;
315 Right : Cursor) return Boolean;
316 -- Returns the result of calling Equivalent_Elements with element Left and
317 -- the element of the node designated by Right.
319 procedure Iterate
320 (Container : Set;
321 Process : not null access procedure (Position : Cursor));
322 -- Calls Process for each node in the set
324 generic
325 type Key_Type (<>) is private;
327 with function Key (Element : Element_Type) return Key_Type;
329 with function Hash (Key : Key_Type) return Hash_Type;
331 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
333 package Generic_Keys is
335 function Key (Position : Cursor) return Key_Type;
336 -- Applies generic formal operation Key to the element of the node
337 -- designated by Position.
339 function Element (Container : Set; Key : Key_Type) return Element_Type;
340 -- Searches (as per the key-based Find) for the node containing Key, and
341 -- returns the associated element.
343 procedure Replace
344 (Container : in out Set;
345 Key : Key_Type;
346 New_Item : Element_Type);
347 -- Searches (as per the key-based Find) for the node containing Key, and
348 -- then replaces the element of that node (as per the element-based
349 -- Replace_Element).
351 procedure Exclude (Container : in out Set; Key : Key_Type);
352 -- Searches for Key in the set, and if found, removes its node from the
353 -- set and then deallocates it. The search works by first calling Hash
354 -- (on Key) to determine the bucket; if the bucket is not empty, it
355 -- calls Equivalent_Keys to compare parameter Key to the value of
356 -- generic formal operation Key applied to element of each node in the
357 -- bucket.
359 procedure Delete (Container : in out Set; Key : Key_Type);
360 -- Deletes the node containing Key as per Exclude, with the difference
361 -- that Constraint_Error is raised if Key is not found.
363 function Find (Container : Set; Key : Key_Type) return Cursor;
364 -- Searches for the node containing Key, and returns a cursor
365 -- designating the node. The search works by first calling Hash (on Key)
366 -- to determine the bucket. If the bucket is not empty, the search
367 -- compares Key to the element of each node in the bucket, and returns
368 -- the matching node. The comparison itself works by applying the
369 -- generic formal Key operation to the element of the node, and then
370 -- calling generic formal operation Equivalent_Keys.
372 function Contains (Container : Set; Key : Key_Type) return Boolean;
373 -- Equivalent to Find (Container, Key) /= No_Element
375 procedure Update_Element_Preserving_Key
376 (Container : in out Set;
377 Position : Cursor;
378 Process : not null access
379 procedure (Element : in out Element_Type));
380 -- Calls Process with the element of the node designated by Position,
381 -- but with the restriction that the key-value of the element is not
382 -- modified. The operation first makes a copy of the value returned by
383 -- applying generic formal operation Key on the element of the node, and
384 -- then calls Process with the element. The operation verifies that the
385 -- key-part has not been modified by calling generic formal operation
386 -- Equivalent_Keys to compare the saved key-value to the value returned
387 -- by applying generic formal operation Key to the post-Process value of
388 -- element. If the key values compare equal then the operation
389 -- completes. Otherwise, the node is removed from the map and
390 -- Program_Error is raised.
392 end Generic_Keys;
394 private
396 pragma Inline (Next);
398 type Node_Type;
399 type Node_Access is access Node_Type;
401 type Element_Access is access Element_Type;
403 type Node_Type is limited record
404 Element : Element_Access;
405 Next : Node_Access;
406 end record;
408 package HT_Types is
409 new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
411 type Set is new Ada.Finalization.Controlled with record
412 HT : HT_Types.Hash_Table_Type;
413 end record;
415 overriding
416 procedure Adjust (Container : in out Set);
418 overriding
419 procedure Finalize (Container : in out Set);
421 use HT_Types;
422 use Ada.Finalization;
423 use Ada.Streams;
425 type Set_Access is access all Set;
426 for Set_Access'Storage_Size use 0;
428 type Cursor is record
429 Container : Set_Access;
430 Node : Node_Access;
431 end record;
433 procedure Write
434 (Stream : not null access Root_Stream_Type'Class;
435 Item : Cursor);
437 for Cursor'Write use Write;
439 procedure Read
440 (Stream : not null access Root_Stream_Type'Class;
441 Item : out Cursor);
443 for Cursor'Read use Read;
445 No_Element : constant Cursor := (Container => null, Node => null);
447 procedure Write
448 (Stream : not null access Root_Stream_Type'Class;
449 Container : Set);
451 for Set'Write use Write;
453 procedure Read
454 (Stream : not null access Root_Stream_Type'Class;
455 Container : out Set);
457 for Set'Read use Read;
459 Empty_Set : constant Set := (Controlled with HT => (null, 0, 0, 0));
461 end Ada.Containers.Indefinite_Hashed_Sets;