1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- G N A T . H T A B L E --
11 -- Copyright (C) 1995-1999 Ada Core Technologies, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
33 ------------------------------------------------------------------------------
35 with Ada
.Unchecked_Deallocation
;
36 package body GNAT
.HTable
is
42 package body Static_HTable
is
44 Table
: array (Header_Num
) of Elmt_Ptr
;
46 Iterator_Index
: Header_Num
;
47 Iterator_Ptr
: Elmt_Ptr
;
48 Iterator_Started
: Boolean := False;
50 function Get_Non_Null
return Elmt_Ptr
;
51 -- Returns Null_Ptr if Iterator_Started is false of the Table is
52 -- empty. Returns Iterator_Ptr if non null, or the next non null
53 -- element in table if any.
59 function Get
(K
: Key
) return Elmt_Ptr
is
63 Elmt
:= Table
(Hash
(K
));
66 if Elmt
= Null_Ptr
then
69 elsif Equal
(Get_Key
(Elmt
), K
) then
82 function Get_First
return Elmt_Ptr
is
84 Iterator_Started
:= True;
85 Iterator_Index
:= Table
'First;
86 Iterator_Ptr
:= Table
(Iterator_Index
);
94 function Get_Next
return Elmt_Ptr
is
96 if not Iterator_Started
then
100 Iterator_Ptr
:= Next
(Iterator_Ptr
);
108 function Get_Non_Null
return Elmt_Ptr
is
110 while Iterator_Ptr
= Null_Ptr
loop
111 if Iterator_Index
= Table
'Last then
112 Iterator_Started
:= False;
116 Iterator_Index
:= Iterator_Index
+ 1;
117 Iterator_Ptr
:= Table
(Iterator_Index
);
127 procedure Remove
(K
: Key
) is
128 Index
: constant Header_Num
:= Hash
(K
);
130 Next_Elmt
: Elmt_Ptr
;
133 Elmt
:= Table
(Index
);
135 if Elmt
= Null_Ptr
then
138 elsif Equal
(Get_Key
(Elmt
), K
) then
139 Table
(Index
) := Next
(Elmt
);
143 Next_Elmt
:= Next
(Elmt
);
145 if Next_Elmt
= Null_Ptr
then
148 elsif Equal
(Get_Key
(Next_Elmt
), K
) then
149 Set_Next
(Elmt
, Next
(Next_Elmt
));
165 for J
in Table
'Range loop
166 Table
(J
) := Null_Ptr
;
174 procedure Set
(E
: Elmt_Ptr
) is
178 Index
:= Hash
(Get_Key
(E
));
179 Set_Next
(E
, Table
(Index
));
189 package body Simple_HTable
is
191 type Element_Wrapper
;
192 type Elmt_Ptr
is access all Element_Wrapper
;
193 type Element_Wrapper
is record
199 procedure Free
is new
200 Ada
.Unchecked_Deallocation
(Element_Wrapper
, Elmt_Ptr
);
202 procedure Set_Next
(E
: Elmt_Ptr
; Next
: Elmt_Ptr
);
203 function Next
(E
: Elmt_Ptr
) return Elmt_Ptr
;
204 function Get_Key
(E
: Elmt_Ptr
) return Key
;
206 package Tab
is new Static_HTable
(
207 Header_Num
=> Header_Num
,
208 Element
=> Element_Wrapper
,
209 Elmt_Ptr
=> Elmt_Ptr
,
211 Set_Next
=> Set_Next
,
222 function Get
(K
: Key
) return Element
is
223 Tmp
: constant Elmt_Ptr
:= Tab
.Get
(K
);
237 function Get_First
return Element
is
238 Tmp
: constant Elmt_Ptr
:= Tab
.Get_First
;
252 function Get_Key
(E
: Elmt_Ptr
) return Key
is
261 function Get_Next
return Element
is
262 Tmp
: constant Elmt_Ptr
:= Tab
.Get_Next
;
276 function Next
(E
: Elmt_Ptr
) return Elmt_Ptr
is
285 procedure Remove
(K
: Key
) is
306 while E1
/= null loop
319 procedure Set
(K
: Key
; E
: Element
) is
320 Tmp
: constant Elmt_Ptr
:= Tab
.Get
(K
);
324 Tab
.Set
(new Element_Wrapper
'(K, E, null));
334 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
344 function Hash (Key : String) return Header_Num is
346 type Uns is mod 2 ** 32;
348 function Rotate_Left (Value : Uns; Amount : Natural) return Uns;
349 pragma Import (Intrinsic, Rotate_Left);
354 for J in Key'Range loop
355 Tmp := Rotate_Left (Tmp, 1) + Character'Pos (Key (J));
358 return Header_Num'First +
359 Header_Num'Base (Tmp mod Header_Num'Range_Length);