1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2004-2011, 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 private with Ada
.Containers
.Hash_Tables
;
36 with Ada
.Streams
; use Ada
.Streams
;
37 with Ada
.Iterator_Interfaces
;
40 type Key_Type
is private;
41 type Element_Type
is private;
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 <>;
47 package Ada
.Containers
.Bounded_Hashed_Maps
is
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
;
57 pragma Preelaborable_Initialization
(Map
);
59 type Cursor
is private;
60 pragma Preelaborable_Initialization
(Cursor
);
62 Empty_Map
: constant Map
;
63 -- Map objects declared without an initialization expression are
64 -- initialized to the value Empty_Map.
66 No_Element
: constant Cursor
;
67 -- Cursor objects declared without an initialization expression are
68 -- initialized to the value No_Element.
70 function Has_Element
(Position
: Cursor
) return Boolean;
71 -- Equivalent to Position /= No_Element
73 package Map_Iterator_Interfaces
is new
74 Ada
.Iterator_Interfaces
(Cursor
, Has_Element
);
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.
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.
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
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).
101 function Length
(Container
: Map
) return Count_Type
;
102 -- Returns the number of items in the map
104 function Is_Empty
(Container
: Map
) return Boolean;
105 -- Equivalent to Length (Container) = 0
107 procedure Clear
(Container
: in out Map
);
108 -- Removes all of the items from the map
110 function Key
(Position
: Cursor
) return Key_Type
;
111 -- Returns the key of the node designated by the cursor
113 function Element
(Position
: Cursor
) return Element_Type
;
114 -- Returns the element of the node designated by the cursor
116 procedure Replace_Element
117 (Container
: in out Map
;
119 New_Item
: Element_Type
);
120 -- Assigns the value New_Item to the element designated by the cursor
122 procedure Query_Element
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.
129 procedure Update_Element
130 (Container
: in out Map
;
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.
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.
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.
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.
161 (Container
: in out Map
;
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).
179 (Container
: in out Map
;
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.
188 (Container
: in out Map
;
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.)
199 (Container
: in out Map
;
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.)
210 (Container
: in out Map
;
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.)
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
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.)
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).
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.
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.
254 procedure Next
(Position
: in out Cursor
);
255 -- Equivalent to Position := Next (Position)
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.
263 function Contains
(Container
: Map
; Key
: Key_Type
) return Boolean;
264 -- Equivalent to Find (Container, Key) /= No_Element
266 function Element
(Container
: Map
; Key
: Key_Type
) return Element_Type
;
267 -- Equivalent to Element (Find (Container, Key))
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.
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.
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.
283 Process
: not null access procedure (Position
: Cursor
));
284 -- Calls Process for each node in the map
286 function Iterate
(Container
: Map
)
287 return Map_Iterator_Interfaces
.Forward_Iterator
'class;
289 type Constant_Reference_Type
290 (Element
: not null access constant Element_Type
) is
293 Implicit_Dereference
=> Element
;
296 (Stream
: not null access Root_Stream_Type
'Class;
297 Item
: Constant_Reference_Type
);
299 for Constant_Reference_Type
'Write use Write
;
302 (Stream
: not null access Root_Stream_Type
'Class;
303 Item
: out Constant_Reference_Type
);
305 for Constant_Reference_Type
'Read use Read
;
307 type Reference_Type
(Element
: not null access Element_Type
) is private
309 Implicit_Dereference
=> Element
;
312 (Stream
: not null access Root_Stream_Type
'Class;
313 Item
: Reference_Type
);
315 for Reference_Type
'Write use Write
;
318 (Stream
: not null access Root_Stream_Type
'Class;
319 Item
: out Reference_Type
);
321 for Reference_Type
'Read use Read
;
323 function Constant_Reference
325 Key
: Key_Type
) -- SHOULD BE ALIASED???
326 return Constant_Reference_Type
;
328 function Reference
(Container
: Map
; Key
: Key_Type
) return Reference_Type
;
331 pragma Inline
(Length
);
332 pragma Inline
(Is_Empty
);
333 pragma Inline
(Clear
);
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
);
343 type Node_Type
is record
345 Element
: Element_Type
;
350 new Hash_Tables
.Generic_Bounded_Hash_Table_Types
(Node_Type
);
352 type Map
(Capacity
: Count_Type
; Modulus
: Hash_Type
) is
353 new HT_Types
.Hash_Table_Type
(Capacity
, Modulus
) with null record;
358 (Stream
: not null access Root_Stream_Type
'Class;
361 for Map
'Write use Write
;
364 (Stream
: not null access Root_Stream_Type
'Class;
365 Container
: out Map
);
367 for Map
'Read use Read
;
369 type Map_Access
is access all Map
;
370 for Map_Access
'Storage_Size use 0;
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.
378 type Cursor
is record
379 Container
: Map_Access
;
380 Node
: Count_Type
:= 0;
384 (Stream
: not null access Root_Stream_Type
'Class;
387 for Cursor
'Read use Read
;
390 (Stream
: not null access Root_Stream_Type
'Class;
393 for Cursor
'Write use Write
;
395 type Constant_Reference_Type
396 (Element
: not null access constant Element_Type
) is null record;
399 (Element
: not null access Element_Type
) is null record;
401 No_Element
: constant Cursor
:= (Container
=> null, Node
=> 0);
403 Empty_Map
: constant Map
:=
404 (Hash_Table_Type
with Capacity
=> 0, Modulus
=> 0);
406 end Ada
.Containers
.Bounded_Hashed_Maps
;