OSDN Git Service

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