1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
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   --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2014, 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------------------------------------------------------------------------------
33
34with Ada.Iterator_Interfaces;
35
36private with Ada.Containers.Hash_Tables;
37private with Ada.Streams;
38private with Ada.Finalization; use Ada.Finalization;
39
40generic
41   type Element_Type is private;
42
43   with function Hash (Element : Element_Type) return Hash_Type;
44
45   with function Equivalent_Elements
46          (Left, Right : Element_Type) return Boolean;
47
48   with function "=" (Left, Right : Element_Type) return Boolean is <>;
49
50package Ada.Containers.Bounded_Hashed_Sets is
51   pragma Pure;
52   pragma Remote_Types;
53
54   type Set (Capacity : Count_Type; Modulus : Hash_Type) is tagged private
55     with Constant_Indexing => Constant_Reference,
56          Default_Iterator  => Iterate,
57          Iterator_Element  => Element_Type;
58
59   pragma Preelaborable_Initialization (Set);
60
61   type Cursor is private;
62   pragma Preelaborable_Initialization (Cursor);
63
64   Empty_Set : constant Set;
65   --  Set objects declared without an initialization expression are
66   --  initialized to the value Empty_Set.
67
68   No_Element : constant Cursor;
69   --  Cursor objects declared without an initialization expression are
70   --  initialized to the value No_Element.
71
72   function Has_Element (Position : Cursor) return Boolean;
73   --  Equivalent to Position /= No_Element
74
75   package Set_Iterator_Interfaces is new
76     Ada.Iterator_Interfaces (Cursor, Has_Element);
77
78   function "=" (Left, Right : Set) return Boolean;
79   --  For each element in Left, set equality attempts to find the equal
80   --  element in Right; if a search fails, then set equality immediately
81   --  returns False. The search works by calling Hash to find the bucket in
82   --  the Right set that corresponds to the Left element. If the bucket is
83   --  non-empty, the search calls the generic formal element equality operator
84   --  to compare the element (in Left) to the element of each node in the
85   --  bucket (in Right); the search terminates when a matching node in the
86   --  bucket is found, or the nodes in the bucket are exhausted. (Note that
87   --  element equality is called here, not Equivalent_Elements. Set equality
88   --  is the only operation in which element equality is used. Compare set
89   --  equality to Equivalent_Sets, which does call Equivalent_Elements.)
90
91   function Equivalent_Sets (Left, Right : Set) return Boolean;
92   --  Similar to set equality, with the difference that the element in Left is
93   --  compared to the elements in Right using the generic formal
94   --  Equivalent_Elements operation instead of element equality.
95
96   function To_Set (New_Item : Element_Type) return Set;
97   --  Constructs a singleton set comprising New_Element. To_Set calls Hash to
98   --  determine the bucket for New_Item.
99
100   function Capacity (Container : Set) return Count_Type;
101   --  Returns the current capacity of the set. Capacity is the maximum length
102   --  before which rehashing in guaranteed not to occur.
103
104   procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
105   --  If the value of the Capacity actual parameter is less or equal to
106   --  Container.Capacity, then the operation has no effect.  Otherwise it
107   --  raises Capacity_Error (as no expansion of capacity is possible for a
108   --  bounded form).
109
110   function Default_Modulus (Capacity : Count_Type) return Hash_Type;
111   --  Returns a modulus value (hash table size) which is optimal for the
112   --  specified capacity (which corresponds to the maximum number of items).
113
114   function Length (Container : Set) return Count_Type;
115   --  Returns the number of items in the set
116
117   function Is_Empty (Container : Set) return Boolean;
118   --  Equivalent to Length (Container) = 0
119
120   procedure Clear (Container : in out Set);
121   --  Removes all of the items from the set
122
123   function Element (Position : Cursor) return Element_Type;
124   --  Returns the element of the node designated by the cursor
125
126   procedure Replace_Element
127     (Container : in out Set;
128      Position  : Cursor;
129      New_Item  : Element_Type);
130   --  If New_Item is equivalent (as determined by calling Equivalent_Elements)
131   --  to the element of the node designated by Position, then New_Element is
132   --  assigned to that element. Otherwise, it calls Hash to determine the
133   --  bucket for New_Item. If the bucket is not empty, then it calls
134   --  Equivalent_Elements for each node in that bucket to determine whether
135   --  New_Item is equivalent to an element in that bucket. If
136   --  Equivalent_Elements returns True then Program_Error is raised (because
137   --  an element may appear only once in the set); otherwise, New_Item is
138   --  assigned to the node designated by Position, and the node is moved to
139   --  its new bucket.
140
141   procedure Query_Element
142     (Position : Cursor;
143      Process  : not null access procedure (Element : Element_Type));
144   --  Calls Process with the element (having only a constant view) of the node
145   --  designated by the cursor.
146
147   type Constant_Reference_Type
148     (Element : not null access constant Element_Type) is private
149        with Implicit_Dereference => Element;
150
151   function Constant_Reference
152     (Container : aliased Set;
153      Position  : Cursor) return Constant_Reference_Type;
154
155   procedure Assign (Target : in out Set; Source : Set);
156   --  If Target denotes the same object as Source, then the operation has no
157   --  effect. If the Target capacity is less than the Source length, then
158   --  Assign raises Capacity_Error.  Otherwise, Assign clears Target and then
159   --  copies the (active) elements from Source to Target.
160
161   function Copy
162     (Source   : Set;
163      Capacity : Count_Type := 0;
164      Modulus  : Hash_Type := 0) return Set;
165   --  Constructs a new set object whose elements correspond to Source.  If the
166   --  Capacity parameter is 0, then the capacity of the result is the same as
167   --  the length of Source. If the Capacity parameter is equal or greater than
168   --  the length of Source, then the capacity of the result is the specified
169   --  value. Otherwise, Copy raises Capacity_Error. If the Modulus parameter
170   --  is 0, then the modulus of the result is the value returned by a call to
171   --  Default_Modulus with the capacity parameter determined as above;
172   --  otherwise the modulus of the result is the specified value.
173
174   procedure Move (Target : in out Set; Source : in out Set);
175   --  Clears Target (if it's not empty), and then moves (not copies) the
176   --  buckets array and nodes from Source to Target.
177
178   procedure Insert
179     (Container : in out Set;
180      New_Item  : Element_Type;
181      Position  : out Cursor;
182      Inserted  : out Boolean);
183   --  Conditionally inserts New_Item into the set. If New_Item is already in
184   --  the set, then Inserted returns False and Position designates the node
185   --  containing the existing element (which is not modified). If New_Item is
186   --  not already in the set, then Inserted returns True and Position
187   --  designates the newly-inserted node containing New_Item. The search for
188   --  an existing element works as follows. Hash is called to determine
189   --  New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
190   --  is called to compare New_Item to the element of each node in that
191   --  bucket. If the bucket is empty, or there were no equivalent elements in
192   --  the bucket, the search "fails" and the New_Item is inserted in the set
193   --  (and Inserted returns True); otherwise, the search "succeeds" (and
194   --  Inserted returns False).
195
196   procedure Insert  (Container : in out Set; New_Item : Element_Type);
197   --  Attempts to insert New_Item into the set, performing the usual insertion
198   --  search (which involves calling both Hash and Equivalent_Elements); if
199   --  the search succeeds (New_Item is equivalent to an element already in the
200   --  set, and so was not inserted), then this operation raises
201   --  Constraint_Error. (This version of Insert is similar to Replace, but
202   --  having the opposite exception behavior. It is intended for use when you
203   --  want to assert that the item is not already in the set.)
204
205   procedure Include (Container : in out Set; New_Item : Element_Type);
206   --  Attempts to insert New_Item into the set. If an element equivalent to
207   --  New_Item is already in the set (the insertion search succeeded, and
208   --  hence New_Item was not inserted), then the value of New_Item is assigned
209   --  to the existing element. (This insertion operation only raises an
210   --  exception if cursor tampering occurs. It is intended for use when you
211   --  want to insert the item in the set, and you don't care whether an
212   --  equivalent element is already present.)
213
214   procedure Replace (Container : in out Set; New_Item : Element_Type);
215   --  Searches for New_Item in the set; if the search fails (because an
216   --  equivalent element was not in the set), then it raises
217   --  Constraint_Error. Otherwise, the existing element is assigned the value
218   --  New_Item. (This is similar to Insert, but with the opposite exception
219   --  behavior. It is intended for use when you want to assert that the item
220   --  is already in the set.)
221
222   procedure Exclude (Container : in out Set; Item : Element_Type);
223   --  Searches for Item in the set, and if found, removes its node from the
224   --  set and then deallocates it. The search works as follows. The operation
225   --  calls Hash to determine the item's bucket; if the bucket is not empty,
226   --  it calls Equivalent_Elements to compare Item to the element of each node
227   --  in the bucket. (This is the deletion analog of Include. It is intended
228   --  for use when you want to remove the item from the set, but don't care
229   --  whether the item is already in the set.)
230
231   procedure Delete  (Container : in out Set; Item : Element_Type);
232   --  Searches for Item in the set (which involves calling both Hash and
233   --  Equivalent_Elements). If the search fails, then the operation raises
234   --  Constraint_Error. Otherwise it removes the node from the set and then
235   --  deallocates it. (This is the deletion analog of non-conditional
236   --  Insert. It is intended for use when you want to assert that the item is
237   --  already in the set.)
238
239   procedure Delete (Container : in out Set; Position : in out Cursor);
240   --  Removes the node designated by Position from the set, and then
241   --  deallocates the node. The operation calls Hash to determine the bucket,
242   --  and then compares Position to each node in the bucket until there's a
243   --  match (it does not call Equivalent_Elements).
244
245   procedure Union (Target : in out Set; Source : Set);
246   --  Iterates over the Source set, and conditionally inserts each element
247   --  into Target.
248
249   function Union (Left, Right : Set) return Set;
250   --  The operation first copies the Left set to the result, and then iterates
251   --  over the Right set to conditionally insert each element into the result.
252
253   function "or" (Left, Right : Set) return Set renames Union;
254
255   procedure Intersection (Target : in out Set; Source : Set);
256   --  Iterates over the Target set (calling First and Next), calling Find to
257   --  determine whether the element is in Source. If an equivalent element is
258   --  not found in Source, the element is deleted from Target.
259
260   function Intersection (Left, Right : Set) return Set;
261   --  Iterates over the Left set, calling Find to determine whether the
262   --  element is in Right. If an equivalent element is found, it is inserted
263   --  into the result set.
264
265   function "and" (Left, Right : Set) return Set renames Intersection;
266
267   procedure Difference (Target : in out Set; Source : Set);
268   --  Iterates over the Source (calling First and Next), calling Find to
269   --  determine whether the element is in Target. If an equivalent element is
270   --  found, it is deleted from Target.
271
272   function Difference (Left, Right : Set) return Set;
273   --  Iterates over the Left set, calling Find to determine whether the
274   --  element is in the Right set. If an equivalent element is not found, the
275   --  element is inserted into the result set.
276
277   function "-" (Left, Right : Set) return Set renames Difference;
278
279   procedure Symmetric_Difference (Target : in out Set; Source : Set);
280   --  The operation iterates over the Source set, searching for the element
281   --  in Target (calling Hash and Equivalent_Elements). If an equivalent
282   --  element is found, it is removed from Target; otherwise it is inserted
283   --  into Target.
284
285   function Symmetric_Difference (Left, Right : Set) return Set;
286   --  The operation first iterates over the Left set. It calls Find to
287   --  determine whether the element is in the Right set. If no equivalent
288   --  element is found, the element from Left is inserted into the result. The
289   --  operation then iterates over the Right set, to determine whether the
290   --  element is in the Left set. If no equivalent element is found, the Right
291   --  element is inserted into the result.
292
293   function "xor" (Left, Right : Set) return Set
294     renames Symmetric_Difference;
295
296   function Overlap (Left, Right : Set) return Boolean;
297   --  Iterates over the Left set (calling First and Next), calling Find to
298   --  determine whether the element is in the Right set. If an equivalent
299   --  element is found, the operation immediately returns True. The operation
300   --  returns False if the iteration over Left terminates without finding any
301   --  equivalent element in Right.
302
303   function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
304   --  Iterates over Subset (calling First and Next), calling Find to determine
305   --  whether the element is in Of_Set. If no equivalent element is found in
306   --  Of_Set, the operation immediately returns False. The operation returns
307   --  True if the iteration over Subset terminates without finding an element
308   --  not in Of_Set (that is, every element in Subset is equivalent to an
309   --  element in Of_Set).
310
311   function First (Container : Set) return Cursor;
312   --  Returns a cursor that designates the first non-empty bucket, by
313   --  searching from the beginning of the buckets array.
314
315   function Next (Position : Cursor) return Cursor;
316   --  Returns a cursor that designates the node that follows the current one
317   --  designated by Position. If Position designates the last node in its
318   --  bucket, the operation calls Hash to compute the index of this bucket,
319   --  and searches the buckets array for the first non-empty bucket, starting
320   --  from that index; otherwise, it simply follows the link to the next node
321   --  in the same bucket.
322
323   procedure Next (Position : in out Cursor);
324   --  Equivalent to Position := Next (Position)
325
326   function Find
327     (Container : Set;
328      Item      : Element_Type) return Cursor;
329   --  Searches for Item in the set. Find calls Hash to determine the item's
330   --  bucket; if the bucket is not empty, it calls Equivalent_Elements to
331   --  compare Item to each element in the bucket. If the search succeeds, Find
332   --  returns a cursor designating the node containing the equivalent element;
333   --  otherwise, it returns No_Element.
334
335   function Contains (Container : Set; Item : Element_Type) return Boolean;
336   --  Equivalent to Find (Container, Item) /= No_Element
337
338   function Equivalent_Elements (Left, Right : Cursor) return Boolean;
339   --  Returns the result of calling Equivalent_Elements with the elements of
340   --  the nodes designated by cursors Left and Right.
341
342   function Equivalent_Elements
343     (Left  : Cursor;
344      Right : Element_Type) return Boolean;
345   --  Returns the result of calling Equivalent_Elements with element of the
346   --  node designated by Left and element Right.
347
348   function Equivalent_Elements
349     (Left  : Element_Type;
350      Right : Cursor) return Boolean;
351   --  Returns the result of calling Equivalent_Elements with element Left and
352   --  the element of the node designated by Right.
353
354   procedure Iterate
355     (Container : Set;
356      Process   : not null access procedure (Position : Cursor));
357   --  Calls Process for each node in the set
358
359   function Iterate
360     (Container : Set)
361      return Set_Iterator_Interfaces.Forward_Iterator'Class;
362
363   generic
364      type Key_Type (<>) is private;
365
366      with function Key (Element : Element_Type) return Key_Type;
367
368      with function Hash (Key : Key_Type) return Hash_Type;
369
370      with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
371
372   package Generic_Keys is
373
374      function Key (Position : Cursor) return Key_Type;
375      --  Applies generic formal operation Key to the element of the node
376      --  designated by Position.
377
378      function Element (Container : Set; Key : Key_Type) return Element_Type;
379      --  Searches (as per the key-based Find) for the node containing Key, and
380      --  returns the associated element.
381
382      procedure Replace
383        (Container : in out Set;
384         Key       : Key_Type;
385         New_Item  : Element_Type);
386      --  Searches (as per the key-based Find) for the node containing Key, and
387      --  then replaces the element of that node (as per the element-based
388      --  Replace_Element).
389
390      procedure Exclude (Container : in out Set; Key : Key_Type);
391      --  Searches for Key in the set, and if found, removes its node from the
392      --  set and then deallocates it. The search works by first calling Hash
393      --  (on Key) to determine the bucket; if the bucket is not empty, it
394      --  calls Equivalent_Keys to compare parameter Key to the value of
395      --  generic formal operation Key applied to element of each node in the
396      --  bucket.
397
398      procedure Delete (Container : in out Set; Key : Key_Type);
399      --  Deletes the node containing Key as per Exclude, with the difference
400      --  that Constraint_Error is raised if Key is not found.
401
402      function Find (Container : Set; Key : Key_Type) return Cursor;
403      --  Searches for the node containing Key, and returns a cursor
404      --  designating the node. The search works by first calling Hash (on Key)
405      --  to determine the bucket. If the bucket is not empty, the search
406      --  compares Key to the element of each node in the bucket, and returns
407      --  the matching node. The comparison itself works by applying the
408      --  generic formal Key operation to the element of the node, and then
409      --  calling generic formal operation Equivalent_Keys.
410
411      function Contains (Container : Set; Key : Key_Type) return Boolean;
412      --  Equivalent to Find (Container, Key) /= No_Element
413
414      procedure Update_Element_Preserving_Key
415        (Container : in out Set;
416         Position  : Cursor;
417         Process   : not null access
418                       procedure (Element : in out Element_Type));
419      --  Calls Process with the element of the node designated by Position,
420      --  but with the restriction that the key-value of the element is not
421      --  modified. The operation first makes a copy of the value returned by
422      --  applying generic formal operation Key on the element of the node, and
423      --  then calls Process with the element. The operation verifies that the
424      --  key-part has not been modified by calling generic formal operation
425      --  Equivalent_Keys to compare the saved key-value to the value returned
426      --  by applying generic formal operation Key to the post-Process value of
427      --  element. If the key values compare equal then the operation
428      --  completes. Otherwise, the node is removed from the map and
429      --  Program_Error is raised.
430
431      type Reference_Type (Element : not null access Element_Type) is private
432        with Implicit_Dereference => Element;
433
434      function Reference_Preserving_Key
435        (Container : aliased in out Set;
436         Position  : Cursor) return Reference_Type;
437
438      function Constant_Reference
439        (Container : aliased Set;
440         Key       : Key_Type) return Constant_Reference_Type;
441
442      function Reference_Preserving_Key
443        (Container : aliased in out Set;
444         Key       : Key_Type) return Reference_Type;
445
446   private
447      type Set_Access is access all Set;
448      for Set_Access'Storage_Size use 0;
449
450      type Reference_Control_Type is
451         new Ada.Finalization.Controlled with
452      record
453         Container : Set_Access;
454         Index     : Hash_Type;
455         Old_Pos   : Cursor;
456         Old_Hash  : Hash_Type;
457      end record;
458
459      overriding procedure Adjust (Control : in out Reference_Control_Type);
460      pragma Inline (Adjust);
461
462      overriding procedure Finalize (Control : in out Reference_Control_Type);
463      pragma Inline (Finalize);
464
465      type Reference_Type (Element : not null access Element_Type) is record
466         Control  : Reference_Control_Type;
467      end record;
468
469      use Ada.Streams;
470
471      procedure Read
472        (Stream : not null access Root_Stream_Type'Class;
473         Item   : out Reference_Type);
474
475      for Reference_Type'Read use Read;
476
477      procedure Write
478        (Stream : not null access Root_Stream_Type'Class;
479         Item   : Reference_Type);
480
481      for Reference_Type'Write use Write;
482
483   end Generic_Keys;
484
485private
486   pragma Inline (Next);
487
488   type Node_Type is record
489      Element : aliased Element_Type;
490      Next    : Count_Type;
491   end record;
492
493   package HT_Types is
494     new Hash_Tables.Generic_Bounded_Hash_Table_Types (Node_Type);
495
496   type Set (Capacity : Count_Type; Modulus : Hash_Type) is
497     new HT_Types.Hash_Table_Type (Capacity, Modulus) with null record;
498
499   use HT_Types;
500   use Ada.Streams;
501
502   procedure Write
503     (Stream    : not null access Root_Stream_Type'Class;
504      Container : Set);
505
506   for Set'Write use Write;
507
508   procedure Read
509     (Stream    : not null access Root_Stream_Type'Class;
510      Container : out Set);
511
512   for Set'Read use Read;
513
514   type Set_Access is access all Set;
515   for Set_Access'Storage_Size use 0;
516
517   --  Note: If a Cursor object has no explicit initialization expression,
518   --  it must default initialize to the same value as constant No_Element.
519   --  The Node component of type Cursor has scalar type Count_Type, so it
520   --  requires an explicit initialization expression of its own declaration,
521   --  in order for objects of record type Cursor to properly initialize.
522
523   type Cursor is record
524      Container : Set_Access;
525      Node      : Count_Type := 0;
526   end record;
527
528   procedure Write
529     (Stream : not null access Root_Stream_Type'Class;
530      Item   : Cursor);
531
532   for Cursor'Write use Write;
533
534   procedure Read
535     (Stream : not null access Root_Stream_Type'Class;
536      Item   : out Cursor);
537
538   for Cursor'Read use Read;
539
540   type Reference_Control_Type is new Controlled with record
541      Container : Set_Access;
542   end record;
543
544   overriding procedure Adjust (Control : in out Reference_Control_Type);
545   pragma Inline (Adjust);
546
547   overriding procedure Finalize (Control : in out Reference_Control_Type);
548   pragma Inline (Finalize);
549
550   type Constant_Reference_Type
551     (Element : not null access constant Element_Type) is
552   record
553      Control : Reference_Control_Type;
554   end record;
555
556   procedure Read
557     (Stream : not null access Root_Stream_Type'Class;
558      Item   : out Constant_Reference_Type);
559
560   for Constant_Reference_Type'Read use Read;
561
562   procedure Write
563     (Stream : not null access Root_Stream_Type'Class;
564      Item   : Constant_Reference_Type);
565
566   for Constant_Reference_Type'Write use Write;
567
568   Empty_Set : constant Set :=
569                 (Hash_Table_Type with Capacity => 0, Modulus => 0);
570
571   No_Element : constant Cursor := (Container => null, Node => 0);
572
573   type Iterator is new Limited_Controlled and
574     Set_Iterator_Interfaces.Forward_Iterator with
575   record
576      Container : Set_Access;
577   end record;
578
579   overriding procedure Finalize (Object : in out Iterator);
580
581   overriding function First (Object : Iterator) return Cursor;
582
583   overriding function Next
584     (Object   : Iterator;
585      Position : Cursor) return Cursor;
586
587end Ada.Containers.Bounded_Hashed_Sets;
588