1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . H T A B L E --
9 -- Copyright (C) 1995-2011, AdaCore --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
34 with Ada.Unchecked_Deallocation;
35 with System.String_Hash;
37 package body System.HTable is
43 package body Static_HTable is
45 Table : array (Header_Num) of Elmt_Ptr;
47 Iterator_Index : Header_Num;
48 Iterator_Ptr : Elmt_Ptr;
49 Iterator_Started : Boolean := False;
51 function Get_Non_Null return Elmt_Ptr;
52 -- Returns Null_Ptr if Iterator_Started is false or the Table is empty.
53 -- Returns Iterator_Ptr if non null, or the next non null element in
60 function Get (K : Key) return Elmt_Ptr is
64 Elmt := Table (Hash (K));
67 if Elmt = Null_Ptr then
70 elsif Equal (Get_Key (Elmt), K) then
83 function Get_First return Elmt_Ptr is
85 Iterator_Started := True;
86 Iterator_Index := Table'First;
87 Iterator_Ptr := Table (Iterator_Index);
95 function Get_Next return Elmt_Ptr is
97 if not Iterator_Started then
101 Iterator_Ptr := Next (Iterator_Ptr);
109 function Get_Non_Null return Elmt_Ptr is
111 while Iterator_Ptr = Null_Ptr loop
112 if Iterator_Index = Table'Last then
113 Iterator_Started := False;
117 Iterator_Index := Iterator_Index + 1;
118 Iterator_Ptr := Table (Iterator_Index);
128 function Present (K : Key) return Boolean is
130 return Get (K) /= Null_Ptr;
137 procedure Remove (K : Key) is
138 Index : constant Header_Num := Hash (K);
140 Next_Elmt : Elmt_Ptr;
143 Elmt := Table (Index);
145 if Elmt = Null_Ptr then
148 elsif Equal (Get_Key (Elmt), K) then
149 Table (Index) := Next (Elmt);
153 Next_Elmt := Next (Elmt);
155 if Next_Elmt = Null_Ptr then
158 elsif Equal (Get_Key (Next_Elmt), K) then
159 Set_Next (Elmt, Next (Next_Elmt));
175 for J in Table'Range loop
176 Table (J) := Null_Ptr;
184 procedure Set (E : Elmt_Ptr) is
188 Index := Hash (Get_Key (E));
189 Set_Next (E, Table (Index));
193 ------------------------
194 -- Set_If_Not_Present --
195 ------------------------
197 function Set_If_Not_Present (E : Elmt_Ptr) return Boolean is
198 K : Key renames Get_Key (E);
199 -- Note that it is important to use a renaming here rather than
200 -- define a constant initialized by the call, because the latter
201 -- construct runs into bootstrap problems with earlier versions
202 -- of the GNAT compiler.
204 Index : constant Header_Num := Hash (K);
208 Elmt := Table (Index);
210 if Elmt = Null_Ptr then
211 Set_Next (E, Table (Index));
215 elsif Equal (Get_Key (Elmt), K) then
222 end Set_If_Not_Present;
230 package body Simple_HTable is
232 type Element_Wrapper;
233 type Elmt_Ptr is access all Element_Wrapper;
234 type Element_Wrapper is record
240 procedure Free is new
241 Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
243 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
244 function Next (E : Elmt_Ptr) return Elmt_Ptr;
245 function Get_Key (E : Elmt_Ptr) return Key;
247 package Tab is new Static_HTable (
248 Header_Num => Header_Num,
249 Element => Element_Wrapper,
250 Elmt_Ptr => Elmt_Ptr,
252 Set_Next => Set_Next,
263 function Get (K : Key) return Element is
264 Tmp : constant Elmt_Ptr := Tab.Get (K);
277 function Get_First return Element is
278 Tmp : constant Elmt_Ptr := Tab.Get_First;
287 procedure Get_First (K : in out Key; E : out Element) is
288 Tmp : constant Elmt_Ptr := Tab.Get_First;
302 function Get_Key (E : Elmt_Ptr) return Key is
311 function Get_Next return Element is
312 Tmp : constant Elmt_Ptr := Tab.Get_Next;
321 procedure Get_Next (K : in out Key; E : out Element) is
322 Tmp : constant Elmt_Ptr := Tab.Get_Next;
336 function Next (E : Elmt_Ptr) return Elmt_Ptr is
345 procedure Remove (K : Key) is
366 while E1 /= null loop
379 procedure Set (K : Key; E : Element) is
380 Tmp : constant Elmt_Ptr := Tab.Get (K);
383 Tab.Set (new Element_Wrapper'(K, E, null));
393 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
403 function Hash (Key : String) return Header_Num is
404 type Uns is mod 2 ** 32;
407 new System.String_Hash.Hash (Character, String, Uns);
410 return Header_Num'First +
411 Header_Num'Base (Hash_Fun (Key) mod Header_Num'Range_Length);