1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ H A S H E D _ S E T S --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
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. --
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. --
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. --
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/>. --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada.Iterator_Interfaces;
35 private with Ada.Containers.Hash_Tables;
36 private with Ada.Streams;
39 type Element_Type is private;
41 with function Hash (Element : Element_Type) return Hash_Type;
43 with function Equivalent_Elements
44 (Left, Right : Element_Type) return Boolean;
46 with function "=" (Left, Right : Element_Type) return Boolean is <>;
48 package Ada.Containers.Bounded_Hashed_Sets is
52 type Set (Capacity : Count_Type; Modulus : Hash_Type) is tagged private
53 with Constant_Indexing => Constant_Reference,
54 Default_Iterator => Iterate,
55 Iterator_Element => Element_Type;
57 pragma Preelaborable_Initialization (Set);
59 type Cursor is private;
60 pragma Preelaborable_Initialization (Cursor);
62 Empty_Set : constant Set;
63 -- Set objects declared without an initialization expression are
64 -- initialized to the value Empty_Set.
66 No_Element : constant Cursor;
67 -- Cursor objects declared without an initialization expression are
68 -- initialized to the value No_Element.
70 function Has_Element (Position : Cursor) return Boolean;
71 -- Equivalent to Position /= No_Element
73 package Set_Iterator_Interfaces is new
74 Ada.Iterator_Interfaces (Cursor, Has_Element);
76 function "=" (Left, Right : Set) return Boolean;
77 -- For each element in Left, set equality attempts to find the equal
78 -- element in Right; if a search fails, then set equality immediately
79 -- returns False. The search works by calling Hash to find the bucket in
80 -- the Right set that corresponds to the Left element. If the bucket is
81 -- non-empty, the search calls the generic formal element equality operator
82 -- to compare the element (in Left) to the element of each node in the
83 -- bucket (in Right); the search terminates when a matching node in the
84 -- bucket is found, or the nodes in the bucket are exhausted. (Note that
85 -- element equality is called here, not Equivalent_Elements. Set equality
86 -- is the only operation in which element equality is used. Compare set
87 -- equality to Equivalent_Sets, which does call Equivalent_Elements.)
89 function Equivalent_Sets (Left, Right : Set) return Boolean;
90 -- Similar to set equality, with the difference that the element in Left is
91 -- compared to the elements in Right using the generic formal
92 -- Equivalent_Elements operation instead of element equality.
94 function To_Set (New_Item : Element_Type) return Set;
95 -- Constructs a singleton set comprising New_Element. To_Set calls Hash to
96 -- determine the bucket for New_Item.
98 function Capacity (Container : Set) return Count_Type;
99 -- Returns the current capacity of the set. Capacity is the maximum length
100 -- before which rehashing in guaranteed not to occur.
102 procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
103 -- If the value of the Capacity actual parameter is less or equal to
104 -- Container.Capacity, then the operation has no effect. Otherwise it
105 -- raises Capacity_Error (as no expansion of capacity is possible for a
108 function Default_Modulus (Capacity : Count_Type) return Hash_Type;
109 -- Returns a modulus value (hash table size) which is optimal for the
110 -- specified capacity (which corresponds to the maximum number of items).
112 function Length (Container : Set) return Count_Type;
113 -- Returns the number of items in the set
115 function Is_Empty (Container : Set) return Boolean;
116 -- Equivalent to Length (Container) = 0
118 procedure Clear (Container : in out Set);
119 -- Removes all of the items from the set
121 function Element (Position : Cursor) return Element_Type;
122 -- Returns the element of the node designated by the cursor
124 procedure Replace_Element
125 (Container : in out Set;
127 New_Item : Element_Type);
128 -- If New_Item is equivalent (as determined by calling Equivalent_Elements)
129 -- to the element of the node designated by Position, then New_Element is
130 -- assigned to that element. Otherwise, it calls Hash to determine the
131 -- bucket for New_Item. If the bucket is not empty, then it calls
132 -- Equivalent_Elements for each node in that bucket to determine whether
133 -- New_Item is equivalent to an element in that bucket. If
134 -- Equivalent_Elements returns True then Program_Error is raised (because
135 -- an element may appear only once in the set); otherwise, New_Item is
136 -- assigned to the node designated by Position, and the node is moved to
139 procedure Query_Element
141 Process : not null access procedure (Element : Element_Type));
142 -- Calls Process with the element (having only a constant view) of the node
143 -- designated by the cursor.
145 type Constant_Reference_Type
146 (Element : not null access constant Element_Type) is private
147 with Implicit_Dereference => Element;
149 function Constant_Reference
150 (Container : aliased Set;
151 Position : Cursor) return Constant_Reference_Type;
153 procedure Assign (Target : in out Set; Source : Set);
154 -- If Target denotes the same object as Source, then the operation has no
155 -- effect. If the Target capacity is less then the Source length, then
156 -- Assign raises Capacity_Error. Otherwise, Assign clears Target and then
157 -- copies the (active) elements from Source to Target.
161 Capacity : Count_Type := 0;
162 Modulus : Hash_Type := 0) return Set;
163 -- Constructs a new set object whose elements correspond to Source. If the
164 -- Capacity parameter is 0, then the capacity of the result is the same as
165 -- the length of Source. If the Capacity parameter is equal or greater than
166 -- the length of Source, then the capacity of the result is the specified
167 -- value. Otherwise, Copy raises Capacity_Error. If the Modulus parameter
168 -- is 0, then the modulus of the result is the value returned by a call to
169 -- Default_Modulus with the capacity parameter determined as above;
170 -- otherwise the modulus of the result is the specified value.
172 procedure Move (Target : in out Set; Source : in out Set);
173 -- Clears Target (if it's not empty), and then moves (not copies) the
174 -- buckets array and nodes from Source to Target.
177 (Container : in out Set;
178 New_Item : Element_Type;
179 Position : out Cursor;
180 Inserted : out Boolean);
181 -- Conditionally inserts New_Item into the set. If New_Item is already in
182 -- the set, then Inserted returns False and Position designates the node
183 -- containing the existing element (which is not modified). If New_Item is
184 -- not already in the set, then Inserted returns True and Position
185 -- designates the newly-inserted node containing New_Item. The search for
186 -- an existing element works as follows. Hash is called to determine
187 -- New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
188 -- is called to compare New_Item to the element of each node in that
189 -- bucket. If the bucket is empty, or there were no equivalent elements in
190 -- the bucket, the search "fails" and the New_Item is inserted in the set
191 -- (and Inserted returns True); otherwise, the search "succeeds" (and
192 -- Inserted returns False).
194 procedure Insert (Container : in out Set; New_Item : Element_Type);
195 -- Attempts to insert New_Item into the set, performing the usual insertion
196 -- search (which involves calling both Hash and Equivalent_Elements); if
197 -- the search succeeds (New_Item is equivalent to an element already in the
198 -- set, and so was not inserted), then this operation raises
199 -- Constraint_Error. (This version of Insert is similar to Replace, but
200 -- having the opposite exception behavior. It is intended for use when you
201 -- want to assert that the item is not already in the set.)
203 procedure Include (Container : in out Set; New_Item : Element_Type);
204 -- Attempts to insert New_Item into the set. If an element equivalent to
205 -- New_Item is already in the set (the insertion search succeeded, and
206 -- hence New_Item was not inserted), then the value of New_Item is assigned
207 -- to the existing element. (This insertion operation only raises an
208 -- exception if cursor tampering occurs. It is intended for use when you
209 -- want to insert the item in the set, and you don't care whether an
210 -- equivalent element is already present.)
212 procedure Replace (Container : in out Set; New_Item : Element_Type);
213 -- Searches for New_Item in the set; if the search fails (because an
214 -- equivalent element was not in the set), then it raises
215 -- Constraint_Error. Otherwise, the existing element is assigned the value
216 -- New_Item. (This is similar to Insert, but with the opposite exception
217 -- behavior. It is intended for use when you want to assert that the item
218 -- is already in the set.)
220 procedure Exclude (Container : in out Set; Item : Element_Type);
221 -- Searches for Item in the set, and if found, removes its node from the
222 -- set and then deallocates it. The search works as follows. The operation
223 -- calls Hash to determine the item's bucket; if the bucket is not empty,
224 -- it calls Equivalent_Elements to compare Item to the element of each node
225 -- in the bucket. (This is the deletion analog of Include. It is intended
226 -- for use when you want to remove the item from the set, but don't care
227 -- whether the item is already in the set.)
229 procedure Delete (Container : in out Set; Item : Element_Type);
230 -- Searches for Item in the set (which involves calling both Hash and
231 -- Equivalent_Elements). If the search fails, then the operation raises
232 -- Constraint_Error. Otherwise it removes the node from the set and then
233 -- deallocates it. (This is the deletion analog of non-conditional
234 -- Insert. It is intended for use when you want to assert that the item is
235 -- already in the set.)
237 procedure Delete (Container : in out Set; Position : in out Cursor);
238 -- Removes the node designated by Position from the set, and then
239 -- deallocates the node. The operation calls Hash to determine the bucket,
240 -- and then compares Position to each node in the bucket until there's a
241 -- match (it does not call Equivalent_Elements).
243 procedure Union (Target : in out Set; Source : Set);
244 -- Iterates over the Source set, and conditionally inserts each element
247 function Union (Left, Right : Set) return Set;
248 -- The operation first copies the Left set to the result, and then iterates
249 -- over the Right set to conditionally insert each element into the result.
251 function "or" (Left, Right : Set) return Set renames Union;
253 procedure Intersection (Target : in out Set; Source : Set);
254 -- Iterates over the Target set (calling First and Next), calling Find to
255 -- determine whether the element is in Source. If an equivalent element is
256 -- not found in Source, the element is deleted from Target.
258 function Intersection (Left, Right : Set) return Set;
259 -- Iterates over the Left set, calling Find to determine whether the
260 -- element is in Right. If an equivalent element is found, it is inserted
261 -- into the result set.
263 function "and" (Left, Right : Set) return Set renames Intersection;
265 procedure Difference (Target : in out Set; Source : Set);
266 -- Iterates over the Source (calling First and Next), calling Find to
267 -- determine whether the element is in Target. If an equivalent element is
268 -- found, it is deleted from Target.
270 function Difference (Left, Right : Set) return Set;
271 -- Iterates over the Left set, calling Find to determine whether the
272 -- element is in the Right set. If an equivalent element is not found, the
273 -- element is inserted into the result set.
275 function "-" (Left, Right : Set) return Set renames Difference;
277 procedure Symmetric_Difference (Target : in out Set; Source : Set);
278 -- The operation iterates over the Source set, searching for the element
279 -- in Target (calling Hash and Equivalent_Elements). If an equivalent
280 -- element is found, it is removed from Target; otherwise it is inserted
283 function Symmetric_Difference (Left, Right : Set) return Set;
284 -- The operation first iterates over the Left set. It calls Find to
285 -- determine whether the element is in the Right set. If no equivalent
286 -- element is found, the element from Left is inserted into the result. The
287 -- operation then iterates over the Right set, to determine whether the
288 -- element is in the Left set. If no equivalent element is found, the Right
289 -- element is inserted into the result.
291 function "xor" (Left, Right : Set) return Set
292 renames Symmetric_Difference;
294 function Overlap (Left, Right : Set) return Boolean;
295 -- Iterates over the Left set (calling First and Next), calling Find to
296 -- determine whether the element is in the Right set. If an equivalent
297 -- element is found, the operation immediately returns True. The operation
298 -- returns False if the iteration over Left terminates without finding any
299 -- equivalent element in Right.
301 function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
302 -- Iterates over Subset (calling First and Next), calling Find to determine
303 -- whether the element is in Of_Set. If no equivalent element is found in
304 -- Of_Set, the operation immediately returns False. The operation returns
305 -- True if the iteration over Subset terminates without finding an element
306 -- not in Of_Set (that is, every element in Subset is equivalent to an
307 -- element in Of_Set).
309 function First (Container : Set) return Cursor;
310 -- Returns a cursor that designates the first non-empty bucket, by
311 -- searching from the beginning of the buckets array.
313 function Next (Position : Cursor) return Cursor;
314 -- Returns a cursor that designates the node that follows the current one
315 -- designated by Position. If Position designates the last node in its
316 -- bucket, the operation calls Hash to compute the index of this bucket,
317 -- and searches the buckets array for the first non-empty bucket, starting
318 -- from that index; otherwise, it simply follows the link to the next node
319 -- in the same bucket.
321 procedure Next (Position : in out Cursor);
322 -- Equivalent to Position := Next (Position)
326 Item : Element_Type) return Cursor;
327 -- Searches for Item in the set. Find calls Hash to determine the item's
328 -- bucket; if the bucket is not empty, it calls Equivalent_Elements to
329 -- compare Item to each element in the bucket. If the search succeeds, Find
330 -- returns a cursor designating the node containing the equivalent element;
331 -- otherwise, it returns No_Element.
333 function Contains (Container : Set; Item : Element_Type) return Boolean;
334 -- Equivalent to Find (Container, Item) /= No_Element
336 function Equivalent_Elements (Left, Right : Cursor) return Boolean;
337 -- Returns the result of calling Equivalent_Elements with the elements of
338 -- the nodes designated by cursors Left and Right.
340 function Equivalent_Elements
342 Right : Element_Type) return Boolean;
343 -- Returns the result of calling Equivalent_Elements with element of the
344 -- node designated by Left and element Right.
346 function Equivalent_Elements
347 (Left : Element_Type;
348 Right : Cursor) return Boolean;
349 -- Returns the result of calling Equivalent_Elements with element Left and
350 -- the element of the node designated by Right.
354 Process : not null access procedure (Position : Cursor));
355 -- Calls Process for each node in the set
359 return Set_Iterator_Interfaces.Forward_Iterator'Class;
362 type Key_Type (<>) is private;
364 with function Key (Element : Element_Type) return Key_Type;
366 with function Hash (Key : Key_Type) return Hash_Type;
368 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
370 package Generic_Keys is
372 function Key (Position : Cursor) return Key_Type;
373 -- Applies generic formal operation Key to the element of the node
374 -- designated by Position.
376 function Element (Container : Set; Key : Key_Type) return Element_Type;
377 -- Searches (as per the key-based Find) for the node containing Key, and
378 -- returns the associated element.
381 (Container : in out Set;
383 New_Item : Element_Type);
384 -- Searches (as per the key-based Find) for the node containing Key, and
385 -- then replaces the element of that node (as per the element-based
388 procedure Exclude (Container : in out Set; Key : Key_Type);
389 -- Searches for Key in the set, and if found, removes its node from the
390 -- set and then deallocates it. The search works by first calling Hash
391 -- (on Key) to determine the bucket; if the bucket is not empty, it
392 -- calls Equivalent_Keys to compare parameter Key to the value of
393 -- generic formal operation Key applied to element of each node in the
396 procedure Delete (Container : in out Set; Key : Key_Type);
397 -- Deletes the node containing Key as per Exclude, with the difference
398 -- that Constraint_Error is raised if Key is not found.
400 function Find (Container : Set; Key : Key_Type) return Cursor;
401 -- Searches for the node containing Key, and returns a cursor
402 -- designating the node. The search works by first calling Hash (on Key)
403 -- to determine the bucket. If the bucket is not empty, the search
404 -- compares Key to the element of each node in the bucket, and returns
405 -- the matching node. The comparison itself works by applying the
406 -- generic formal Key operation to the element of the node, and then
407 -- calling generic formal operation Equivalent_Keys.
409 function Contains (Container : Set; Key : Key_Type) return Boolean;
410 -- Equivalent to Find (Container, Key) /= No_Element
412 procedure Update_Element_Preserving_Key
413 (Container : in out Set;
415 Process : not null access
416 procedure (Element : in out Element_Type));
417 -- Calls Process with the element of the node designated by Position,
418 -- but with the restriction that the key-value of the element is not
419 -- modified. The operation first makes a copy of the value returned by
420 -- applying generic formal operation Key on the element of the node, and
421 -- then calls Process with the element. The operation verifies that the
422 -- key-part has not been modified by calling generic formal operation
423 -- Equivalent_Keys to compare the saved key-value to the value returned
424 -- by applying generic formal operation Key to the post-Process value of
425 -- element. If the key values compare equal then the operation
426 -- completes. Otherwise, the node is removed from the map and
427 -- Program_Error is raised.
429 type Reference_Type (Element : not null access Element_Type) is private
430 with Implicit_Dereference => Element;
432 function Reference_Preserving_Key
433 (Container : aliased in out Set;
434 Position : Cursor) return Reference_Type;
436 function Reference_Preserving_Key
437 (Container : aliased in out Set;
438 Key : Key_Type) return Reference_Type;
441 type Reference_Type (Element : not null access Element_Type)
447 pragma Inline (Next);
449 type Node_Type is record
450 Element : Element_Type;
455 new Hash_Tables.Generic_Bounded_Hash_Table_Types (Node_Type);
457 type Set (Capacity : Count_Type; Modulus : Hash_Type) is
458 new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
463 type Set_Access is access all Set;
464 for Set_Access'Storage_Size use 0;
466 -- Note: If a Cursor object has no explicit initialization expression,
467 -- it must default initialize to the same value as constant No_Element.
468 -- The Node component of type Cursor has scalar type Count_Type, so it
469 -- requires an explicit initialization expression of its own declaration,
470 -- in order for objects of record type Cursor to properly initialize.
472 type Cursor is record
473 Container : Set_Access;
474 Node : Count_Type := 0;
478 (Stream : not null access Root_Stream_Type'Class;
481 for Cursor'Write use Write;
484 (Stream : not null access Root_Stream_Type'Class;
487 for Cursor'Read use Read;
489 No_Element : constant Cursor := (Container => null, Node => 0);
492 (Stream : not null access Root_Stream_Type'Class;
495 for Set'Write use Write;
498 (Stream : not null access Root_Stream_Type'Class;
499 Container : out Set);
501 for Set'Read use Read;
503 type Constant_Reference_Type
504 (Element : not null access constant Element_Type) is null record;
507 (Stream : not null access Root_Stream_Type'Class;
508 Item : out Constant_Reference_Type);
510 for Constant_Reference_Type'Read use Read;
513 (Stream : not null access Root_Stream_Type'Class;
514 Item : Constant_Reference_Type);
516 for Constant_Reference_Type'Write use Write;
518 Empty_Set : constant Set :=
519 (Hash_Table_Type with Capacity => 0, Modulus => 0);
521 end Ada.Containers.Bounded_Hashed_Sets;