OSDN Git Service

2012-01-10 Richard Guenther <rguenther@suse.de>
[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-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.Finalization;
36
37 with Ada.Streams; use Ada.Streams;
38 with Ada.Iterator_Interfaces;
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    with
54       Constant_Indexing => Constant_Reference,
55       Variable_Indexing => Reference,
56       Default_Iterator  => Iterate,
57       Iterator_Element  => Element_Type;
58
59    pragma Preelaborable_Initialization (Map);
60
61    type Cursor is private;
62    pragma Preelaborable_Initialization (Cursor);
63
64    procedure Read
65      (Stream : not null access Root_Stream_Type'Class;
66       Item   : out Cursor);
67
68    for Cursor'Read use Read;
69
70    procedure Write
71      (Stream : not null access Root_Stream_Type'Class;
72       Item   : Cursor);
73
74    for Cursor'Write use Write;
75
76    Empty_Map : constant Map;
77    --  Map objects declared without an initialization expression are
78    --  initialized to the value Empty_Map.
79
80    No_Element : constant Cursor;
81    --  Cursor objects declared without an initialization expression are
82    --  initialized to the value No_Element.
83
84    function Has_Element (Position : Cursor) return Boolean;
85    --  Equivalent to Position /= No_Element
86
87    package Map_Iterator_Interfaces is new
88      Ada.Iterator_Interfaces (Cursor, Has_Element);
89
90    function "=" (Left, Right : Map) return Boolean;
91    --  For each key/element pair in Left, equality attempts to find the key in
92    --  Right; if a search fails the equality returns False. The search works by
93    --  calling Hash to find the bucket in the Right map that corresponds to the
94    --  Left key. If bucket is non-empty, then equality calls Equivalent_Keys
95    --  to compare the key (in Left) to the key of each node in the bucket (in
96    --  Right); if the keys are equivalent, then the equality test for this
97    --  key/element pair (in Left) completes by calling the element equality
98    --  operator to compare the element (in Left) to the element of the node
99    --  (in Right) whose key matched.
100
101    function Capacity (Container : Map) return Count_Type;
102    --  Returns the current capacity of the map. Capacity is the maximum length
103    --  before which rehashing in guaranteed not to occur.
104
105    procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
106    --  Adjusts the current capacity, by allocating a new buckets array. If the
107    --  requested capacity is less than the current capacity, then the capacity
108    --  is contracted (to a value not less than the current length). If the
109    --  requested capacity is greater than the current capacity, then the
110    --  capacity is expanded (to a value not less than what is requested). In
111    --  either case, the nodes are rehashed from the old buckets array onto the
112    --  new buckets array (Hash is called once for each existing key in order to
113    --  compute the new index), and then the old buckets array is deallocated.
114
115    function Length (Container : Map) return Count_Type;
116    --  Returns the number of items in the map
117
118    function Is_Empty (Container : Map) return Boolean;
119    --  Equivalent to Length (Container) = 0
120
121    procedure Clear (Container : in out Map);
122    --  Removes all of the items from the map
123
124    function Key (Position : Cursor) return Key_Type;
125    --  Returns the key of the node designated by the cursor
126
127    function Element (Position : Cursor) return Element_Type;
128    --  Returns the element of the node designated by the cursor
129
130    procedure Replace_Element
131      (Container : in out Map;
132       Position  : Cursor;
133       New_Item  : Element_Type);
134    --  Assigns the value New_Item to the element designated by the cursor
135
136    procedure Query_Element
137      (Position : Cursor;
138       Process  : not null access
139                    procedure (Key : Key_Type; Element : Element_Type));
140    --  Calls Process with the key and element (both having only a constant
141    --  view) of the node designed by the cursor.
142
143    procedure Update_Element
144      (Container : in out Map;
145       Position  : Cursor;
146       Process   : not null access
147                     procedure (Key : Key_Type; Element : in out Element_Type));
148    --  Calls Process with the key (with only a constant view) and element (with
149    --  a variable view) of the node designed by the cursor.
150
151    type Constant_Reference_Type
152       (Element : not null access constant Element_Type) is private
153    with
154       Implicit_Dereference => Element;
155
156    procedure Write
157      (Stream : not null access Root_Stream_Type'Class;
158       Item   : Constant_Reference_Type);
159
160    for Constant_Reference_Type'Write use Write;
161
162    procedure Read
163      (Stream : not null access Root_Stream_Type'Class;
164       Item   : out Constant_Reference_Type);
165
166    for Constant_Reference_Type'Read use Read;
167
168    type Reference_Type (Element : not null access Element_Type) is private
169    with
170       Implicit_Dereference => Element;
171
172    procedure Write
173      (Stream : not null access Root_Stream_Type'Class;
174       Item   : Reference_Type);
175
176    for Reference_Type'Write use Write;
177
178    procedure Read
179      (Stream : not null access Root_Stream_Type'Class;
180       Item   : out Reference_Type);
181
182    for Reference_Type'Read use Read;
183
184    function Constant_Reference
185      (Container : aliased Map;
186       Position  : Cursor) return Constant_Reference_Type;
187
188    function Reference
189      (Container : aliased in out Map;
190       Position  : Cursor) return Reference_Type;
191
192    function Constant_Reference
193      (Container : aliased Map;
194       Key       : Key_Type) return Constant_Reference_Type;
195
196    function Reference
197      (Container : aliased in out Map;
198       Key       : Key_Type) return Reference_Type;
199
200    procedure Assign (Target : in out Map; Source : Map);
201
202    function Copy (Source : Map; Capacity : Count_Type := 0) return Map;
203
204    procedure Move (Target : in out Map; Source : in out Map);
205    --  Clears Target (if it's not empty), and then moves (not copies) the
206    --  buckets array and nodes from Source to Target.
207
208    procedure Insert
209      (Container : in out Map;
210       Key       : Key_Type;
211       New_Item  : Element_Type;
212       Position  : out Cursor;
213       Inserted  : out Boolean);
214    --  Conditionally inserts New_Item into the map. If Key is already in the
215    --  map, then Inserted returns False and Position designates the node
216    --  containing the existing key/element pair (neither of which is modified).
217    --  If Key is not already in the map, the Inserted returns True and Position
218    --  designates the newly-inserted node container Key and New_Item. The
219    --  search for the key works as follows. Hash is called to determine Key's
220    --  bucket; if the bucket is non-empty, then Equivalent_Keys is called to
221    --  compare Key to each node in that bucket. If the bucket is empty, or
222    --  there were no matching keys in the bucket, the search "fails" and the
223    --  key/item pair is inserted in the map (and Inserted returns True);
224    --  otherwise, the search "succeeds" (and Inserted returns False).
225
226    procedure Insert
227      (Container : in out Map;
228       Key       : Key_Type;
229       Position  : out Cursor;
230       Inserted  : out Boolean);
231    --  The same as the (conditional) Insert that accepts an element parameter,
232    --  with the difference that if Inserted returns True, then the element of
233    --  the newly-inserted node is initialized to its default value.
234
235    procedure Insert
236      (Container : in out Map;
237       Key       : Key_Type;
238       New_Item  : Element_Type);
239    --  Attempts to insert Key into the map, performing the usual search (which
240    --  involves calling both Hash and Equivalent_Keys); if the search succeeds
241    --  (because Key is already in the map), then it raises Constraint_Error.
242    --  (This version of Insert is similar to Replace, but having the opposite
243    --  exception behavior. It is intended for use when you want to assert that
244    --  Key is not already in the map.)
245
246    procedure Include
247      (Container : in out Map;
248       Key       : Key_Type;
249       New_Item  : Element_Type);
250    --  Attempts to insert Key into the map. If Key is already in the map, then
251    --  both the existing key and element are assigned the values of Key and
252    --  New_Item, respectively. (This version of Insert only raises an exception
253    --  if cursor tampering occurs. It is intended for use when you want to
254    --  insert the key/element pair in the map, and you don't care whether Key
255    --  is already present.)
256
257    procedure Replace
258      (Container : in out Map;
259       Key       : Key_Type;
260       New_Item  : Element_Type);
261    --  Searches for Key in the map; if the search fails (because Key was not in
262    --  the map), then it raises Constraint_Error. Otherwise, both the existing
263    --  key and element are assigned the values of Key and New_Item rsp. (This
264    --  is similar to Insert, but with the opposite exception behavior. It is to
265    --  be used when you want to assert that Key is already in the map.)
266
267    procedure Exclude (Container : in out Map; Key : Key_Type);
268    --  Searches for Key in the map, and if found, removes its node from the map
269    --  and then deallocates it. The search works as follows. The operation
270    --  calls Hash to determine the key's bucket; if the bucket is not empty, it
271    --  calls Equivalent_Keys to compare Key to each key in the bucket. (This is
272    --  the deletion analog of Include. It is intended for use when you want to
273    --  remove the item from the map, but don't care whether the key is already
274    --  in the map.)
275
276    procedure Delete (Container : in out Map; Key : Key_Type);
277    --  Searches for Key in the map (which involves calling both Hash and
278    --  Equivalent_Keys). If the search fails, then the operation raises
279    --  Constraint_Error. Otherwise it removes the node from the map and then
280    --  deallocates it. (This is the deletion analog of non-conditional
281    --  Insert. It is intended for use when you want to assert that the item is
282    --  already in the map.)
283
284    procedure Delete (Container : in out Map; Position : in out Cursor);
285    --  Removes the node designated by Position from the map, and then
286    --  deallocates the node. The operation calls Hash to determine the bucket,
287    --  and then compares Position to each node in the bucket until there's a
288    --  match (it does not call Equivalent_Keys).
289
290    function First (Container : Map) 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 (Container : Map; Key : Key_Type) return Cursor;
306    --  Searches for Key in the map. Find calls Hash to determine the key's
307    --  bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
308    --  Key to each key in the bucket. If the search succeeds, Find returns a
309    --  cursor designating the matching node; otherwise, it returns No_Element.
310
311    function Contains (Container : Map; Key : Key_Type) return Boolean;
312    --  Equivalent to Find (Container, Key) /= No_Element
313
314    function Element (Container : Map; Key : Key_Type) return Element_Type;
315    --  Equivalent to Element (Find (Container, Key))
316
317    function Equivalent_Keys (Left, Right : Cursor) return Boolean;
318    --  Returns the result of calling Equivalent_Keys with the keys of the nodes
319    --  designated by cursors Left and Right.
320
321    function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
322    --  Returns the result of calling Equivalent_Keys with key of the node
323    --  designated by Left and key Right.
324
325    function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
326    --  Returns the result of calling Equivalent_Keys with key Left and the node
327    --  designated by Right.
328
329    procedure Iterate
330      (Container : Map;
331       Process   : not null access procedure (Position : Cursor));
332    --  Calls Process for each node in the map
333
334    function Iterate
335      (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'class;
336
337 private
338    pragma Inline ("=");
339    pragma Inline (Length);
340    pragma Inline (Is_Empty);
341    pragma Inline (Clear);
342    pragma Inline (Key);
343    pragma Inline (Element);
344    pragma Inline (Move);
345    pragma Inline (Contains);
346    pragma Inline (Capacity);
347    pragma Inline (Reserve_Capacity);
348    pragma Inline (Has_Element);
349    pragma Inline (Equivalent_Keys);
350    pragma Inline (Next);
351
352    type Node_Type;
353    type Node_Access is access Node_Type;
354
355    type Node_Type is limited record
356       Key     : Key_Type;
357       Element : aliased Element_Type;
358       Next    : Node_Access;
359    end record;
360
361    package HT_Types is
362      new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
363
364    type Map is new Ada.Finalization.Controlled with record
365       HT : HT_Types.Hash_Table_Type;
366    end record;
367
368    use HT_Types;
369    use Ada.Finalization;
370
371    overriding procedure Adjust (Container : in out Map);
372
373    overriding procedure Finalize (Container : in out Map);
374
375    procedure Write
376      (Stream    : not null access Root_Stream_Type'Class;
377       Container : Map);
378
379    for Map'Write use Write;
380
381    procedure Read
382      (Stream    : not null access Root_Stream_Type'Class;
383       Container : out Map);
384
385    for Map'Read use Read;
386
387    type Map_Access is access all Map;
388    for Map_Access'Storage_Size use 0;
389
390    type Cursor is record
391       Container : Map_Access;
392       Node      : Node_Access;
393    end record;
394
395    type Constant_Reference_Type
396       (Element : not null access constant Element_Type) is null record;
397
398    type Reference_Type
399       (Element : not null access Element_Type) is null record;
400
401    Empty_Map : constant Map := (Controlled with HT => (null, 0, 0, 0));
402
403    No_Element : constant Cursor := (Container => null, Node => null);
404
405 end Ada.Containers.Hashed_Maps;