OSDN Git Service

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