111
|
1 ------------------------------------------------------------------------------
|
|
2 -- --
|
|
3 -- GNAT LIBRARY COMPONENTS --
|
|
4 -- --
|
|
5 -- ADA.CONTAINERS.INDEFINITE_HASHED_MAPS --
|
|
6 -- --
|
|
7 -- S p e c --
|
|
8 -- --
|
145
|
9 -- Copyright (C) 2004-2019, Free Software Foundation, Inc. --
|
111
|
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 with Ada.Iterator_Interfaces;
|
|
35
|
|
36 private with Ada.Containers.Hash_Tables;
|
|
37 private with Ada.Finalization;
|
|
38 private with Ada.Streams;
|
|
39
|
|
40 generic
|
|
41 type Key_Type (<>) is private;
|
|
42 type Element_Type (<>) is private;
|
|
43
|
|
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 <>;
|
|
47
|
|
48 package Ada.Containers.Indefinite_Hashed_Maps is
|
|
49 pragma Annotate (CodePeer, Skip_Analysis);
|
|
50 pragma Preelaborate;
|
|
51 pragma Remote_Types;
|
|
52
|
|
53 type Map is tagged private with
|
|
54 Constant_Indexing => Constant_Reference,
|
|
55 Variable_Indexing => Reference,
|
|
56 Default_Iterator => Iterate,
|
|
57 Iterator_Element => Element_Type;
|
|
58
|
|
59 pragma Preelaborable_Initialization (Map);
|
|
60
|
|
61 type Cursor is private;
|
|
62 pragma Preelaborable_Initialization (Cursor);
|
|
63
|
|
64 Empty_Map : constant Map;
|
|
65 -- Map objects declared without an initialization expression are
|
|
66 -- initialized to the value Empty_Map.
|
|
67
|
|
68 No_Element : constant Cursor;
|
|
69 -- Cursor objects declared without an initialization expression are
|
|
70 -- initialized to the value No_Element.
|
|
71
|
|
72 function Has_Element (Position : Cursor) return Boolean;
|
|
73 -- Equivalent to Position /= No_Element
|
|
74
|
|
75 package Map_Iterator_Interfaces is new
|
|
76 Ada.Iterator_Interfaces (Cursor, Has_Element);
|
|
77
|
|
78 overriding 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.
|
|
88
|
|
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.
|
|
92
|
|
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.
|
|
102
|
|
103 function Length (Container : Map) return Count_Type;
|
|
104 -- Returns the number of items in the map
|
|
105
|
|
106 function Is_Empty (Container : Map) return Boolean;
|
|
107 -- Equivalent to Length (Container) = 0
|
|
108
|
|
109 procedure Clear (Container : in out Map);
|
|
110 -- Removes all of the items from the map
|
|
111
|
|
112 function Key (Position : Cursor) return Key_Type;
|
|
113 -- Returns the key of the node designated by the cursor
|
|
114
|
|
115 function Element (Position : Cursor) return Element_Type;
|
|
116 -- Returns the element of the node designated by the cursor
|
|
117
|
|
118 procedure Replace_Element
|
|
119 (Container : in out Map;
|
|
120 Position : Cursor;
|
|
121 New_Item : Element_Type);
|
|
122 -- Assigns the value New_Item to the element designated by the cursor
|
|
123
|
|
124 procedure Query_Element
|
|
125 (Position : Cursor;
|
|
126 Process : not null access procedure (Key : Key_Type;
|
|
127 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.
|
|
130
|
|
131 procedure Update_Element
|
|
132 (Container : in out Map;
|
|
133 Position : Cursor;
|
|
134 Process : not null access procedure (Key : Key_Type;
|
|
135 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.
|
|
138
|
|
139 type Constant_Reference_Type
|
|
140 (Element : not null access constant Element_Type) is private
|
|
141 with
|
|
142 Implicit_Dereference => Element;
|
|
143
|
|
144 type Reference_Type (Element : not null access Element_Type) is private
|
|
145 with
|
|
146 Implicit_Dereference => Element;
|
|
147
|
|
148 function Constant_Reference
|
|
149 (Container : aliased Map;
|
|
150 Position : Cursor) return Constant_Reference_Type;
|
|
151 pragma Inline (Constant_Reference);
|
|
152
|
|
153 function Reference
|
|
154 (Container : aliased in out Map;
|
|
155 Position : Cursor) return Reference_Type;
|
|
156 pragma Inline (Reference);
|
|
157
|
|
158 function Constant_Reference
|
|
159 (Container : aliased Map;
|
|
160 Key : Key_Type) return Constant_Reference_Type;
|
|
161 pragma Inline (Constant_Reference);
|
|
162
|
|
163 function Reference
|
|
164 (Container : aliased in out Map;
|
|
165 Key : Key_Type) return Reference_Type;
|
|
166 pragma Inline (Reference);
|
|
167
|
|
168 procedure Assign (Target : in out Map; Source : Map);
|
|
169
|
|
170 function Copy (Source : Map; Capacity : Count_Type := 0) return Map;
|
|
171
|
|
172 procedure Move (Target : in out Map; Source : in out Map);
|
|
173 -- Clears Target (if it's not empty), and then moves (not copies) the
|
|
174 -- buckets array and nodes from Source to Target.
|
|
175
|
|
176 procedure Insert
|
|
177 (Container : in out Map;
|
|
178 Key : Key_Type;
|
|
179 New_Item : Element_Type;
|
|
180 Position : out Cursor;
|
|
181 Inserted : out Boolean);
|
|
182 -- Conditionally inserts New_Item into the map. If Key is already in the
|
|
183 -- map, then Inserted returns False and Position designates the node
|
|
184 -- containing the existing key/element pair (neither of which is modified).
|
|
185 -- If Key is not already in the map, the Inserted returns True and Position
|
|
186 -- designates the newly-inserted node container Key and New_Item. The
|
|
187 -- search for the key works as follows. Hash is called to determine Key's
|
|
188 -- bucket; if the bucket is non-empty, then Equivalent_Keys is called to
|
|
189 -- compare Key to each node in that bucket. If the bucket is empty, or
|
|
190 -- there were no matching keys in the bucket, the search "fails" and the
|
|
191 -- key/item pair is inserted in the map (and Inserted returns True);
|
|
192 -- otherwise, the search "succeeds" (and Inserted returns False).
|
|
193
|
|
194 procedure Insert
|
|
195 (Container : in out Map;
|
|
196 Key : Key_Type;
|
|
197 New_Item : Element_Type);
|
|
198 -- Attempts to insert Key into the map, performing the usual search (which
|
|
199 -- involves calling both Hash and Equivalent_Keys); if the search succeeds
|
|
200 -- (because Key is already in the map), then it raises Constraint_Error.
|
|
201 -- (This version of Insert is similar to Replace, but having the opposite
|
|
202 -- exception behavior. It is intended for use when you want to assert that
|
|
203 -- Key is not already in the map.)
|
|
204
|
|
205 procedure Include
|
|
206 (Container : in out Map;
|
|
207 Key : Key_Type;
|
|
208 New_Item : Element_Type);
|
|
209 -- Attempts to insert Key into the map. If Key is already in the map, then
|
|
210 -- both the existing key and element are assigned the values of Key and
|
|
211 -- New_Item, respectively. (This version of Insert only raises an exception
|
|
212 -- if cursor tampering occurs. It is intended for use when you want to
|
|
213 -- insert the key/element pair in the map, and you don't care whether Key
|
|
214 -- is already present.)
|
|
215
|
|
216 procedure Replace
|
|
217 (Container : in out Map;
|
|
218 Key : Key_Type;
|
|
219 New_Item : Element_Type);
|
|
220 -- Searches for Key in the map; if the search fails (because Key was not in
|
|
221 -- the map), then it raises Constraint_Error. Otherwise, both the existing
|
|
222 -- key and element are assigned the values of Key and New_Item rsp. (This
|
|
223 -- is similar to Insert, but with the opposite exception behavior. It is
|
|
224 -- intended for use when you want to assert that Key is already in the
|
|
225 -- map.)
|
|
226
|
|
227 procedure Exclude (Container : in out Map; Key : Key_Type);
|
|
228 -- Searches for Key in the map, and if found, removes its node from the map
|
|
229 -- and then deallocates it. The search works as follows. The operation
|
|
230 -- calls Hash to determine the key's bucket; if the bucket is not empty, it
|
|
231 -- calls Equivalent_Keys to compare Key to each key in the bucket. (This is
|
|
232 -- the deletion analog of Include. It is intended for use when you want to
|
|
233 -- remove the item from the map, but don't care whether the key is already
|
|
234 -- in the map.)
|
|
235
|
|
236 procedure Delete (Container : in out Map; Key : Key_Type);
|
|
237 -- Searches for Key in the map (which involves calling both Hash and
|
|
238 -- Equivalent_Keys). If the search fails, then the operation raises
|
|
239 -- Constraint_Error. Otherwise it removes the node from the map and then
|
|
240 -- deallocates it. (This is the deletion analog of non-conditional
|
|
241 -- Insert. It is intended for use when you want to assert that the item is
|
|
242 -- already in the map.)
|
|
243
|
|
244 procedure Delete (Container : in out Map; Position : in out Cursor);
|
|
245 -- Removes the node designated by Position from the map, and then
|
|
246 -- deallocates the node. The operation calls Hash to determine the bucket,
|
|
247 -- and then compares Position to each node in the bucket until there's a
|
|
248 -- match (it does not call Equivalent_Keys).
|
|
249
|
|
250 function First (Container : Map) return Cursor;
|
|
251 -- Returns a cursor that designates the first non-empty bucket, by
|
|
252 -- searching from the beginning of the buckets array.
|
|
253
|
|
254 function Next (Position : Cursor) return Cursor;
|
|
255 -- Returns a cursor that designates the node that follows the current one
|
|
256 -- designated by Position. If Position designates the last node in its
|
|
257 -- bucket, the operation calls Hash to compute the index of this bucket,
|
|
258 -- and searches the buckets array for the first non-empty bucket, starting
|
|
259 -- from that index; otherwise, it simply follows the link to the next node
|
|
260 -- in the same bucket.
|
|
261
|
|
262 procedure Next (Position : in out Cursor);
|
|
263 -- Equivalent to Position := Next (Position)
|
|
264
|
|
265 function Find (Container : Map; Key : Key_Type) return Cursor;
|
|
266 -- Searches for Key in the map. Find calls Hash to determine the key's
|
|
267 -- bucket; if the bucket is not empty, it calls Equivalent_Keys to compare
|
|
268 -- Key to each key in the bucket. If the search succeeds, Find returns a
|
|
269 -- cursor designating the matching node; otherwise, it returns No_Element.
|
|
270
|
|
271 function Contains (Container : Map; Key : Key_Type) return Boolean;
|
|
272 -- Equivalent to Find (Container, Key) /= No_Element
|
|
273
|
|
274 function Element (Container : Map; Key : Key_Type) return Element_Type;
|
|
275 -- Equivalent to Element (Find (Container, Key))
|
|
276
|
|
277 function Equivalent_Keys (Left, Right : Cursor) return Boolean;
|
|
278 -- Returns the result of calling Equivalent_Keys with the keys of the nodes
|
|
279 -- designated by cursors Left and Right.
|
|
280
|
|
281 function Equivalent_Keys (Left : Cursor; Right : Key_Type) return Boolean;
|
|
282 -- Returns the result of calling Equivalent_Keys with key of the node
|
|
283 -- designated by Left and key Right.
|
|
284
|
|
285 function Equivalent_Keys (Left : Key_Type; Right : Cursor) return Boolean;
|
|
286 -- Returns the result of calling Equivalent_Keys with key Left and the node
|
|
287 -- designated by Right.
|
|
288
|
|
289 procedure Iterate
|
|
290 (Container : Map;
|
|
291 Process : not null access procedure (Position : Cursor));
|
|
292 -- Calls Process for each node in the map
|
|
293
|
|
294 function Iterate (Container : Map)
|
|
295 return Map_Iterator_Interfaces.Forward_Iterator'class;
|
|
296
|
|
297 private
|
|
298 pragma Inline ("=");
|
|
299 pragma Inline (Length);
|
|
300 pragma Inline (Is_Empty);
|
|
301 pragma Inline (Clear);
|
|
302 pragma Inline (Key);
|
|
303 pragma Inline (Element);
|
|
304 pragma Inline (Move);
|
|
305 pragma Inline (Contains);
|
|
306 pragma Inline (Capacity);
|
|
307 pragma Inline (Reserve_Capacity);
|
|
308 pragma Inline (Has_Element);
|
|
309 pragma Inline (Equivalent_Keys);
|
|
310 pragma Inline (Next);
|
|
311
|
|
312 type Node_Type;
|
|
313 type Node_Access is access Node_Type;
|
|
314
|
|
315 type Key_Access is access Key_Type;
|
|
316 type Element_Access is access all Element_Type;
|
|
317
|
|
318 type Node_Type is limited record
|
|
319 Key : Key_Access;
|
|
320 Element : Element_Access;
|
|
321 Next : Node_Access;
|
|
322 end record;
|
|
323
|
|
324 package HT_Types is
|
|
325 new Hash_Tables.Generic_Hash_Table_Types (Node_Type, Node_Access);
|
|
326
|
|
327 type Map is new Ada.Finalization.Controlled with record
|
|
328 HT : HT_Types.Hash_Table_Type;
|
|
329 end record;
|
|
330
|
|
331 overriding procedure Adjust (Container : in out Map);
|
|
332
|
|
333 overriding procedure Finalize (Container : in out Map);
|
|
334
|
|
335 use HT_Types, HT_Types.Implementation;
|
|
336 use Ada.Finalization;
|
|
337 use Ada.Streams;
|
|
338
|
|
339 procedure Write
|
|
340 (Stream : not null access Root_Stream_Type'Class;
|
|
341 Container : Map);
|
|
342
|
|
343 for Map'Write use Write;
|
|
344
|
|
345 procedure Read
|
|
346 (Stream : not null access Root_Stream_Type'Class;
|
|
347 Container : out Map);
|
|
348
|
|
349 for Map'Read use Read;
|
|
350
|
|
351 type Map_Access is access all Map;
|
|
352 for Map_Access'Storage_Size use 0;
|
|
353
|
|
354 type Cursor is record
|
|
355 Container : Map_Access;
|
|
356 Node : Node_Access;
|
|
357 Position : Hash_Type := Hash_Type'Last;
|
|
358 end record;
|
|
359
|
|
360 procedure Write
|
|
361 (Stream : not null access Root_Stream_Type'Class;
|
|
362 Item : Cursor);
|
|
363
|
|
364 for Cursor'Write use Write;
|
|
365
|
|
366 procedure Read
|
|
367 (Stream : not null access Root_Stream_Type'Class;
|
|
368 Item : out Cursor);
|
|
369
|
|
370 for Cursor'Read use Read;
|
|
371
|
|
372 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
|
|
373 -- It is necessary to rename this here, so that the compiler can find it
|
|
374
|
|
375 type Constant_Reference_Type
|
|
376 (Element : not null access constant Element_Type) is
|
|
377 record
|
|
378 Control : Reference_Control_Type :=
|
|
379 raise Program_Error with "uninitialized reference";
|
|
380 -- The RM says, "The default initialization of an object of
|
|
381 -- type Constant_Reference_Type or Reference_Type propagates
|
|
382 -- Program_Error."
|
|
383 end record;
|
|
384
|
|
385 procedure Write
|
|
386 (Stream : not null access Root_Stream_Type'Class;
|
|
387 Item : Constant_Reference_Type);
|
|
388
|
|
389 for Constant_Reference_Type'Write use Write;
|
|
390
|
|
391 procedure Read
|
|
392 (Stream : not null access Root_Stream_Type'Class;
|
|
393 Item : out Constant_Reference_Type);
|
|
394
|
|
395 for Constant_Reference_Type'Read use Read;
|
|
396
|
|
397 type Reference_Type
|
|
398 (Element : not null access Element_Type) is
|
|
399 record
|
|
400 Control : Reference_Control_Type :=
|
|
401 raise Program_Error with "uninitialized reference";
|
|
402 -- The RM says, "The default initialization of an object of
|
|
403 -- type Constant_Reference_Type or Reference_Type propagates
|
|
404 -- Program_Error."
|
|
405 end record;
|
|
406
|
|
407 procedure Write
|
|
408 (Stream : not null access Root_Stream_Type'Class;
|
|
409 Item : Reference_Type);
|
|
410
|
|
411 for Reference_Type'Write use Write;
|
|
412
|
|
413 procedure Read
|
|
414 (Stream : not null access Root_Stream_Type'Class;
|
|
415 Item : out Reference_Type);
|
|
416
|
|
417 for Reference_Type'Read use Read;
|
|
418
|
|
419 -- Three operations are used to optimize in the expansion of "for ... of"
|
|
420 -- loops: the Next(Cursor) procedure in the visible part, and the following
|
|
421 -- Pseudo_Reference and Get_Element_Access functions. See Sem_Ch5 for
|
|
422 -- details.
|
|
423
|
|
424 function Pseudo_Reference
|
|
425 (Container : aliased Map'Class) return Reference_Control_Type;
|
|
426 pragma Inline (Pseudo_Reference);
|
|
427 -- Creates an object of type Reference_Control_Type pointing to the
|
|
428 -- container, and increments the Lock. Finalization of this object will
|
|
429 -- decrement the Lock.
|
|
430
|
|
431 function Get_Element_Access
|
|
432 (Position : Cursor) return not null Element_Access;
|
|
433 -- Returns a pointer to the element designated by Position.
|
|
434
|
|
435 Empty_Map : constant Map := (Controlled with others => <>);
|
|
436
|
|
437 No_Element : constant Cursor :=
|
|
438 (Container => null, Node => null, Position => Hash_Type'Last);
|
|
439
|
|
440 type Iterator is new Limited_Controlled and
|
|
441 Map_Iterator_Interfaces.Forward_Iterator with
|
|
442 record
|
|
443 Container : Map_Access;
|
|
444 end record
|
|
445 with Disable_Controlled => not T_Check;
|
|
446
|
|
447 overriding procedure Finalize (Object : in out Iterator);
|
|
448
|
|
449 overriding function First (Object : Iterator) return Cursor;
|
|
450
|
|
451 overriding function Next
|
|
452 (Object : Iterator;
|
|
453 Position : Cursor) return Cursor;
|
|
454
|
|
455 end Ada.Containers.Indefinite_Hashed_Maps;
|