annotate gcc/ada/libgnat/g-dynhta.ads @ 131:84e7813d76e9

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