Merge -r 127928:132243 from trunk
[official-gcc.git] / gcc / ada / a-cihama.ads
blob5e0aea0119ac5007d9c50e8a3b198ce01b4d05aa
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- A D A . C O N T A I N E R S . --
6 -- I N D E F I N I T E _ H A S H E D _ M A P S --
7 -- --
8 -- S p e c --
9 -- --
10 -- Copyright (C) 2004-2007, Free Software Foundation, Inc. --
11 -- --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
15 -- --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 2, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
22 -- for more details. You should have received a copy of the GNU General --
23 -- Public License distributed with GNAT; see file COPYING. If not, write --
24 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
25 -- Boston, MA 02110-1301, USA. --
26 -- --
27 -- As a special exception, if other files instantiate generics from this --
28 -- unit, or you link this unit with other files to produce an executable, --
29 -- this unit does not by itself cause the resulting executable to be --
30 -- covered by the GNU General Public License. This exception does not --
31 -- however invalidate any other reasons why the executable file might be --
32 -- covered by the GNU Public License. --
33 -- --
34 -- This unit was originally developed by Matthew J Heaney. --
35 ------------------------------------------------------------------------------
37 private with Ada.Containers.Hash_Tables;
38 private with Ada.Streams;
39 private with Ada.Finalization;
41 generic
42 type Key_Type (<>) is private;
43 type Element_Type (<>) is private;
45 with function Hash (Key : Key_Type) return Hash_Type;
46 with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
47 with function "=" (Left, Right : Element_Type) return Boolean is <>;
49 package Ada.Containers.Indefinite_Hashed_Maps is
50 pragma Preelaborate;
51 pragma Remote_Types;
53 type Map is tagged private;
54 pragma Preelaborable_Initialization (Map);
56 type Cursor is private;
57 pragma Preelaborable_Initialization (Cursor);
59 Empty_Map : constant Map;
60 -- Map objects declared without an initialization expression are
61 -- initialized to the value Empty_Map.
63 No_Element : constant Cursor;
64 -- Cursor objects declared without an initialization expression are
65 -- initialized to the value No_Element.
67 function "=" (Left, Right : Map) return Boolean;
68 -- For each key/element pair in Left, equality attempts to find the key in
69 -- Right; if a search fails the equality returns False. The search works by
70 -- calling Hash to find the bucket in the Right map that corresponds to the
71 -- Left key. If bucket is non-empty, then equality calls Equivalent_Keys
72 -- to compare the key (in Left) to the key of each node in the bucket (in
73 -- Right); if the keys are equivalent, then the equality test for this
74 -- key/element pair (in Left) completes by calling the element equality
75 -- operator to compare the element (in Left) to the element of the node
76 -- (in Right) whose key matched.
78 function Capacity (Container : Map) return Count_Type;
79 -- Returns the current capacity of the map. Capacity is the maximum length
80 -- before which rehashing in guaranteed not to occur.
82 procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
83 -- Adjusts the current capacity, by allocating a new buckets array. If the
84 -- requested capacity is less than the current capacity, then the capacity
85 -- is contracted (to a value not less than the curent length). If the
86 -- requested capacity is greater than the current capacity, then the
87 -- capacity is expanded (to a value not less than what is requested). In
88 -- either case, the nodes are rehashed from the old buckets array onto the
89 -- new buckets array (Hash is called once for each existing key in order to
90 -- compute the new index), and then the old buckets array is deallocated.
92 function Length (Container : Map) return Count_Type;
93 -- Returns the number of items in the map
95 function Is_Empty (Container : Map) return Boolean;
96 -- Equivalent to Length (Container) = 0
98 procedure Clear (Container : in out Map);
99 -- Removes all of the items from the map
101 function Key (Position : Cursor) return Key_Type;
102 -- Returns the key of the node designated by the cursor
104 function Element (Position : Cursor) return Element_Type;
105 -- Returns the element of the node designated by the cursor
107 procedure Replace_Element
108 (Container : in out Map;
109 Position : Cursor;
110 New_Item : Element_Type);
111 -- Assigns the value New_Item to the element designated by the cursor
113 procedure Query_Element
114 (Position : Cursor;
115 Process : not null access procedure (Key : Key_Type;
116 Element : Element_Type));
117 -- Calls Process with the key and element (both having only a constant
118 -- view) of the node designed by the cursor.
120 procedure Update_Element
121 (Container : in out Map;
122 Position : Cursor;
123 Process : not null access procedure (Key : Key_Type;
124 Element : in out Element_Type));
125 -- Calls Process with the key (with only a constant view) and element (with
126 -- a variable view) of the node designed by the cursor.
128 procedure Move (Target : in out Map; Source : in out Map);
129 -- Clears Target (if it's not empty), and then moves (not copies) the
130 -- buckets array and nodes from Source to Target.
132 procedure Insert
133 (Container : in out Map;
134 Key : Key_Type;
135 New_Item : Element_Type;
136 Position : out Cursor;
137 Inserted : out Boolean);
138 -- Conditionally inserts New_Item into the map. If Key is already in the
139 -- map, then Inserted returns False and Position designates the node
140 -- containing the existing key/element pair (neither of which is modified).
141 -- If Key is not already in the map, the Inserted returns True and Position
142 -- designates the newly-inserted node container Key and New_Item. The
143 -- search for the key works as follows. Hash is called to determine Key's
144 -- bucket; if the bucket is non-empty, then Equivalent_Keys is called to
145 -- compare Key to each node in that bucket. If the bucket is empty, or
146 -- there were no matching keys in the bucket, the search "fails" and the
147 -- key/item pair is inserted in the map (and Inserted returns True);
148 -- otherwise, the search "succeeds" (and Inserted returns False).
150 procedure Insert
151 (Container : in out Map;
152 Key : Key_Type;
153 New_Item : Element_Type);
154 -- Attempts to insert Key into the map, performing the usual search (which
155 -- involves calling both Hash and Equivalent_Keys); if the search succeeds
156 -- (because Key is already in the map), then it raises Constraint_Error.
157 -- (This version of Insert is similar to Replace, but having the opposite
158 -- exception behavior. It is intended for use when you want to assert that
159 -- Key is not already in the map.)
161 procedure Include
162 (Container : in out Map;
163 Key : Key_Type;
164 New_Item : Element_Type);
165 -- Attempts to insert Key into the map. If Key is already in the map, then
166 -- both the existing key and element are assigned the values of Key and
167 -- New_Item, respectively. (This version of Insert only raises an exception
168 -- if cursor tampering occurs. It is intended for use when you want to
169 -- insert the key/element pair in the map, and you don't care whether Key
170 -- is already present.)
172 procedure Replace
173 (Container : in out Map;
174 Key : Key_Type;
175 New_Item : Element_Type);
176 -- Searches for Key in the map; if the search fails (because Key was not in
177 -- the map), then it raises Constraint_Error. Otherwise, both the existing
178 -- key and element are assigned the values of Key and New_Item rsp. (This
179 -- is similar to Insert, but with the opposite exception behavior. It is
180 -- intended for use when you want to assert that Key is already in the
181 -- map.)
183 procedure Exclude (Container : in out Map; Key : Key_Type);
184 -- Searches for Key in the map, and if found, removes its node from the map
185 -- and then deallocates it. The search works as follows. The operation
186 -- calls Hash to determine the key's bucket; if the bucket is not empty, it
187 -- calls Equivalent_Keys to compare Key to each key in the bucket. (This is
188 -- the deletion analog of Include. It is intended for use when you want to
189 -- remove the item from the map, but don't care whether the key is already
190 -- in the map.)
192 procedure Delete (Container : in out Map; Key : Key_Type);
193 -- Searches for Key in the map (which involves calling both Hash and
194 -- Equivalent_Keys). If the search fails, then the operation raises
195 -- Constraint_Eror. Otherwise it removes the node from the map and then
196 -- deallocates it. (This is the deletion analog of non-conditional
197 -- Insert. It is intended for use when you want to assert that the item is
198 -- already in the map.)
200 procedure Delete (Container : in out Map; Position : in out Cursor);
201 -- Removes the node designated by Position from the map, and then
202 -- deallocates the node. The operation calls Hash to determine the bucket,
203 -- and then compares Position to each node in the bucket until there's a
204 -- match (it does not call Equivalent_Keys).
206 function First (Container : Map) return Cursor;
207 -- Returns a cursor that designates the first non-empty bucket, by
208 -- searching from the beginning of the buckets array.
210 function Next (Position : Cursor) return Cursor;
211 -- Returns a cursor that designates the node that follows the current one
212 -- designated by Position. If Position designates the last node in its
213 -- bucket, the operation calls Hash to compute the index of this bucket,
214 -- and searches the buckets array for the first non-empty bucket, starting
215 -- from that index; otherwise, it simply follows the link to the next node
216 -- in the same bucket.
218 procedure Next (Position : in out Cursor);
219 -- Equivalent to Position := Next (Position)
221 function Find (Container : Map; Key : Key_Type) return Cursor;
222 -- Searches for Key in the map. Find calls Hash to determine the key's
223 -- bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
224 -- Key to each key in the bucket. If the search succeeds, Find returns a
225 -- cursor designating the matching node; otherwise, it returns No_Element.
227 function Contains (Container : Map; Key : Key_Type) return Boolean;
228 -- Equivalent to Find (Container, Key) /= No_Element
230 function Element (Container : Map; Key : Key_Type) return Element_Type;
231 -- Equivalent to Element (Find (Container, Key))
233 function Has_Element (Position : Cursor) return Boolean;
234 -- Equivalent to Position /= No_Element
236 function Equivalent_Keys (Left, Right : Cursor) return Boolean;
237 -- Returns the result of calling Equivalent_Keys with the keys of the nodes
238 -- designated by cursors Left and Right.
240 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
241 -- Returns the result of calling Equivalent_Keys with key of the node
242 -- designated by Left and key Right.
244 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
245 -- Returns the result of calling Equivalent_Keys with key Left and the node
246 -- designated by Right.
248 procedure Iterate
249 (Container : Map;
250 Process : not null access procedure (Position : Cursor));
251 -- Calls Process for each node in the map
253 private
254 pragma Inline ("=");
255 pragma Inline (Length);
256 pragma Inline (Is_Empty);
257 pragma Inline (Clear);
258 pragma Inline (Key);
259 pragma Inline (Element);
260 pragma Inline (Move);
261 pragma Inline (Contains);
262 pragma Inline (Capacity);
263 pragma Inline (Reserve_Capacity);
264 pragma Inline (Has_Element);
265 pragma Inline (Equivalent_Keys);
266 pragma Inline (Next);
268 type Node_Type;
269 type Node_Access is access Node_Type;
271 type Key_Access is access Key_Type;
272 type Element_Access is access Element_Type;
274 type Node_Type is limited record
275 Key : Key_Access;
276 Element : Element_Access;
277 Next : Node_Access;
278 end record;
280 package HT_Types is new Hash_Tables.Generic_Hash_Table_Types
281 (Node_Type,
282 Node_Access);
284 type Map is new Ada.Finalization.Controlled with record
285 HT : HT_Types.Hash_Table_Type;
286 end record;
288 use HT_Types;
289 use Ada.Finalization;
290 use Ada.Streams;
292 overriding
293 procedure Adjust (Container : in out Map);
295 overriding
296 procedure Finalize (Container : in out Map);
298 type Map_Access is access constant Map;
299 for Map_Access'Storage_Size use 0;
301 type Cursor is
302 record
303 Container : Map_Access;
304 Node : Node_Access;
305 end record;
307 procedure Write
308 (Stream : not null access Root_Stream_Type'Class;
309 Item : Cursor);
311 for Cursor'Write use Write;
313 procedure Read
314 (Stream : not null access Root_Stream_Type'Class;
315 Item : out Cursor);
317 for Cursor'Read use Read;
319 No_Element : constant Cursor :=
320 (Container => null,
321 Node => null);
323 procedure Write
324 (Stream : not null access Root_Stream_Type'Class;
325 Container : Map);
327 for Map'Write use Write;
329 procedure Read
330 (Stream : not null access Root_Stream_Type'Class;
331 Container : out Map);
333 for Map'Read use Read;
335 Empty_Map : constant Map := (Controlled with HT => (null, 0, 0, 0));
337 end Ada.Containers.Indefinite_Hashed_Maps;