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