Debian Codegraph @

gcc/ada/libgnat/s-htable.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- S Y S T E M . H T A B L E --
6 -- --
7 -- B o d y --
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 with Ada.Unchecked_Deallocation;
33 with System.String_Hash;
34
35 package body System.HTable is
36
37 -------------------
38 -- Static_HTable --
39 -------------------
40
41 package body Static_HTable is
42
43 Table : array (Header_Num) of Elmt_Ptr;
44
45 Iterator_Index : Header_Num;
46 Iterator_Ptr : Elmt_Ptr;
47 Iterator_Started : Boolean := False;
48
49 function Get_Non_Null return Elmt_Ptr;
50 -- Returns Null_Ptr if Iterator_Started is false or the Table is empty.
51 -- Returns Iterator_Ptr if non null, or the next non null element in
52 -- table if any.
53
54 ---------
55 -- Get --
56 ---------
57
58 function Get (K : Key) return Elmt_Ptr is
59 Elmt : Elmt_Ptr;
60
61 begin
62 Elmt := Table (Hash (K));
63 loop
64 if Elmt = Null_Ptr then
65 return Null_Ptr;
66
67 elsif Equal (Get_Key (Elmt), K) then
68 return Elmt;
69
70 else
71 Elmt := Next (Elmt);
72 end if;
73 end loop;
74 end Get;
75
76 ---------------
77 -- Get_First --
78 ---------------
79
80 function Get_First return Elmt_Ptr is
81 begin
82 Iterator_Started := True;
83 Iterator_Index := Table'First;
84 Iterator_Ptr := Table (Iterator_Index);
85 return Get_Non_Null;
86 end Get_First;
87
88 --------------
89 -- Get_Next --
90 --------------
91
92 function Get_Next return Elmt_Ptr is
93 begin
94 if not Iterator_Started then
95 return Null_Ptr;
96 else
97 Iterator_Ptr := Next (Iterator_Ptr);
98 return Get_Non_Null;
99 end if;
100 end Get_Next;
101
102 ------------------
103 -- Get_Non_Null --
104 ------------------
105
106 function Get_Non_Null return Elmt_Ptr is
107 begin
108 while Iterator_Ptr = Null_Ptr loop
109 if Iterator_Index = Table'Last then
110 Iterator_Started := False;
111 return Null_Ptr;
112 end if;
113
114 Iterator_Index := Iterator_Index + 1;
115 Iterator_Ptr := Table (Iterator_Index);
116 end loop;
117
118 return Iterator_Ptr;
119 end Get_Non_Null;
120
121 -------------
122 -- Present --
123 -------------
124
125 function Present (K : Key) return Boolean is
126 begin
127 return Get (K) /= Null_Ptr;
128 end Present;
129
130 ------------
131 -- Remove --
132 ------------
133
134 procedure Remove (K : Key) is
135 Index : constant Header_Num := Hash (K);
136 Elmt : Elmt_Ptr;
137 Next_Elmt : Elmt_Ptr;
138
139 begin
140 Elmt := Table (Index);
141
142 if Elmt = Null_Ptr then
143 return;
144
145 elsif Equal (Get_Key (Elmt), K) then
146 Table (Index) := Next (Elmt);
147
148 else
149 loop
150 Next_Elmt := Next (Elmt);
151
152 if Next_Elmt = Null_Ptr then
153 return;
154
155 elsif Equal (Get_Key (Next_Elmt), K) then
156 Set_Next (Elmt, Next (Next_Elmt));
157 return;
158
159 else
160 Elmt := Next_Elmt;
161 end if;
162 end loop;
163 end if;
164 end Remove;
165
166 -----------
167 -- Reset --
168 -----------
169
170 procedure Reset is
171 begin
172 -- Use an aggregate for efficiency reasons
173
174 Table := [others => Null_Ptr];
175 end Reset;
176
177 ---------
178 -- Set --
179 ---------
180
181 procedure Set (E : Elmt_Ptr) is
182 Index : Header_Num;
183 begin
184 Index := Hash (Get_Key (E));
185 Set_Next (E, Table (Index));
186 Table (Index) := E;
187 end Set;
188
189 ------------------------
190 -- Set_If_Not_Present --
191 ------------------------
192
193 function Set_If_Not_Present (E : Elmt_Ptr) return Boolean is
194 K : Key renames Get_Key (E);
195 -- Note that it is important to use a renaming here rather than
196 -- define a constant initialized by the call, because the latter
197 -- construct runs into bootstrap problems with earlier versions
198 -- of the GNAT compiler.
199
200 Index : constant Header_Num := Hash (K);
201 Elmt : Elmt_Ptr;
202
203 begin
204 Elmt := Table (Index);
205 loop
206 if Elmt = Null_Ptr then
207 Set_Next (E, Table (Index));
208 Table (Index) := E;
209 return True;
210
211 elsif Equal (Get_Key (Elmt), K) then
212 return False;
213
214 else
215 Elmt := Next (Elmt);
216 end if;
217 end loop;
218 end Set_If_Not_Present;
219
220 end Static_HTable;
221
222 -------------------
223 -- Simple_HTable --
224 -------------------
225
226 package body Simple_HTable is
227
228 type Element_Wrapper;
229 type Elmt_Ptr is access all Element_Wrapper;
230 type Element_Wrapper is record
231 K : Key;
232 E : Element;
233 Next : Elmt_Ptr;
234 end record;
235
236 procedure Free is new
237 Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
238
239 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
240 function Next (E : Elmt_Ptr) return Elmt_Ptr;
241 function Get_Key (E : Elmt_Ptr) return Key;
242
243 package Tab is new Static_HTable (
244 Header_Num => Header_Num,
245 Element => Element_Wrapper,
246 Elmt_Ptr => Elmt_Ptr,
247 Null_Ptr => null,
248 Set_Next => Set_Next,
249 Next => Next,
250 Key => Key,
251 Get_Key => Get_Key,
252 Hash => Hash,
253 Equal => Equal);
254
255 ---------
256 -- Get --
257 ---------
258
259 function Get (K : Key) return Element is
260 Tmp : constant Elmt_Ptr := Tab.Get (K);
261 begin
262 if Tmp = null then
263 return No_Element;
264 else
265 return Tmp.E;
266 end if;
267 end Get;
268
269 ---------------
270 -- Get_First --
271 ---------------
272
273 function Get_First return Element is
274 Tmp : constant Elmt_Ptr := Tab.Get_First;
275 begin
276 if Tmp = null then
277 return No_Element;
278 else
279 return Tmp.E;
280 end if;
281 end Get_First;
282
283 procedure Get_First (K : in out Key; E : out Element) is
284 Tmp : constant Elmt_Ptr := Tab.Get_First;
285 begin
286 if Tmp = null then
287 E := No_Element;
288 else
289 K := Tmp.K;
290 E := Tmp.E;
291 end if;
292 end Get_First;
293
294 -------------
295 -- Get_Key --
296 -------------
297
298 function Get_Key (E : Elmt_Ptr) return Key is
299 begin
300 return E.K;
301 end Get_Key;
302
303 --------------
304 -- Get_Next --
305 --------------
306
307 function Get_Next return Element is
308 Tmp : constant Elmt_Ptr := Tab.Get_Next;
309 begin
310 if Tmp = null then
311 return No_Element;
312 else
313 return Tmp.E;
314 end if;
315 end Get_Next;
316
317 procedure Get_Next (K : in out Key; E : out Element) is
318 Tmp : constant Elmt_Ptr := Tab.Get_Next;
319 begin
320 if Tmp = null then
321 E := No_Element;
322 else
323 K := Tmp.K;
324 E := Tmp.E;
325 end if;
326 end Get_Next;
327
328 ----------
329 -- Next --
330 ----------
331
332 function Next (E : Elmt_Ptr) return Elmt_Ptr is
333 begin
334 return E.Next;
335 end Next;
336
337 ------------
338 -- Remove --
339 ------------
340
341 procedure Remove (K : Key) is
342 Tmp : Elmt_Ptr;
343
344 begin
345 Tmp := Tab.Get (K);
346
347 if Tmp /= null then
348 Tab.Remove (K);
349 Free (Tmp);
350 end if;
351 end Remove;
352
353 -----------
354 -- Reset --
355 -----------
356
357 procedure Reset is
358 E1, E2 : Elmt_Ptr;
359
360 begin
361 E1 := Tab.Get_First;
362 while E1 /= null loop
363 E2 := Tab.Get_Next;
364 Free (E1);
365 E1 := E2;
366 end loop;
367
368 Tab.Reset;
369 end Reset;
370
371 ---------
372 -- Set --
373 ---------
374
375 procedure Set (K : Key; E : Element) is
376 Tmp : constant Elmt_Ptr := Tab.Get (K);
377 begin
378 if Tmp = null then
379 Tab.Set (new Element_Wrapper'(K, E, null));
380 else
381 Tmp.E := E;
382 end if;
383 end Set;
384
385 --------------
386 -- Set_Next --
387 --------------
388
389 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
390 begin
391 E.Next := Next;
392 end Set_Next;
393 end Simple_HTable;
394
395 ----------
396 -- Hash --
397 ----------
398
399 function Hash (Key : String) return Header_Num is
400 type Uns is mod 2 ** 32;
401
402 function Hash_Fun is
403 new System.String_Hash.Hash (Character, String, Uns);
404
405 begin
406 return Header_Num'First +
407 Header_Num'Base (Hash_Fun (Key) mod Header_Num'Range_Length);
408 end Hash;
409
410 end System.HTable;