1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ H A S H E D _ S E T S --
10 -- Copyright (C) 2004-2007, Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
34 -- This unit was originally developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 with Ada.Containers.Hash_Tables;
39 with Ada.Finalization;
42 type Element_Type (<>) is private;
44 with function Hash (Element : Element_Type) return Hash_Type;
46 with function Equivalent_Elements (Left, Right : Element_Type)
49 with function "=" (Left, Right : Element_Type) return Boolean is <>;
51 package Ada.Containers.Indefinite_Hashed_Sets is
55 type Set is tagged private;
56 pragma Preelaborable_Initialization (Set);
58 type Cursor is private;
59 pragma Preelaborable_Initialization (Cursor);
61 Empty_Set : constant Set;
62 -- Set objects declared without an initialization expression are
63 -- initialized to the value Empty_Set.
65 No_Element : constant Cursor;
66 -- Cursor objects declared without an initialization expression are
67 -- initialized to the value No_Element.
69 function "=" (Left, Right : Set) return Boolean;
70 -- For each element in Left, set equality attempts to find the equal
71 -- element in Right; if a search fails, then set equality immediately
72 -- returns False. The search works by calling Hash to find the bucket in
73 -- the Right set that corresponds to the Left element. If the bucket is
74 -- non-empty, the search calls the generic formal element equality operator
75 -- to compare the element (in Left) to the element of each node in the
76 -- bucket (in Right); the search terminates when a matching node in the
77 -- bucket is found, or the nodes in the bucket are exhausted. (Note that
78 -- element equality is called here, not Equivalent_Elements. Set equality
79 -- is the only operation in which element equality is used. Compare set
80 -- equality to Equivalent_Sets, which does call Equivalent_Elements.)
82 function Equivalent_Sets (Left, Right : Set) return Boolean;
83 -- Similar to set equality, with the difference that the element in Left is
84 -- compared to the elements in Right using the generic formal
85 -- Equivalent_Elements operation instead of element equality.
87 function To_Set (New_Item : Element_Type) return Set;
88 -- Constructs a singleton set comprising New_Element. To_Set calls Hash to
89 -- determine the bucket for New_Item.
91 function Capacity (Container : Set) return Count_Type;
92 -- Returns the current capacity of the set. Capacity is the maximum length
93 -- before which rehashing in guaranteed not to occur.
95 procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
96 -- Adjusts the current capacity, by allocating a new buckets array. If the
97 -- requested capacity is less than the current capacity, then the capacity
98 -- is contracted (to a value not less than the current length). If the
99 -- requested capacity is greater than the current capacity, then the
100 -- capacity is expanded (to a value not less than what is requested). In
101 -- either case, the nodes are rehashed from the old buckets array onto the
102 -- new buckets array (Hash is called once for each existing element in
103 -- order to compute the new index), and then the old buckets array is
106 function Length (Container : Set) return Count_Type;
107 -- Returns the number of items in the set
109 function Is_Empty (Container : Set) return Boolean;
110 -- Equivalent to Length (Container) = 0
112 procedure Clear (Container : in out Set);
113 -- Removes all of the items from the set
115 function Element (Position : Cursor) return Element_Type;
116 -- Returns the element of the node designated by the cursor
118 procedure Replace_Element
119 (Container : in out Set;
121 New_Item : Element_Type);
122 -- If New_Item is equivalent (as determined by calling Equivalent_Elements)
123 -- to the element of the node designated by Position, then New_Element is
124 -- assigned to that element. Otherwise, it calls Hash to determine the
125 -- bucket for New_Item. If the bucket is not empty, then it calls
126 -- Equivalent_Elements for each node in that bucket to determine whether
127 -- New_Item is equivalent to an element in that bucket. If
128 -- Equivalent_Elements returns True then Program_Error is raised (because
129 -- an element may appear only once in the set); otherwise, New_Item is
130 -- assigned to the node designated by Position, and the node is moved to
133 procedure Query_Element
135 Process : not null access procedure (Element : Element_Type));
136 -- Calls Process with the element (having only a constant view) of the node
137 -- designed by the cursor.
139 procedure Move (Target : in out Set; Source : in out Set);
140 -- Clears Target (if it's not empty), and then moves (not copies) the
141 -- buckets array and nodes from Source to Target.
144 (Container : in out Set;
145 New_Item : Element_Type;
146 Position : out Cursor;
147 Inserted : out Boolean);
148 -- Conditionally inserts New_Item into the set. If New_Item is already in
149 -- the set, then Inserted returns False and Position designates the node
150 -- containing the existing element (which is not modified). If New_Item is
151 -- not already in the set, then Inserted returns True and Position
152 -- designates the newly-inserted node containing New_Item. The search for
153 -- an existing element works as follows. Hash is called to determine
154 -- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
155 -- is called to compare New_Item to the element of each node in that
156 -- bucket. If the bucket is empty, or there were no equivalent elements in
157 -- the bucket, the search "fails" and the New_Item is inserted in the set
158 -- (and Inserted returns True); otherwise, the search "succeeds" (and
159 -- Inserted returns False).
161 procedure Insert (Container : in out Set; New_Item : Element_Type);
162 -- Attempts to insert New_Item into the set, performing the usual insertion
163 -- search (which involves calling both Hash and Equivalent_Elements); if
164 -- the search succeeds (New_Item is equivalent to an element already in the
165 -- set, and so was not inserted), then this operation raises
166 -- Constraint_Error. (This version of Insert is similar to Replace, but
167 -- having the opposite exception behavior. It is intended for use when you
168 -- want to assert that the item is not already in the set.)
170 procedure Include (Container : in out Set; New_Item : Element_Type);
171 -- Attempts to insert New_Item into the set. If an element equivalent to
172 -- New_Item is already in the set (the insertion search succeeded, and
173 -- hence New_Item was not inserted), then the value of New_Item is assigned
174 -- to the existing element. (This insertion operation only raises an
175 -- exception if cursor tampering occurs. It is intended for use when you
176 -- want to insert the item in the set, and you don't care whether an
177 -- equivalent element is already present.)
179 procedure Replace (Container : in out Set; New_Item : Element_Type);
180 -- Searches for New_Item in the set; if the search fails (because an
181 -- equivalent element was not in the set), then it raises
182 -- Constraint_Error. Otherwise, the existing element is assigned the value
183 -- New_Item. (This is similar to Insert, but with the opposite exception
184 -- behavior. It is intended for use when you want to assert that the item
185 -- is already in the set.)
187 procedure Exclude (Container : in out Set; Item : Element_Type);
188 -- Searches for Item in the set, and if found, removes its node from the
189 -- set and then deallocates it. The search works as follows. The operation
190 -- calls Hash to determine the item's bucket; if the bucket is not empty,
191 -- it calls Equivalent_Elements to compare Item to the element of each node
192 -- in the bucket. (This is the deletion analog of Include. It is intended
193 -- for use when you want to remove the item from the set, but don't care
194 -- whether the item is already in the set.)
196 procedure Delete (Container : in out Set; Item : Element_Type);
197 -- Searches for Item in the set (which involves calling both Hash and
198 -- Equivalent_Elements). If the search fails, then the operation raises
199 -- Constraint_Error. Otherwise it removes the node from the set and then
200 -- deallocates it. (This is the deletion analog of non-conditional
201 -- Insert. It is intended for use when you want to assert that the item is
202 -- already in the set.)
204 procedure Delete (Container : in out Set; Position : in out Cursor);
205 -- Removes the node designated by Position from the set, and then
206 -- deallocates the node. The operation calls Hash to determine the bucket,
207 -- and then compares Position to each node in the bucket until there's a
208 -- match (it does not call Equivalent_Elements).
210 procedure Union (Target : in out Set; Source : Set);
211 -- The operation first calls Reserve_Capacity if the current capacity is
212 -- less than the sum of the lengths of Source and Target. It then iterates
213 -- over the Source set, and conditionally inserts each element into Target.
215 function Union (Left, Right : Set) return Set;
216 -- The operation first copies the Left set to the result, and then iterates
217 -- over the Right set to conditionally insert each element into the result.
219 function "or" (Left, Right : Set) return Set renames Union;
221 procedure Intersection (Target : in out Set; Source : Set);
222 -- Iterates over the Target set (calling First and Next), calling Find to
223 -- determine whether the element is in Source. If an equivalent element is
224 -- not found in Source, the element is deleted from Target.
226 function Intersection (Left, Right : Set) return Set;
227 -- Iterates over the Left set, calling Find to determine whether the
228 -- element is in Right. If an equivalent element is found, it is inserted
229 -- into the result set.
231 function "and" (Left, Right : Set) return Set renames Intersection;
233 procedure Difference (Target : in out Set; Source : Set);
234 -- Iterates over the Source (calling First and Next), calling Find to
235 -- determine whether the element is in Target. If an equivalent element is
236 -- found, it is deleted from Target.
238 function Difference (Left, Right : Set) return Set;
239 -- Iterates over the Left set, calling Find to determine whether the
240 -- element is in the Right set. If an equivalent element is not found, the
241 -- element is inserted into the result set.
243 function "-" (Left, Right : Set) return Set renames Difference;
245 procedure Symmetric_Difference (Target : in out Set; Source : Set);
246 -- The operation first calls Reserve_Capacity if the current capacity is
247 -- less than the sum of the lengths of Source and Target. It then iterates
248 -- over the Source set, searching for the element in Target (calling Hash
249 -- and Equivalent_Elements). If an equivalent element is found, it is
250 -- removed from Target; otherwise it is inserted into Target.
252 function Symmetric_Difference (Left, Right : Set) return Set;
253 -- The operation first iterates over the Left set. It calls Find to
254 -- determine whether the element is in the Right set. If no equivalent
255 -- element is found, the element from Left is inserted into the result. The
256 -- operation then iterates over the Right set, to determine whether the
257 -- element is in the Left set. If no equivalent element is found, the Right
258 -- element is inserted into the result.
260 function "xor" (Left, Right : Set) return Set
261 renames Symmetric_Difference;
263 function Overlap (Left, Right : Set) return Boolean;
264 -- Iterates over the Left set (calling First and Next), calling Find to
265 -- determine whether the element is in the Right set. If an equivalent
266 -- element is found, the operation immediately returns True. The operation
267 -- returns False if the iteration over Left terminates without finding any
268 -- equivalent element in Right.
270 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
271 -- Iterates over Subset (calling First and Next), calling Find to determine
272 -- whether the element is in Of_Set. If no equivalent element is found in
273 -- Of_Set, the operation immediately returns False. The operation returns
274 -- True if the iteration over Subset terminates without finding an element
275 -- not in Of_Set (that is, every element in Subset is equivalent to an
276 -- element in Of_Set).
278 function First (Container : Set) return Cursor;
279 -- Returns a cursor that designates the first non-empty bucket, by
280 -- searching from the beginning of the buckets array.
282 function Next (Position : Cursor) return Cursor;
283 -- Returns a cursor that designates the node that follows the current one
284 -- designated by Position. If Position designates the last node in its
285 -- bucket, the operation calls Hash to compute the index of this bucket,
286 -- and searches the buckets array for the first non-empty bucket, starting
287 -- from that index; otherwise, it simply follows the link to the next node
288 -- in the same bucket.
290 procedure Next (Position : in out Cursor);
291 -- Equivalent to Position := Next (Position)
293 function Find (Container : Set; Item : Element_Type) return Cursor;
294 -- Searches for Item in the set. Find calls Hash to determine the item's
295 -- bucket; if the bucket is not empty, it calls Equivalent_Elements to
296 -- compare Item to each element in the bucket. If the search succeeds, Find
297 -- returns a cursor designating the node containing the equivalent element;
298 -- otherwise, it returns No_Element.
300 function Contains (Container : Set; Item : Element_Type) return Boolean;
301 -- Equivalent to Find (Container, Item) /= No_Element
303 function Has_Element (Position : Cursor) return Boolean;
304 -- Equivalent to Position /= No_Element
306 function Equivalent_Elements (Left, Right : Cursor) return Boolean;
307 -- Returns the result of calling Equivalent_Elements with the elements of
308 -- the nodes designated by cursors Left and Right.
310 function Equivalent_Elements
312 Right : Element_Type) return Boolean;
313 -- Returns the result of calling Equivalent_Elements with element of the
314 -- node designated by Left and element Right.
316 function Equivalent_Elements
317 (Left : Element_Type;
318 Right : Cursor) return Boolean;
319 -- Returns the result of calling Equivalent_Elements with element Left and
320 -- the element of the node designated by Right.
324 Process : not null access procedure (Position : Cursor));
325 -- Calls Process for each node in the set
328 type Key_Type (<>) is private;
330 with function Key (Element : Element_Type) return Key_Type;
332 with function Hash (Key : Key_Type) return Hash_Type;
334 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
336 package Generic_Keys is
338 function Key (Position : Cursor) return Key_Type;
339 -- Applies generic formal operation Key to the element of the node
340 -- designated by Position.
342 function Element (Container : Set; Key : Key_Type) return Element_Type;
343 -- Searches (as per the key-based Find) for the node containing Key, and
344 -- returns the associated element.
347 (Container : in out Set;
349 New_Item : Element_Type);
350 -- Searches (as per the key-based Find) for the node containing Key, and
351 -- then replaces the element of that node (as per the element-based
354 procedure Exclude (Container : in out Set; Key : Key_Type);
355 -- Searches for Key in the set, and if found, removes its node from the
356 -- set and then deallocates it. The search works by first calling Hash
357 -- (on Key) to determine the bucket; if the bucket is not empty, it
358 -- calls Equivalent_Keys to compare parameter Key to the value of
359 -- generic formal operation Key applied to element of each node in the
362 procedure Delete (Container : in out Set; Key : Key_Type);
363 -- Deletes the node containing Key as per Exclude, with the difference
364 -- that Constraint_Error is raised if Key is not found.
366 function Find (Container : Set; Key : Key_Type) return Cursor;
367 -- Searches for the node containing Key, and returns a cursor
368 -- designating the node. The search works by first calling Hash (on Key)
369 -- to determine the bucket. If the bucket is not empty, the search
370 -- compares Key to the element of each node in the bucket, and returns
371 -- the matching node. The comparison itself works by applying the
372 -- generic formal Key operation to the element of the node, and then
373 -- calling generic formal operation Equivalent_Keys.
375 function Contains (Container : Set; Key : Key_Type) return Boolean;
376 -- Equivalent to Find (Container, Key) /= No_Element
378 procedure Update_Element_Preserving_Key
379 (Container : in out Set;
381 Process : not null access
382 procedure (Element : in out Element_Type));
383 -- Calls Process with the element of the node designated by Position,
384 -- but with the restriction that the key-value of the element is not
385 -- modified. The operation first makes a copy of the value returned by
386 -- applying generic formal operation Key on the element of the node, and
387 -- then calls Process with the element. The operation verifies that the
388 -- key-part has not been modified by calling generic formal operation
389 -- Equivalent_Keys to compare the saved key-value to the value returned
390 -- by applying generic formal operation Key to the post-Process value of
391 -- element. If the key values compare equal then the operation
392 -- completes. Otherwise, the node is removed from the map and
393 -- Program_Error is raised.
399 pragma Inline (Next);
402 type Node_Access is access Node_Type;
404 type Element_Access is access Element_Type;
408 Element : Element_Access;
412 package HT_Types is new Hash_Tables.Generic_Hash_Table_Types
416 type Set is new Ada.Finalization.Controlled with record
417 HT : HT_Types.Hash_Table_Type;
420 procedure Adjust (Container : in out Set);
422 procedure Finalize (Container : in out Set);
425 use Ada.Finalization;
428 type Set_Access is access all Set;
429 for Set_Access'Storage_Size use 0;
433 Container : Set_Access;
438 (Stream : not null access Root_Stream_Type'Class;
441 for Cursor'Write use Write;
444 (Stream : not null access Root_Stream_Type'Class;
447 for Cursor'Read use Read;
449 No_Element : constant Cursor :=
454 (Stream : not null access Root_Stream_Type'Class;
457 for Set'Write use Write;
460 (Stream : not null access Root_Stream_Type'Class;
461 Container : out Set);
463 for Set'Read use Read;
465 Empty_Set : constant Set := (Controlled with HT => (null, 0, 0, 0));
467 end Ada.Containers.Indefinite_Hashed_Sets;