1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- G N A T . H T A B L E --
9 -- Copyright (C) 1995-1999 Ada Core Technologies, Inc. --
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 2, 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. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
29 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 with Ada.Unchecked_Deallocation;
34 package body GNAT.HTable is
40 package body Static_HTable is
42 Table : array (Header_Num) of Elmt_Ptr;
44 Iterator_Index : Header_Num;
45 Iterator_Ptr : Elmt_Ptr;
46 Iterator_Started : Boolean := False;
48 function Get_Non_Null return Elmt_Ptr;
49 -- Returns Null_Ptr if Iterator_Started is false of the Table is
50 -- empty. Returns Iterator_Ptr if non null, or the next non null
51 -- element in table if any.
57 function Get (K : Key) return Elmt_Ptr is
61 Elmt := Table (Hash (K));
64 if Elmt = Null_Ptr then
67 elsif Equal (Get_Key (Elmt), K) then
80 function Get_First return Elmt_Ptr is
82 Iterator_Started := True;
83 Iterator_Index := Table'First;
84 Iterator_Ptr := Table (Iterator_Index);
92 function Get_Next return Elmt_Ptr is
94 if not Iterator_Started then
98 Iterator_Ptr := Next (Iterator_Ptr);
106 function Get_Non_Null return Elmt_Ptr is
108 while Iterator_Ptr = Null_Ptr loop
109 if Iterator_Index = Table'Last then
110 Iterator_Started := False;
114 Iterator_Index := Iterator_Index + 1;
115 Iterator_Ptr := Table (Iterator_Index);
125 procedure Remove (K : Key) is
126 Index : constant Header_Num := Hash (K);
128 Next_Elmt : Elmt_Ptr;
131 Elmt := Table (Index);
133 if Elmt = Null_Ptr then
136 elsif Equal (Get_Key (Elmt), K) then
137 Table (Index) := Next (Elmt);
141 Next_Elmt := Next (Elmt);
143 if Next_Elmt = Null_Ptr then
146 elsif Equal (Get_Key (Next_Elmt), K) then
147 Set_Next (Elmt, Next (Next_Elmt));
163 for J in Table'Range loop
164 Table (J) := Null_Ptr;
172 procedure Set (E : Elmt_Ptr) is
176 Index := Hash (Get_Key (E));
177 Set_Next (E, Table (Index));
187 package body Simple_HTable is
189 type Element_Wrapper;
190 type Elmt_Ptr is access all Element_Wrapper;
191 type Element_Wrapper is record
197 procedure Free is new
198 Ada.Unchecked_Deallocation (Element_Wrapper, Elmt_Ptr);
200 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr);
201 function Next (E : Elmt_Ptr) return Elmt_Ptr;
202 function Get_Key (E : Elmt_Ptr) return Key;
204 package Tab is new Static_HTable (
205 Header_Num => Header_Num,
206 Element => Element_Wrapper,
207 Elmt_Ptr => Elmt_Ptr,
209 Set_Next => Set_Next,
220 function Get (K : Key) return Element is
221 Tmp : constant Elmt_Ptr := Tab.Get (K);
235 function Get_First return Element is
236 Tmp : constant Elmt_Ptr := Tab.Get_First;
250 function Get_Key (E : Elmt_Ptr) return Key is
259 function Get_Next return Element is
260 Tmp : constant Elmt_Ptr := Tab.Get_Next;
274 function Next (E : Elmt_Ptr) return Elmt_Ptr is
283 procedure Remove (K : Key) is
304 while E1 /= null loop
317 procedure Set (K : Key; E : Element) is
318 Tmp : constant Elmt_Ptr := Tab.Get (K);
322 Tab.Set (new Element_Wrapper'(K, E, null));
332 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
342 function Hash (Key : String) return Header_Num is
344 type Uns is mod 2 ** 32;
346 function Rotate_Left (Value : Uns; Amount : Natural) return Uns;
347 pragma Import (Intrinsic, Rotate_Left);
352 for J in Key'Range loop
353 Tmp := Rotate_Left (Tmp, 1) + Character'Pos (Key (J));
356 return Header_Num'First +
357 Header_Num'Base (Tmp mod Header_Num'Range_Length);