Debian Codegraph @

gcc/ada/libgnat/a-cbhama.ads

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