1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . H A S H E D _ M A P S --
9 -- Copyright (C) 2004-2012, Free Software Foundation, Inc. --
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. --
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. --
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. --
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/>. --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 with Ada.Iterator_Interfaces;
36 private with Ada.Containers.Hash_Tables;
37 private with Ada.Finalization;
38 private with Ada.Streams;
41 type Key_Type is private;
42 type Element_Type is private;
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 <>;
48 package Ada.Containers.Hashed_Maps is
52 type Map is tagged private
54 Constant_Indexing => Constant_Reference,
55 Variable_Indexing => Reference,
56 Default_Iterator => Iterate,
57 Iterator_Element => Element_Type;
59 pragma Preelaborable_Initialization (Map);
61 type Cursor is private;
62 pragma Preelaborable_Initialization (Cursor);
64 Empty_Map : constant Map;
65 -- Map objects declared without an initialization expression are
66 -- initialized to the value Empty_Map.
68 No_Element : constant Cursor;
69 -- Cursor objects declared without an initialization expression are
70 -- initialized to the value No_Element.
72 function Has_Element (Position : Cursor) return Boolean;
73 -- Equivalent to Position /= No_Element
75 package Map_Iterator_Interfaces is new
76 Ada.Iterator_Interfaces (Cursor, Has_Element);
78 function "=" (Left, Right : Map) return Boolean;
79 -- For each key/element pair in Left, equality attempts to find the key in
80 -- Right; if a search fails the equality returns False. The search works by
81 -- calling Hash to find the bucket in the Right map that corresponds to the
82 -- Left key. If bucket is non-empty, then equality calls Equivalent_Keys
83 -- to compare the key (in Left) to the key of each node in the bucket (in
84 -- Right); if the keys are equivalent, then the equality test for this
85 -- key/element pair (in Left) completes by calling the element equality
86 -- operator to compare the element (in Left) to the element of the node
87 -- (in Right) whose key matched.
89 function Capacity (Container : Map) return Count_Type;
90 -- Returns the current capacity of the map. Capacity is the maximum length
91 -- before which rehashing in guaranteed not to occur.
93 procedure Reserve_Capacity (Container : in out Map; Capacity : Count_Type);
94 -- Adjusts the current capacity, by allocating a new buckets array. If the
95 -- requested capacity is less than the current capacity, then the capacity
96 -- is contracted (to a value not less than the current length). If the
97 -- requested capacity is greater than the current capacity, then the
98 -- capacity is expanded (to a value not less than what is requested). In
99 -- either case, the nodes are rehashed from the old buckets array onto the
100 -- new buckets array (Hash is called once for each existing key in order to
101 -- compute the new index), and then the old buckets array is deallocated.
103 function Length (Container : Map) return Count_Type;
104 -- Returns the number of items in the map
106 function Is_Empty (Container : Map) return Boolean;
107 -- Equivalent to Length (Container) = 0
109 procedure Clear (Container : in out Map);
110 -- Removes all of the items from the map
112 function Key (Position : Cursor) return Key_Type;
113 -- Returns the key of the node designated by the cursor
115 function Element (Position : Cursor) return Element_Type;
116 -- Returns the element of the node designated by the cursor
118 procedure Replace_Element
119 (Container : in out Map;
121 New_Item : Element_Type);
122 -- Assigns the value New_Item to the element designated by the cursor
124 procedure Query_Element
126 Process : not null access
127 procedure (Key : Key_Type; Element : Element_Type));
128 -- Calls Process with the key and element (both having only a constant
129 -- view) of the node designed by the cursor.
131 procedure Update_Element
132 (Container : in out Map;
134 Process : not null access
135 procedure (Key : Key_Type; Element : in out Element_Type));
136 -- Calls Process with the key (with only a constant view) and element (with
137 -- a variable view) of the node designed by the cursor.
139 type Constant_Reference_Type
140 (Element : not null access constant Element_Type) is private
142 Implicit_Dereference => Element;
144 type Reference_Type (Element : not null access Element_Type) is private
146 Implicit_Dereference => Element;
148 function Constant_Reference
149 (Container : aliased Map;
150 Position : Cursor) return Constant_Reference_Type;
153 (Container : aliased in out Map;
154 Position : Cursor) return Reference_Type;
156 function Constant_Reference
157 (Container : aliased Map;
158 Key : Key_Type) return Constant_Reference_Type;
161 (Container : aliased in out Map;
162 Key : Key_Type) return Reference_Type;
164 procedure Assign (Target : in out Map; Source : Map);
166 function Copy (Source : Map; Capacity : Count_Type := 0) return Map;
168 procedure Move (Target : in out Map; Source : in out Map);
169 -- Clears Target (if it's not empty), and then moves (not copies) the
170 -- buckets array and nodes from Source to Target.
173 (Container : in out Map;
175 New_Item : Element_Type;
176 Position : out Cursor;
177 Inserted : out Boolean);
178 -- Conditionally inserts New_Item into the map. If Key is already in the
179 -- map, then Inserted returns False and Position designates the node
180 -- containing the existing key/element pair (neither of which is modified).
181 -- If Key is not already in the map, the Inserted returns True and Position
182 -- designates the newly-inserted node container Key and New_Item. The
183 -- search for the key works as follows. Hash is called to determine Key's
184 -- bucket; if the bucket is non-empty, then Equivalent_Keys is called to
185 -- compare Key to each node in that bucket. If the bucket is empty, or
186 -- there were no matching keys in the bucket, the search "fails" and the
187 -- key/item pair is inserted in the map (and Inserted returns True);
188 -- otherwise, the search "succeeds" (and Inserted returns False).
191 (Container : in out Map;
193 Position : out Cursor;
194 Inserted : out Boolean);
195 -- The same as the (conditional) Insert that accepts an element parameter,
196 -- with the difference that if Inserted returns True, then the element of
197 -- the newly-inserted node is initialized to its default value.
200 (Container : in out Map;
202 New_Item : Element_Type);
203 -- Attempts to insert Key into the map, performing the usual search (which
204 -- involves calling both Hash and Equivalent_Keys); if the search succeeds
205 -- (because Key is already in the map), then it raises Constraint_Error.
206 -- (This version of Insert is similar to Replace, but having the opposite
207 -- exception behavior. It is intended for use when you want to assert that
208 -- Key is not already in the map.)
211 (Container : in out Map;
213 New_Item : Element_Type);
214 -- Attempts to insert Key into the map. If Key is already in the map, then
215 -- both the existing key and element are assigned the values of Key and
216 -- New_Item, respectively. (This version of Insert only raises an exception
217 -- if cursor tampering occurs. It is intended for use when you want to
218 -- insert the key/element pair in the map, and you don't care whether Key
219 -- is already present.)
222 (Container : in out Map;
224 New_Item : Element_Type);
225 -- Searches for Key in the map; if the search fails (because Key was not in
226 -- the map), then it raises Constraint_Error. Otherwise, both the existing
227 -- key and element are assigned the values of Key and New_Item rsp. (This
228 -- is similar to Insert, but with the opposite exception behavior. It is to
229 -- be used when you want to assert that Key is already in the map.)
231 procedure Exclude (Container : in out Map; Key : Key_Type);
232 -- Searches for Key in the map, and if found, removes its node from the map
233 -- and then deallocates it. The search works as follows. The operation
234 -- calls Hash to determine the key's bucket; if the bucket is not empty, it
235 -- calls Equivalent_Keys to compare Key to each key in the bucket. (This is
236 -- the deletion analog of Include. It is intended for use when you want to
237 -- remove the item from the map, but don't care whether the key is already
240 procedure Delete (Container : in out Map; Key : Key_Type);
241 -- Searches for Key in the map (which involves calling both Hash and
242 -- Equivalent_Keys). If the search fails, then the operation raises
243 -- Constraint_Error. Otherwise it removes the node from the map and then
244 -- deallocates it. (This is the deletion analog of non-conditional
245 -- Insert. It is intended for use when you want to assert that the item is
246 -- already in the map.)
248 procedure Delete (Container : in out Map; Position : in out Cursor);
249 -- Removes the node designated by Position from the map, and then
250 -- deallocates the node. The operation calls Hash to determine the bucket,
251 -- and then compares Position to each node in the bucket until there's a
252 -- match (it does not call Equivalent_Keys).
254 function First (Container : Map) return Cursor;
255 -- Returns a cursor that designates the first non-empty bucket, by
256 -- searching from the beginning of the buckets array.
258 function Next (Position : Cursor) return Cursor;
259 -- Returns a cursor that designates the node that follows the current one
260 -- designated by Position. If Position designates the last node in its
261 -- bucket, the operation calls Hash to compute the index of this bucket,
262 -- and searches the buckets array for the first non-empty bucket, starting
263 -- from that index; otherwise, it simply follows the link to the next node
264 -- in the same bucket.
266 procedure Next (Position : in out Cursor);
267 -- Equivalent to Position := Next (Position)
269 function Find (Container : Map; Key : Key_Type) return Cursor;
270 -- Searches for Key in the map. Find calls Hash to determine the key's
271 -- bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
272 -- Key to each key in the bucket. If the search succeeds, Find returns a
273 -- cursor designating the matching node; otherwise, it returns No_Element.
275 function Contains (Container : Map; Key : Key_Type) return Boolean;
276 -- Equivalent to Find (Container, Key) /= No_Element
278 function Element (Container : Map; Key : Key_Type) return Element_Type;
279 -- Equivalent to Element (Find (Container, Key))
281 function Equivalent_Keys (Left, Right : Cursor) return Boolean;
282 -- Returns the result of calling Equivalent_Keys with the keys of the nodes
283 -- designated by cursors Left and Right.
285 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
286 -- Returns the result of calling Equivalent_Keys with key of the node
287 -- designated by Left and key Right.
289 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
290 -- Returns the result of calling Equivalent_Keys with key Left and the node
291 -- designated by Right.
295 Process : not null access procedure (Position : Cursor));
296 -- Calls Process for each node in the map
299 (Container : Map) return Map_Iterator_Interfaces.Forward_Iterator'class;
303 pragma Inline (Length);
304 pragma Inline (Is_Empty);
305 pragma Inline (Clear);
307 pragma Inline (Element);
308 pragma Inline (Move);
309 pragma Inline (Contains);
310 pragma Inline (Capacity);
311 pragma Inline (Reserve_Capacity);
312 pragma Inline (Has_Element);
313 pragma Inline (Equivalent_Keys);
314 pragma Inline (Next);
317 type Node_Access is access Node_Type;
319 type Node_Type is limited record
321 Element : aliased Element_Type;
326 new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
328 type Map is new Ada.Finalization.Controlled with record
329 HT : HT_Types.Hash_Table_Type;
332 overriding procedure Adjust (Container : in out Map);
334 overriding procedure Finalize (Container : in out Map);
337 use Ada.Finalization;
341 (Stream : not null access Root_Stream_Type'Class;
344 for Map'Write use Write;
347 (Stream : not null access Root_Stream_Type'Class;
348 Container : out Map);
350 for Map'Read use Read;
352 type Map_Access is access all Map;
353 for Map_Access'Storage_Size use 0;
355 type Cursor is record
356 Container : Map_Access;
361 (Stream : not null access Root_Stream_Type'Class;
364 for Cursor'Read use Read;
367 (Stream : not null access Root_Stream_Type'Class;
370 for Cursor'Write use Write;
372 type Constant_Reference_Type
373 (Element : not null access constant Element_Type) is null record;
376 (Stream : not null access Root_Stream_Type'Class;
377 Item : Constant_Reference_Type);
379 for Constant_Reference_Type'Write use Write;
382 (Stream : not null access Root_Stream_Type'Class;
383 Item : out Constant_Reference_Type);
385 for Constant_Reference_Type'Read use Read;
388 (Element : not null access Element_Type) is null record;
391 (Stream : not null access Root_Stream_Type'Class;
392 Item : Reference_Type);
394 for Reference_Type'Write use Write;
397 (Stream : not null access Root_Stream_Type'Class;
398 Item : out Reference_Type);
400 for Reference_Type'Read use Read;
402 Empty_Map : constant Map := (Controlled with HT => (null, 0, 0, 0));
404 No_Element : constant Cursor := (Container => null, Node => null);
406 end Ada.Containers.Hashed_Maps;