OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-cohama.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 _ M A P S           --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2007, 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 Key_Type is private;
42    type Element_Type is private;
43
44    with function Hash (Key : Key_Type) return Hash_Type;
45    with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
46    with function "=" (Left, Right : Element_Type) return Boolean is <>;
47
48 package Ada.Containers.Hashed_Maps is
49    pragma Preelaborate;
50    pragma Remote_Types;
51
52    type Map is tagged private;
53    pragma Preelaborable_Initialization (Map);
54
55    type Cursor is private;
56    pragma Preelaborable_Initialization (Cursor);
57
58    Empty_Map : constant Map;
59    --  Map objects declared without an initialization expression are
60    --  initialized to the value Empty_Map.
61
62    No_Element : constant Cursor;
63    --  Cursor objects declared without an initialization expression are
64    --  initialized to the value No_Element.
65
66    function "=" (Left, Right : Map) return Boolean;
67    --  For each key/element pair in Left, equality attempts to find the key in
68    --  Right; if a search fails the equality returns False. The search works by
69    --  calling Hash to find the bucket in the Right map that corresponds to the
70    --  Left key. If bucket is non-empty, then equality calls Equivalent_Keys
71    --  to compare the key (in Left) to the key of each node in the bucket (in
72    --  Right); if the keys are equivalent, then the equality test for this
73    --  key/element pair (in Left) completes by calling the element equality
74    --  operator to compare the element (in Left) to the element of the node
75    --  (in Right) whose key matched.
76
77    function Capacity (Container : Map) return Count_Type;
78    --  Returns the current capacity of the map. Capacity is the maximum length
79    --  before which rehashing in guaranteed not to occur.
80
81    procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
82    --  Adjusts the current capacity, by allocating a new buckets array. If the
83    --  requested capacity is less than the current capacity, then the capacity
84    --  is contracted (to a value not less than the curent length). If the
85    --  requested capacity is greater than the current capacity, then the
86    --  capacity is expanded (to a value not less than what is requested). In
87    --  either case, the nodes are rehashed from the old buckets array onto the
88    --  new buckets array (Hash is called once for each existing key in order to
89    --  compute the new index), and then the old buckets array is deallocated.
90
91    function Length (Container : Map) return Count_Type;
92    --  Returns the number of items in the map
93
94    function Is_Empty (Container : Map) return Boolean;
95    --  Equivalent to Length (Container) = 0
96
97    procedure Clear (Container : in out Map);
98    --  Removes all of the items from the map
99
100    function Key (Position : Cursor) return Key_Type;
101    --  Returns the key of the node designated by the cursor
102
103    function Element (Position : Cursor) return Element_Type;
104    --  Returns the element of the node designated by the cursor
105
106    procedure Replace_Element
107      (Container : in out Map;
108       Position  : Cursor;
109       New_Item  : Element_Type);
110    --  Assigns the value New_Item to the element designated by the cursor
111
112    procedure Query_Element
113      (Position : Cursor;
114       Process  : not null access
115                    procedure (Key : Key_Type; Element : Element_Type));
116    --  Calls Process with the key and element (both having only a constant
117    --  view) of the node designed by the cursor.
118
119    procedure Update_Element
120      (Container : in out Map;
121       Position  : Cursor;
122       Process   : not null access
123                     procedure (Key : Key_Type; Element : in out Element_Type));
124    --  Calls Process with the key (with only a constant view) and element (with
125    --  a variable view) of the node designed by the cursor.
126
127    procedure Move (Target : in out Map; Source : in out Map);
128    --  Clears Target (if it's not empty), and then moves (not copies) the
129    --  buckets array and nodes from Source to Target.
130
131    procedure Insert
132      (Container : in out Map;
133       Key       : Key_Type;
134       New_Item  : Element_Type;
135       Position  : out Cursor;
136       Inserted  : out Boolean);
137    --  Conditionally inserts New_Item into the map. If Key is already in the
138    --  map, then Inserted returns False and Position designates the node
139    --  containing the existing key/element pair (neither of which is modified).
140    --  If Key is not already in the map, the Inserted returns True and Position
141    --  designates the newly-inserted node container Key and New_Item. The
142    --  search for the key works as follows. Hash is called to determine Key's
143    --  bucket; if the bucket is non-empty, then Equivalent_Keys is called to
144    --  compare Key to each node in that bucket. If the bucket is empty, or
145    --  there were no matching keys in the bucket, the search "fails" and the
146    --  key/item pair is inserted in the map (and Inserted returns True);
147    --  otherwise, the search "succeeds" (and Inserted returns False).
148
149    procedure Insert
150      (Container : in out Map;
151       Key       : Key_Type;
152       Position  : out Cursor;
153       Inserted  : out Boolean);
154    --  The same as the (conditional) Insert that accepts an element parameter,
155    --  with the difference that if Inserted returns True, then the element of
156    --  the newly-inserted node is initialized to its default value.
157
158    procedure Insert
159      (Container : in out Map;
160       Key       : Key_Type;
161       New_Item  : Element_Type);
162    --  Attempts to insert Key into the map, performing the usual search (which
163    --  involves calling both Hash and Equivalent_Keys); if the search succeeds
164    --  (because Key is already in the map), then it raises Constraint_Error.
165    --  (This version of Insert is similar to Replace, but having the opposite
166    --  exception behavior. It is intended for use when you want to assert that
167    --  Key is not already in the map.)
168
169    procedure Include
170      (Container : in out Map;
171       Key       : Key_Type;
172       New_Item  : Element_Type);
173    --  Attempts to insert Key into the map. If Key is already in the map, then
174    --  both the existing key and element are assigned the values of Key and
175    --  New_Item, respectively. (This version of Insert only raises an exception
176    --  if cursor tampering occurs. It is intended for use when you want to
177    --  insert the key/element pair in the map, and you don't care whether Key
178    --  is already present.)
179
180    procedure Replace
181      (Container : in out Map;
182       Key       : Key_Type;
183       New_Item  : Element_Type);
184    --  Searches for Key in the map; if the search fails (because Key was not in
185    --  the map), then it raises Constraint_Error. Otherwise, both the existing
186    --  key and element are assigned the values of Key and New_Item rsp. (This
187    --  is similar to Insert, but with the opposite exception behavior. It is to
188    --  be used when you want to assert that Key is already in the map.)
189
190    procedure Exclude (Container : in out Map; Key : Key_Type);
191    --  Searches for Key in the map, and if found, removes its node from the map
192    --  and then deallocates it. The search works as follows. The operation
193    --  calls Hash to determine the key's bucket; if the bucket is not empty, it
194    --  calls Equivalent_Keys to compare Key to each key in the bucket. (This is
195    --  the deletion analog of Include. It is intended for use when you want to
196    --  remove the item from the map, but don't care whether the key is already
197    --  in the map.)
198
199    procedure Delete (Container : in out Map; Key : Key_Type);
200    --  Searches for Key in the map (which involves calling both Hash and
201    --  Equivalent_Keys). If the search fails, then the operation raises
202    --  Constraint_Eror. Otherwise it removes the node from the map and then
203    --  deallocates it. (This is the deletion analog of non-conditional
204    --  Insert. It is intended for use when you want to assert that the item is
205    --  already in the map.)
206
207    procedure Delete (Container : in out Map; Position : in out Cursor);
208    --  Removes the node designated by Position from the map, and then
209    --  deallocates the node. The operation calls Hash to determine the bucket,
210    --  and then compares Position to each node in the bucket until there's a
211    --  match (it does not call Equivalent_Keys).
212
213    function First (Container : Map) return Cursor;
214    --  Returns a cursor that designates the first non-empty bucket, by
215    --  searching from the beginning of the buckets array.
216
217    function Next (Position : Cursor) return Cursor;
218    --  Returns a cursor that designates the node that follows the current one
219    --  designated by Position. If Position designates the last node in its
220    --  bucket, the operation calls Hash to compute the index of this bucket,
221    --  and searches the buckets array for the first non-empty bucket, starting
222    --  from that index; otherwise, it simply follows the link to the next node
223    --  in the same bucket.
224
225    procedure Next (Position : in out Cursor);
226    --  Equivalent to Position := Next (Position)
227
228    function Find (Container : Map; Key : Key_Type) return Cursor;
229    --  Searches for Key in the map. Find calls Hash to determine the key's
230    --  bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
231    --  Key to each key in the bucket. If the search succeeds, Find returns a
232    --  cursor designating the matching node; otherwise, it returns No_Element.
233
234    function Contains (Container : Map; Key : Key_Type) return Boolean;
235    --  Equivalent to Find (Container, Key) /= No_Element
236
237    function Element (Container : Map; Key : Key_Type) return Element_Type;
238    --  Equivalent to Element (Find (Container, Key))
239
240    function Has_Element (Position : Cursor) return Boolean;
241    --  Equivalent to Position /= No_Element
242
243    function Equivalent_Keys (Left, Right : Cursor) return Boolean;
244    --  Returns the result of calling Equivalent_Keys with the keys of the nodes
245    --  designated by cursors Left and Right.
246
247    function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
248    --  Returns the result of calling Equivalent_Keys with key of the node
249    --  designated by Left and key Right.
250
251    function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
252    --  Returns the result of calling Equivalent_Keys with key Left and the node
253    --  designated by Right.
254
255    procedure Iterate
256      (Container : Map;
257       Process   : not null access procedure (Position : Cursor));
258    --  Calls Process for each node in the map
259
260 private
261    pragma Inline ("=");
262    pragma Inline (Length);
263    pragma Inline (Is_Empty);
264    pragma Inline (Clear);
265    pragma Inline (Key);
266    pragma Inline (Element);
267    pragma Inline (Move);
268    pragma Inline (Contains);
269    pragma Inline (Capacity);
270    pragma Inline (Reserve_Capacity);
271    pragma Inline (Has_Element);
272    pragma Inline (Equivalent_Keys);
273    pragma Inline (Next);
274
275    type Node_Type;
276    type Node_Access is access Node_Type;
277
278    type Node_Type is limited record
279       Key     : Key_Type;
280       Element : Element_Type;
281       Next    : Node_Access;
282    end record;
283
284    package HT_Types is new Hash_Tables.Generic_Hash_Table_Types
285      (Node_Type,
286       Node_Access);
287
288    type Map is new Ada.Finalization.Controlled with record
289       HT : HT_Types.Hash_Table_Type;
290    end record;
291
292    use HT_Types;
293    use Ada.Finalization;
294
295    overriding
296    procedure Adjust (Container : in out Map);
297
298    overriding
299    procedure Finalize (Container : in out Map);
300
301    use Ada.Streams;
302
303    procedure Write
304      (Stream    : not null access Root_Stream_Type'Class;
305       Container : Map);
306
307    for Map'Write use Write;
308
309    procedure Read
310      (Stream    : not null access Root_Stream_Type'Class;
311       Container : out Map);
312
313    for Map'Read use Read;
314
315    type Map_Access is access constant Map;
316    for Map_Access'Storage_Size use 0;
317
318    type Cursor is
319       record
320          Container : Map_Access;
321          Node      : Node_Access;
322       end record;
323
324    procedure Read
325      (Stream : not null access Root_Stream_Type'Class;
326       Item   : out Cursor);
327
328    for Cursor'Read use Read;
329
330    procedure Write
331      (Stream : not null access Root_Stream_Type'Class;
332       Item   : Cursor);
333
334    for Cursor'Write use Write;
335
336    Empty_Map : constant Map := (Controlled with HT => (null, 0, 0, 0));
337
338    No_Element : constant Cursor := (Container => null, Node => null);
339
340 end Ada.Containers.Hashed_Maps;