Debian Codegraph @

gcc/ada/libgnat/g-dynhta.ads

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . D Y N A M I C _ H T A B L E S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 1995-2025, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- Hash table searching routines
33
34 -- This package contains two separate packages. The Simple_HTable package
35 -- provides a very simple abstraction that associates one element to one key
36 -- value and takes care of all allocations automatically using the heap. The
37 -- Static_HTable package provides a more complex interface that allows full
38 -- control over allocation.
39
40 -- This package provides a facility similar to that of GNAT.HTable, except
41 -- that this package declares types that can be used to define dynamic
42 -- instances of hash tables, while instantiations in GNAT.HTable creates a
43 -- single instance of the hash table.
44
45 -- Note that this interface should remain synchronized with those in
46 -- GNAT.HTable to keep as much coherency as possible between these two
47 -- related units.
48
49 -- Note: this unit is used during bootstrap, see ADA_GENERATED_FILES in
50 -- gcc-interface/Make-lang.in for details on the constraints.
51
52 package GNAT.Dynamic_HTables is
53
54 function Hash_Two_Keys
55 (Left : Bucket_Range_Type;
56 Right : Bucket_Range_Type) return Bucket_Range_Type;
57 pragma Inline (Hash_Two_Keys);
58 -- Obtain the hash value of keys Left and Right
59
60 -------------------
61 -- Static_HTable --
62 -------------------
63
64 -- A low-level Hash-Table abstraction, not as easy to instantiate as
65 -- Simple_HTable. This mirrors the interface of GNAT.HTable.Static_HTable,
66 -- but does require dynamic allocation (since we allow multiple instances
67 -- of the table). The model is that each Element contains its own Key that
68 -- can be retrieved by Get_Key. Furthermore, Element provides a link that
69 -- can be used by the HTable for linking elements with same hash codes:
70
71 -- Element
72
73 -- +-------------------+
74 -- | Key |
75 -- +-------------------+
76 -- : other data :
77 -- +-------------------+
78 -- | Next Elmt |
79 -- +-------------------+
80
81 generic
82 type Header_Num is range <>;
83 -- An integer type indicating the number and range of hash headers
84
85 type Element (<>) is limited private;
86 -- The type of element to be stored
87
88 type Elmt_Ptr is private;
89 -- The type used to reference an element (will usually be an access
90 -- type, but could be some other form of type such as an integer type).
91
92 Null_Ptr : Elmt_Ptr;
93 -- The null value of the Elmt_Ptr type
94
95 with function Next (E : Elmt_Ptr) return Elmt_Ptr;
96 with procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
97 -- The type must provide an internal link for the sake of the
98 -- staticness of the HTable.
99
100 type Key is limited private;
101 with function Get_Key (E : Elmt_Ptr) return Key;
102 with function Hash (F : Key) return Header_Num;
103 with function Equal (F1 : Key; F2 : Key) return Boolean;
104
105 package Static_HTable is
106 type Instance is private;
107 Nil : constant Instance;
108
109 procedure Reset (T : in out Instance);
110 -- Resets the hash table by releasing all memory associated with it. The
111 -- hash table can safely be reused after this call. For the most common
112 -- case where Elmt_Ptr is an access type, and Null_Ptr is null, this is
113 -- only needed if the same table is reused in a new context. If Elmt_Ptr
114 -- is other than an access type, or Null_Ptr is other than null, then
115 -- Reset must be called before the first use of the hash table.
116
117 procedure Set (T : in out Instance; E : Elmt_Ptr);
118 -- Insert the element pointer in the HTable
119
120 function Get (T : Instance; K : Key) return Elmt_Ptr;
121 -- Returns the latest inserted element pointer with the given Key or
122 -- null if none.
123
124 procedure Remove (T : Instance; K : Key);
125 -- Removes the latest inserted element pointer associated with the given
126 -- key if any, does nothing if none.
127
128 function Get_First (T : Instance) return Elmt_Ptr;
129 -- Returns Null_Ptr if the Htable is empty, otherwise returns one
130 -- unspecified element. There is no guarantee that 2 calls to this
131 -- function will return the same element.
132
133 function Get_Next (T : Instance) return Elmt_Ptr;
134 -- Returns an unspecified element that has not been returned by the same
135 -- function since the last call to Get_First or Null_Ptr if there is no
136 -- such element or Get_First has never been called. If there is no call
137 -- to 'Set' in between Get_Next calls, all the elements of the Htable
138 -- will be traversed.
139
140 private
141 type Table_Type is array (Header_Num) of Elmt_Ptr;
142
143 type Instance_Data is record
144 Table : Table_Type;
145 Iterator_Index : Header_Num;
146 Iterator_Ptr : Elmt_Ptr;
147 Iterator_Started : Boolean := False;
148 end record;
149
150 type Instance is access all Instance_Data;
151
152 Nil : constant Instance := null;
153 end Static_HTable;
154
155 -------------------
156 -- Simple_HTable --
157 -------------------
158
159 -- A simple hash table abstraction, easy to instantiate, easy to use.
160 -- The table associates one element to one key with the procedure Set.
161 -- Get retrieves the Element stored for a given Key. The efficiency of
162 -- retrieval is function of the size of the Table parameterized by
163 -- Header_Num and the hashing function Hash.
164
165 generic
166 type Header_Num is range <>;
167 -- An integer type indicating the number and range of hash headers
168
169 type Element is private;
170 -- The type of element to be stored
171
172 No_Element : Element;
173 -- The object that is returned by Get when no element has been set for
174 -- a given key
175
176 type Key is private;
177 with function Hash (F : Key) return Header_Num;
178 with function Equal (F1 : Key; F2 : Key) return Boolean;
179
180 package Simple_HTable is
181 type Instance is private;
182 Nil : constant Instance;
183
184 type Key_Option (Present : Boolean := False) is record
185 case Present is
186 when True => K : Key;
187 when False => null;
188 end case;
189 end record;
190
191 procedure Set (T : in out Instance; K : Key; E : Element);
192 -- Associates an element with a given key. Overrides any previously
193 -- associated element.
194
195 procedure Reset (T : in out Instance);
196 -- Releases all memory associated with the table. The table can be
197 -- reused after this call (it is automatically allocated on the first
198 -- access to the table).
199
200 function Get (T : Instance; K : Key) return Element;
201 -- Returns the Element associated with a key or No_Element if the given
202 -- key has not associated element
203
204 procedure Remove (T : Instance; K : Key);
205 -- Removes the latest inserted element pointer associated with the given
206 -- key if any, does nothing if none.
207
208 function Get_First (T : Instance) return Element;
209 -- Returns No_Element if the Htable is empty, otherwise returns one
210 -- unspecified element. There is no guarantee that two calls to this
211 -- function will return the same element, if the Htable has been
212 -- modified between the two calls.
213
214 function Get_First_Key (T : Instance) return Key_Option;
215 -- Returns an option type giving an unspecified key. If the Htable
216 -- is empty, the discriminant will have field Present set to False,
217 -- otherwise its Present field is set to True and the field K contains
218 -- the key. There is no guarantee that two calls to this function will
219 -- return the same key, if the Htable has been modified between the two
220 -- calls.
221
222 function Get_Next (T : Instance) return Element;
223 -- Returns an unspecified element that has not been returned by the
224 -- same function since the last call to Get_First or No_Element if
225 -- there is no such element. If there is no call to 'Set' in between
226 -- Get_Next calls, all the elements of the Htable will be traversed.
227 -- To guarantee that all the elements of the Htable will be traversed,
228 -- no modification of the Htable (Set, Reset, Remove) should occur
229 -- between a call to Get_First and subsequent consecutive calls to
230 -- Get_Next, until one of these calls returns No_Element.
231
232 function Get_Next_Key (T : Instance) return Key_Option;
233 -- Same as Get_Next except that this returns an option type having field
234 -- Present set either to False if there no key never returned before by
235 -- either Get_First_Key or this very same function, or to True if there
236 -- is one, with the field K containing the key specified as before. The
237 -- same restrictions apply as Get_Next.
238
239 private
240 type Element_Wrapper;
241 type Elmt_Ptr is access all Element_Wrapper;
242 type Element_Wrapper is record
243 K : Key;
244 E : Element;
245 Next : Elmt_Ptr;
246 end record;
247
248 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
249 function Next (E : Elmt_Ptr) return Elmt_Ptr;
250 function Get_Key (E : Elmt_Ptr) return Key;
251
252 package Tab is new Static_HTable
253 (Header_Num => Header_Num,
254 Element => Element_Wrapper,
255 Elmt_Ptr => Elmt_Ptr,
256 Null_Ptr => null,
257 Set_Next => Set_Next,
258 Next => Next,
259 Key => Key,
260 Get_Key => Get_Key,
261 Hash => Hash,
262 Equal => Equal);
263
264 type Instance is new Tab.Instance;
265 Nil : constant Instance := Instance (Tab.Nil);
266 end Simple_HTable;
267
268 -------------------------
269 -- Dynamic_Hash_Tables --
270 -------------------------
271
272 -- The following package offers a hash table abstraction with the following
273 -- characteristics:
274 --
275 -- * Dynamic resizing based on load factor
276 -- * Creation of multiple instances, of different sizes
277 -- * Iterable keys
278 --
279 -- This type of hash table is best used in scenarios where the size of the
280 -- key set is not known. The dynamic resizing aspect allows for performance
281 -- to remain within reasonable bounds as the size of the key set grows.
282 --
283 -- The following use pattern must be employed when operating this table:
284 --
285 -- Table : Dynamic_Hash_Table := Create (<some size>);
286 --
287 -- <various operations>
288 --
289 -- Destroy (Table);
290 --
291 -- The destruction of the table reclaims all storage occupied by it.
292
293 -- The following type denotes the multiplicative factor used in expansion
294 -- and compression of the hash table.
295
296 subtype Factor_Type is Bucket_Range_Type range 2 .. 100;
297
298 -- The following type denotes the threshold range used in expansion and
299 -- compression of the hash table.
300
301 subtype Threshold_Type is Long_Float range 0.0 .. Long_Float'Last;
302
303 generic
304 type Key_Type is private;
305 type Value_Type is private;
306 -- The types of the key-value pairs stored in the hash table
307
308 No_Value : Value_Type;
309 -- An indicator for a non-existent value
310
311 Expansion_Threshold : Threshold_Type;
312 Expansion_Factor : Factor_Type;
313 -- Once the load factor goes over Expansion_Threshold, the size of the
314 -- buckets is increased using the formula
315 --
316 -- New_Size = Old_Size * Expansion_Factor
317 --
318 -- An Expansion_Threshold of 1.5 and Expansion_Factor of 2 indicate that
319 -- the size of the buckets will be doubled once the load factor exceeds
320 -- 1.5.
321
322 Compression_Threshold : Threshold_Type;
323 Compression_Factor : Factor_Type;
324 -- Once the load factor drops below Compression_Threshold, the size of
325 -- the buckets is decreased using the formula
326 --
327 -- New_Size = Old_Size / Compression_Factor
328 --
329 -- A Compression_Threshold of 0.5 and Compression_Factor of 2 indicate
330 -- that the size of the buckets will be halved once the load factor
331 -- drops below 0.5.
332
333 with function "="
334 (Left : Key_Type;
335 Right : Key_Type) return Boolean;
336
337 with procedure Destroy_Value (Val : in out Value_Type);
338 -- Value destructor
339
340 with function Hash (Key : Key_Type) return Bucket_Range_Type;
341 -- Map an arbitrary key into the range of buckets
342
343 package Dynamic_Hash_Tables is
344
345 ----------------------
346 -- Table operations --
347 ----------------------
348
349 -- The following type denotes a hash table handle. Each instance must be
350 -- created using routine Create.
351
352 type Dynamic_Hash_Table is private;
353 Nil : constant Dynamic_Hash_Table;
354
355 function Contains
356 (T : Dynamic_Hash_Table;
357 Key : Key_Type) return Boolean;
358 -- Determine whether key Key exists in hash table T
359
360 function Create (Initial_Size : Positive) return Dynamic_Hash_Table;
361 -- Create a new table with bucket capacity Initial_Size. This routine
362 -- must be called at the start of a hash table's lifetime.
363
364 procedure Delete
365 (T : Dynamic_Hash_Table;
366 Key : Key_Type);
367 -- Delete the value which corresponds to key Key from hash table T. The
368 -- routine has no effect if the value is not present in the hash table.
369 -- This action will raise Iterated if the hash table has outstanding
370 -- iterators. If the load factor drops below Compression_Threshold, the
371 -- size of the buckets is decreased by Copression_Factor.
372
373 procedure Destroy (T : in out Dynamic_Hash_Table);
374 -- Destroy the contents of hash table T, rendering it unusable. This
375 -- routine must be called at the end of a hash table's lifetime. This
376 -- action will raise Iterated if the hash table has outstanding
377 -- iterators.
378
379 function Get
380 (T : Dynamic_Hash_Table;
381 Key : Key_Type) return Value_Type;
382 -- Obtain the value which corresponds to key Key from hash table T. If
383 -- the value does not exist, return No_Value.
384
385 function Is_Empty (T : Dynamic_Hash_Table) return Boolean;
386 -- Determine whether hash table T is empty
387
388 function Present (T : Dynamic_Hash_Table) return Boolean;
389 -- Determine whether hash table T exists
390
391 procedure Put
392 (T : Dynamic_Hash_Table;
393 Key : Key_Type;
394 Value : Value_Type);
395 -- Associate value Value with key Key in hash table T. If the table
396 -- already contains a mapping of the same key to a previous value, the
397 -- previous value is overwritten. This action will raise Iterated if
398 -- the hash table has outstanding iterators. If the load factor goes
399 -- over Expansion_Threshold, the size of the buckets is increased by
400 -- Expansion_Factor.
401
402 procedure Reset (T : Dynamic_Hash_Table);
403 -- Destroy the contents of hash table T, and reset it to its initial
404 -- created state. This action will raise Iterated if the hash table
405 -- has outstanding iterators.
406
407 function Size (T : Dynamic_Hash_Table) return Natural;
408 -- Obtain the number of key-value pairs in hash table T
409
410 -------------------------
411 -- Iterator operations --
412 -------------------------
413
414 -- The following type represents a key iterator. An iterator locks
415 -- all mutation operations, and unlocks them once it is exhausted.
416 -- The iterator must be used with the following pattern:
417 --
418 -- Iter := Iterate (My_Table);
419 -- while Has_Next (Iter) loop
420 -- Key := Next (Iter);
421 -- . . .
422 -- end loop;
423 --
424 -- It is possible to advance the iterator by using Next only, however
425 -- this risks raising Iterator_Exhausted.
426
427 type Iterator is private;
428
429 function Has_Next (Iter : Iterator) return Boolean;
430 -- Determine whether iterator Iter has more keys to examine. If the
431 -- iterator has been exhausted, restore all mutation functionality of
432 -- the associated hash table.
433
434 function Iterate (T : Dynamic_Hash_Table) return Iterator;
435 -- Obtain an iterator over the keys of hash table T. This action locks
436 -- all mutation functionality of the associated hash table.
437
438 procedure Next (Iter : in out Iterator; Key : out Key_Type);
439 -- Return the current key referenced by iterator Iter and advance to
440 -- the next available key. If the iterator has been exhausted and
441 -- further attempts are made to advance it, this routine restores
442 -- mutation functionality of the associated hash table, and then
443 -- raises Iterator_Exhausted.
444
445 private
446 -- The following type represents a doubly linked list node used to
447 -- store a key-value pair. There are several reasons to use a doubly
448 -- linked list:
449 --
450 -- * Most read and write operations utilize the same primitve
451 -- routines to locate, create, and delete a node, allowing for
452 -- greater degree of code sharing.
453 --
454 -- * Special cases are eliminated by maintaining a circular node
455 -- list with a dummy head (see type Bucket_Table).
456 --
457 -- A node is said to be "valid" if it is non-null, and does not refer to
458 -- the dummy head of some bucket.
459
460 type Node;
461 type Node_Ptr is access all Node;
462 type Node is record
463 Key : Key_Type;
464 Value : Value_Type := No_Value;
465 -- Key-value pair stored in a bucket
466
467 Prev : Node_Ptr := null;
468 Next : Node_Ptr := null;
469 end record;
470
471 -- The following type represents a bucket table. Each bucket contains a
472 -- circular doubly linked list of nodes with a dummy head. Initially,
473 -- the head does not refer to itself. This is intentional because it
474 -- improves the performance of creation, compression, and expansion by
475 -- avoiding a separate pass to link a head to itself. Several routines
476 -- ensure that the head is properly formed.
477
478 type Bucket_Table is array (Bucket_Range_Type range <>) of aliased Node;
479 type Bucket_Table_Ptr is access Bucket_Table;
480
481 -- The following type represents a hash table
482
483 type Dynamic_Hash_Table_Attributes is record
484 Buckets : Bucket_Table_Ptr := null;
485 -- Reference to the compressing / expanding buckets
486
487 Initial_Size : Bucket_Range_Type := 0;
488 -- The initial size of the buckets as specified at creation time
489
490 Iterators : Natural := 0;
491 -- Number of outstanding iterators
492
493 Pairs : Natural := 0;
494 -- Number of key-value pairs in the buckets
495 end record;
496
497 type Dynamic_Hash_Table is access Dynamic_Hash_Table_Attributes;
498 Nil : constant Dynamic_Hash_Table := null;
499
500 -- The following type represents a key iterator
501
502 type Iterator is record
503 Curr_Idx : Bucket_Range_Type := 0;
504 -- Index of the current bucket being examined. This index is always
505 -- kept within the range of the buckets.
506
507 Curr_Nod : Node_Ptr := null;
508 -- Reference to the current node being examined within the current
509 -- bucket. The invariant of the iterator requires that this field
510 -- always point to a valid node. A value of null indicates that the
511 -- iterator is exhausted.
512
513 Table : Dynamic_Hash_Table := null;
514 -- Reference to the associated hash table
515 end record;
516 end Dynamic_Hash_Tables;
517
518 end GNAT.Dynamic_HTables;