OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cihase.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT LIBRARY COMPONENTS                          --
4 --                                                                          --
5 --                  ADA.CONTAINERS.INDEFINITE_HASHED_SETS                   --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2008, 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 2,  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.  See the GNU General Public License --
21 -- for  more details.  You should have  received  a copy of the GNU General --
22 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
23 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
24 -- Boston, MA 02110-1301, USA.                                              --
25 --                                                                          --
26 -- As a special exception,  if other files  instantiate  generics from this --
27 -- unit, or you link  this unit with other files  to produce an executable, --
28 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
29 -- covered  by the  GNU  General  Public  License.  This exception does not --
30 -- however invalidate  any other reasons why  the executable file  might be --
31 -- covered by the  GNU Public License.                                      --
32 --                                                                          --
33 -- This unit was originally developed by Matthew J Heaney.                  --
34 ------------------------------------------------------------------------------
35
36 private with Ada.Containers.Hash_Tables;
37 private with Ada.Streams;
38 private with Ada.Finalization;
39
40 generic
41    type Element_Type (<>) is private;
42
43    with function Hash (Element : Element_Type) return Hash_Type;
44
45    with function Equivalent_Elements (Left, Right : Element_Type)
46                                      return Boolean;
47
48    with function "=" (Left, Right : Element_Type) return Boolean is <>;
49
50 package Ada.Containers.Indefinite_Hashed_Sets is
51    pragma Preelaborate;
52    pragma Remote_Types;
53
54    type Set is tagged private;
55    pragma Preelaborable_Initialization (Set);
56
57    type Cursor is private;
58    pragma Preelaborable_Initialization (Cursor);
59
60    Empty_Set : constant Set;
61    --  Set objects declared without an initialization expression are
62    --  initialized to the value Empty_Set.
63
64    No_Element : constant Cursor;
65    --  Cursor objects declared without an initialization expression are
66    --  initialized to the value No_Element.
67
68    function "=" (Left, Right : Set) return Boolean;
69    --  For each element in Left, set equality attempts to find the equal
70    --  element in Right; if a search fails, then set equality immediately
71    --  returns False. The search works by calling Hash to find the bucket in
72    --  the Right set that corresponds to the Left element. If the bucket is
73    --  non-empty, the search calls the generic formal element equality operator
74    --  to compare the element (in Left) to the element of each node in the
75    --  bucket (in Right); the search terminates when a matching node in the
76    --  bucket is found, or the nodes in the bucket are exhausted. (Note that
77    --  element equality is called here, not Equivalent_Elements. Set equality
78    --  is the only operation in which element equality is used. Compare set
79    --  equality to Equivalent_Sets, which does call Equivalent_Elements.)
80
81    function Equivalent_Sets (Left, Right : Set) return Boolean;
82    --  Similar to set equality, with the difference that the element in Left is
83    --  compared to the elements in Right using the generic formal
84    --  Equivalent_Elements operation instead of element equality.
85
86    function To_Set (New_Item : Element_Type) return Set;
87    --  Constructs a singleton set comprising New_Element. To_Set calls Hash to
88    --  determine the bucket for New_Item.
89
90    function Capacity (Container : Set) return Count_Type;
91    --  Returns the current capacity of the set. Capacity is the maximum length
92    --  before which rehashing in guaranteed not to occur.
93
94    procedure Reserve_Capacity (Container : in out Set; Capacity : Count_Type);
95    --  Adjusts the current capacity, by allocating a new buckets array. If the
96    --  requested capacity is less than the current capacity, then the capacity
97    --  is contracted (to a value not less than the current length). If the
98    --  requested capacity is greater than the current capacity, then the
99    --  capacity is expanded (to a value not less than what is requested). In
100    --  either case, the nodes are rehashed from the old buckets array onto the
101    --  new buckets array (Hash is called once for each existing element in
102    --  order to compute the new index), and then the old buckets array is
103    --  deallocated.
104
105    function Length (Container : Set) return Count_Type;
106    --  Returns the number of items in the set
107
108    function Is_Empty (Container : Set) return Boolean;
109    --  Equivalent to Length (Container) = 0
110
111    procedure Clear (Container : in out Set);
112    --  Removes all of the items from the set
113
114    function Element (Position : Cursor) return Element_Type;
115    --  Returns the element of the node designated by the cursor
116
117    procedure Replace_Element
118      (Container : in out Set;
119       Position  : Cursor;
120       New_Item  : Element_Type);
121    --  If New_Item is equivalent (as determined by calling Equivalent_Elements)
122    --  to the element of the node designated by Position, then New_Element is
123    --  assigned to that element. Otherwise, it calls Hash to determine the
124    --  bucket for New_Item. If the bucket is not empty, then it calls
125    --  Equivalent_Elements for each node in that bucket to determine whether
126    --  New_Item is equivalent to an element in that bucket. If
127    --  Equivalent_Elements returns True then Program_Error is raised (because
128    --  an element may appear only once in the set); otherwise, New_Item is
129    --  assigned to the node designated by Position, and the node is moved to
130    --  its new bucket.
131
132    procedure Query_Element
133      (Position : Cursor;
134       Process  : not null access procedure (Element : Element_Type));
135    --  Calls Process with the element (having only a constant view) of the node
136    --  designed by the cursor.
137
138    procedure Move (Target : in out Set; Source : in out Set);
139    --  Clears Target (if it's not empty), and then moves (not copies) the
140    --  buckets array and nodes from Source to Target.
141
142    procedure Insert
143      (Container : in out Set;
144       New_Item  : Element_Type;
145       Position  : out Cursor;
146       Inserted  : out Boolean);
147    --  Conditionally inserts New_Item into the set. If New_Item is already in
148    --  the set, then Inserted returns False and Position designates the node
149    --  containing the existing element (which is not modified). If New_Item is
150    --  not already in the set, then Inserted returns True and Position
151    --  designates the newly-inserted node containing New_Item. The search for
152    --  an existing element works as follows. Hash is called to determine
153    --  New_Item's bucket; if the bucket is non-empty, then Equivalent_Elements
154    --  is called to compare New_Item to the element of each node in that
155    --  bucket. If the bucket is empty, or there were no equivalent elements in
156    --  the bucket, the search "fails" and the New_Item is inserted in the set
157    --  (and Inserted returns True); otherwise, the search "succeeds" (and
158    --  Inserted returns False).
159
160    procedure Insert  (Container : in out Set; New_Item : Element_Type);
161    --  Attempts to insert New_Item into the set, performing the usual insertion
162    --  search (which involves calling both Hash and Equivalent_Elements); if
163    --  the search succeeds (New_Item is equivalent to an element already in the
164    --  set, and so was not inserted), then this operation raises
165    --  Constraint_Error. (This version of Insert is similar to Replace, but
166    --  having the opposite exception behavior. It is intended for use when you
167    --  want to assert that the item is not already in the set.)
168
169    procedure Include (Container : in out Set; New_Item : Element_Type);
170    --  Attempts to insert New_Item into the set. If an element equivalent to
171    --  New_Item is already in the set (the insertion search succeeded, and
172    --  hence New_Item was not inserted), then the value of New_Item is assigned
173    --  to the existing element. (This insertion operation only raises an
174    --  exception if cursor tampering occurs. It is intended for use when you
175    --  want to insert the item in the set, and you don't care whether an
176    --  equivalent element is already present.)
177
178    procedure Replace (Container : in out Set; New_Item : Element_Type);
179    --  Searches for New_Item in the set; if the search fails (because an
180    --  equivalent element was not in the set), then it raises
181    --  Constraint_Error. Otherwise, the existing element is assigned the value
182    --  New_Item. (This is similar to Insert, but with the opposite exception
183    --  behavior. It is intended for use when you want to assert that the item
184    --  is already in the set.)
185
186    procedure Exclude (Container : in out Set; Item : Element_Type);
187    --  Searches for Item in the set, and if found, removes its node from the
188    --  set and then deallocates it. The search works as follows. The operation
189    --  calls Hash to determine the item's bucket; if the bucket is not empty,
190    --  it calls Equivalent_Elements to compare Item to the element of each node
191    --  in the bucket. (This is the deletion analog of Include. It is intended
192    --  for use when you want to remove the item from the set, but don't care
193    --  whether the item is already in the set.)
194
195    procedure Delete  (Container : in out Set; Item : Element_Type);
196    --  Searches for Item in the set (which involves calling both Hash and
197    --  Equivalent_Elements). If the search fails, then the operation raises
198    --  Constraint_Error. Otherwise it removes the node from the set and then
199    --  deallocates it. (This is the deletion analog of non-conditional
200    --  Insert. It is intended for use when you want to assert that the item is
201    --  already in the set.)
202
203    procedure Delete (Container : in out Set; Position  : in out Cursor);
204    --  Removes the node designated by Position from the set, and then
205    --  deallocates the node. The operation calls Hash to determine the bucket,
206    --  and then compares Position to each node in the bucket until there's a
207    --  match (it does not call Equivalent_Elements).
208
209    procedure Union (Target : in out Set; Source : Set);
210    --  The operation first calls Reserve_Capacity if the current capacity is
211    --  less than the sum of the lengths of Source and Target. It then iterates
212    --  over the Source set, and conditionally inserts each element into Target.
213
214    function Union (Left, Right : Set) return Set;
215    --  The operation first copies the Left set to the result, and then iterates
216    --  over the Right set to conditionally insert each element into the result.
217
218    function "or" (Left, Right : Set) return Set renames Union;
219
220    procedure Intersection (Target : in out Set; Source : Set);
221    --  Iterates over the Target set (calling First and Next), calling Find to
222    --  determine whether the element is in Source. If an equivalent element is
223    --  not found in Source, the element is deleted from Target.
224
225    function Intersection (Left, Right : Set) return Set;
226    --  Iterates over the Left set, calling Find to determine whether the
227    --  element is in Right. If an equivalent element is found, it is inserted
228    --  into the result set.
229
230    function "and" (Left, Right : Set) return Set renames Intersection;
231
232    procedure Difference (Target : in out Set; Source : Set);
233    --  Iterates over the Source (calling First and Next), calling Find to
234    --  determine whether the element is in Target. If an equivalent element is
235    --  found, it is deleted from Target.
236
237    function Difference (Left, Right : Set) return Set;
238    --  Iterates over the Left set, calling Find to determine whether the
239    --  element is in the Right set. If an equivalent element is not found, the
240    --  element is inserted into the result set.
241
242    function "-" (Left, Right : Set) return Set renames Difference;
243
244    procedure Symmetric_Difference (Target : in out Set; Source : Set);
245    --  The operation first calls Reserve_Capacity if the current capacity is
246    --  less than the sum of the lengths of Source and Target. It then iterates
247    --  over the Source set, searching for the element in Target (calling Hash
248    --  and Equivalent_Elements). If an equivalent element is found, it is
249    --  removed from Target; otherwise it is inserted into Target.
250
251    function Symmetric_Difference (Left, Right : Set) return Set;
252    --  The operation first iterates over the Left set. It calls Find to
253    --  determine whether the element is in the Right set. If no equivalent
254    --  element is found, the element from Left is inserted into the result. The
255    --  operation then iterates over the Right set, to determine whether the
256    --  element is in the Left set. If no equivalent element is found, the Right
257    --  element is inserted into the result.
258
259    function "xor" (Left, Right : Set) return Set
260      renames Symmetric_Difference;
261
262    function Overlap (Left, Right : Set) return Boolean;
263    --  Iterates over the Left set (calling First and Next), calling Find to
264    --  determine whether the element is in the Right set. If an equivalent
265    --  element is found, the operation immediately returns True. The operation
266    --  returns False if the iteration over Left terminates without finding any
267    --  equivalent element in Right.
268
269    function Is_Subset (Subset : Set; Of_Set : Set) return Boolean;
270    --  Iterates over Subset (calling First and Next), calling Find to determine
271    --  whether the element is in Of_Set. If no equivalent element is found in
272    --  Of_Set, the operation immediately returns False. The operation returns
273    --  True if the iteration over Subset terminates without finding an element
274    --  not in Of_Set (that is, every element in Subset is equivalent to an
275    --  element in Of_Set).
276
277    function First (Container : Set) return Cursor;
278    --  Returns a cursor that designates the first non-empty bucket, by
279    --  searching from the beginning of the buckets array.
280
281    function Next (Position : Cursor) return Cursor;
282    --  Returns a cursor that designates the node that follows the current one
283    --  designated by Position. If Position designates the last node in its
284    --  bucket, the operation calls Hash to compute the index of this bucket,
285    --  and searches the buckets array for the first non-empty bucket, starting
286    --  from that index; otherwise, it simply follows the link to the next node
287    --  in the same bucket.
288
289    procedure Next (Position : in out Cursor);
290    --  Equivalent to Position := Next (Position)
291
292    function Find (Container : Set; Item : Element_Type) return Cursor;
293    --  Searches for Item in the set. Find calls Hash to determine the item's
294    --  bucket; if the bucket is not empty, it calls Equivalent_Elements to
295    --  compare Item to each element in the bucket. If the search succeeds, Find
296    --  returns a cursor designating the node containing the equivalent element;
297    --  otherwise, it returns No_Element.
298
299    function Contains (Container : Set; Item : Element_Type) return Boolean;
300    --  Equivalent to Find (Container, Item) /= No_Element
301
302    function Has_Element (Position : Cursor) return Boolean;
303    --  Equivalent to Position /= No_Element
304
305    function Equivalent_Elements (Left, Right : Cursor) return Boolean;
306    --  Returns the result of calling Equivalent_Elements with the elements of
307    --  the nodes designated by cursors Left and Right.
308
309    function Equivalent_Elements
310      (Left  : Cursor;
311       Right : Element_Type) return Boolean;
312    --  Returns the result of calling Equivalent_Elements with element of the
313    --  node designated by Left and element Right.
314
315    function Equivalent_Elements
316      (Left  : Element_Type;
317       Right : Cursor) return Boolean;
318    --  Returns the result of calling Equivalent_Elements with element Left and
319    --  the element of the node designated by Right.
320
321    procedure Iterate
322      (Container : Set;
323       Process   : not null access procedure (Position : Cursor));
324    --  Calls Process for each node in the set
325
326    generic
327       type Key_Type (<>) is private;
328
329       with function Key (Element : Element_Type) return Key_Type;
330
331       with function Hash (Key : Key_Type) return Hash_Type;
332
333       with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
334
335    package Generic_Keys is
336
337       function Key (Position : Cursor) return Key_Type;
338       --  Applies generic formal operation Key to the element of the node
339       --  designated by Position.
340
341       function Element (Container : Set; Key : Key_Type) return Element_Type;
342       --  Searches (as per the key-based Find) for the node containing Key, and
343       --  returns the associated element.
344
345       procedure Replace
346         (Container : in out Set;
347          Key       : Key_Type;
348          New_Item  : Element_Type);
349       --  Searches (as per the key-based Find) for the node containing Key, and
350       --  then replaces the element of that node (as per the element-based
351       --  Replace_Element).
352
353       procedure Exclude (Container : in out Set; Key : Key_Type);
354       --  Searches for Key in the set, and if found, removes its node from the
355       --  set and then deallocates it. The search works by first calling Hash
356       --  (on Key) to determine the bucket; if the bucket is not empty, it
357       --  calls Equivalent_Keys to compare parameter Key to the value of
358       --  generic formal operation Key applied to element of each node in the
359       --  bucket.
360
361       procedure Delete (Container : in out Set; Key : Key_Type);
362       --  Deletes the node containing Key as per Exclude, with the difference
363       --  that Constraint_Error is raised if Key is not found.
364
365       function Find (Container : Set; Key : Key_Type) return Cursor;
366       --  Searches for the node containing Key, and returns a cursor
367       --  designating the node. The search works by first calling Hash (on Key)
368       --  to determine the bucket. If the bucket is not empty, the search
369       --  compares Key to the element of each node in the bucket, and returns
370       --  the matching node. The comparison itself works by applying the
371       --  generic formal Key operation to the element of the node, and then
372       --  calling generic formal operation Equivalent_Keys.
373
374       function Contains (Container : Set; Key : Key_Type) return Boolean;
375       --  Equivalent to Find (Container, Key) /= No_Element
376
377       procedure Update_Element_Preserving_Key
378         (Container : in out Set;
379          Position  : Cursor;
380          Process   : not null access
381                        procedure (Element : in out Element_Type));
382       --  Calls Process with the element of the node designated by Position,
383       --  but with the restriction that the key-value of the element is not
384       --  modified. The operation first makes a copy of the value returned by
385       --  applying generic formal operation Key on the element of the node, and
386       --  then calls Process with the element. The operation verifies that the
387       --  key-part has not been modified by calling generic formal operation
388       --  Equivalent_Keys to compare the saved key-value to the value returned
389       --  by applying generic formal operation Key to the post-Process value of
390       --  element. If the key values compare equal then the operation
391       --  completes. Otherwise, the node is removed from the map and
392       --  Program_Error is raised.
393
394    end Generic_Keys;
395
396 private
397
398    pragma Inline (Next);
399
400    type Node_Type;
401    type Node_Access is access Node_Type;
402
403    type Element_Access is access Element_Type;
404
405    type Node_Type is
406       limited record
407          Element : Element_Access;
408          Next    : Node_Access;
409       end record;
410
411    package HT_Types is new Hash_Tables.Generic_Hash_Table_Types
412      (Node_Type,
413       Node_Access);
414
415    type Set is new Ada.Finalization.Controlled with record
416       HT : HT_Types.Hash_Table_Type;
417    end record;
418
419    overriding
420    procedure Adjust (Container : in out Set);
421
422    overriding
423    procedure Finalize (Container : in out Set);
424
425    use HT_Types;
426    use Ada.Finalization;
427    use Ada.Streams;
428
429    type Set_Access is access all Set;
430    for Set_Access'Storage_Size use 0;
431
432    type Cursor is
433       record
434          Container : Set_Access;
435          Node      : Node_Access;
436       end record;
437
438    procedure Write
439      (Stream : not null access Root_Stream_Type'Class;
440       Item   : Cursor);
441
442    for Cursor'Write use Write;
443
444    procedure Read
445      (Stream : not null access Root_Stream_Type'Class;
446       Item   : out Cursor);
447
448    for Cursor'Read use Read;
449
450    No_Element : constant Cursor :=
451                   (Container => null,
452                    Node      => null);
453
454    procedure Write
455      (Stream    : not null access Root_Stream_Type'Class;
456       Container : Set);
457
458    for Set'Write use Write;
459
460    procedure Read
461      (Stream    : not null access Root_Stream_Type'Class;
462       Container : out Set);
463
464    for Set'Read use Read;
465
466    Empty_Set : constant Set := (Controlled with HT => (null, 0, 0, 0));
467
468 end Ada.Containers.Indefinite_Hashed_Sets;