Debian Codegraph @

gcc/ada/libgnat/a-cihama.ads

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