1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- G N A T . D Y N A M I C _ H T A B L E S --
9 -- Copyright (C) 2002-2004 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 was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
32 ------------------------------------------------------------------------------
34 with Ada
.Unchecked_Deallocation
;
35 package body GNAT
.Dynamic_HTables
is
41 package body Static_HTable
is
43 type Table_Type
is array (Header_Num
) of Elmt_Ptr
;
45 type Instance_Data
is record
47 Iterator_Index
: Header_Num
;
48 Iterator_Ptr
: Elmt_Ptr
;
49 Iterator_Started
: Boolean := False;
52 function Get_Non_Null
(T
: Instance
) return Elmt_Ptr
;
53 -- Returns Null_Ptr if Iterator_Started is False or if the Table is
54 -- empty. Returns Iterator_Ptr if non null, or the next non null
55 -- element in table if any.
61 function Get
(T
: Instance
; K
: Key
) return Elmt_Ptr
is
69 Elmt
:= T
.Table
(Hash
(K
));
72 if Elmt
= Null_Ptr
then
75 elsif Equal
(Get_Key
(Elmt
), K
) then
88 function Get_First
(T
: Instance
) return Elmt_Ptr
is
94 T
.Iterator_Started
:= True;
95 T
.Iterator_Index
:= T
.Table
'First;
96 T
.Iterator_Ptr
:= T
.Table
(T
.Iterator_Index
);
97 return Get_Non_Null
(T
);
104 function Get_Next
(T
: Instance
) return Elmt_Ptr
is
106 if T
= null or else not T
.Iterator_Started
then
110 T
.Iterator_Ptr
:= Next
(T
.Iterator_Ptr
);
111 return Get_Non_Null
(T
);
118 function Get_Non_Null
(T
: Instance
) return Elmt_Ptr
is
124 while T
.Iterator_Ptr
= Null_Ptr
loop
125 if T
.Iterator_Index
= T
.Table
'Last then
126 T
.Iterator_Started
:= False;
130 T
.Iterator_Index
:= T
.Iterator_Index
+ 1;
131 T
.Iterator_Ptr
:= T
.Table
(T
.Iterator_Index
);
134 return T
.Iterator_Ptr
;
141 procedure Remove
(T
: Instance
; K
: Key
) is
142 Index
: constant Header_Num
:= Hash
(K
);
144 Next_Elmt
: Elmt_Ptr
;
151 Elmt
:= T
.Table
(Index
);
153 if Elmt
= Null_Ptr
then
156 elsif Equal
(Get_Key
(Elmt
), K
) then
157 T
.Table
(Index
) := Next
(Elmt
);
161 Next_Elmt
:= Next
(Elmt
);
163 if Next_Elmt
= Null_Ptr
then
166 elsif Equal
(Get_Key
(Next_Elmt
), K
) then
167 Set_Next
(Elmt
, Next
(Next_Elmt
));
181 procedure Reset
(T
: in out Instance
) is
187 for J
in T
.Table
'Range loop
188 T
.Table
(J
) := Null_Ptr
;
196 procedure Set
(T
: in out Instance
; E
: Elmt_Ptr
) is
201 T
:= new Instance_Data
;
204 Index
:= Hash
(Get_Key
(E
));
205 Set_Next
(E
, T
.Table
(Index
));
206 T
.Table
(Index
) := E
;
214 package body Simple_HTable
is
220 function Get
(T
: Instance
; K
: Key
) return Element
is
228 Tmp
:= Tab
.Get
(Tab
.Instance
(T
), K
);
241 function Get_First
(T
: Instance
) return Element
is
242 Tmp
: constant Elmt_Ptr
:= Tab
.Get_First
(Tab
.Instance
(T
));
256 function Get_Key
(E
: Elmt_Ptr
) return Key
is
265 function Get_Next
(T
: Instance
) return Element
is
266 Tmp
: constant Elmt_Ptr
:= Tab
.Get_Next
(Tab
.Instance
(T
));
280 function Next
(E
: Elmt_Ptr
) return Elmt_Ptr
is
289 procedure Remove
(T
: Instance
; K
: Key
) is
293 Tmp
:= Tab
.Get
(Tab
.Instance
(T
), K
);
296 Tab
.Remove
(Tab
.Instance
(T
), K
);
305 procedure Reset
(T
: in out Instance
) is
309 E1
:= Tab
.Get_First
(Tab
.Instance
(T
));
310 while E1
/= null loop
311 E2
:= Tab
.Get_Next
(Tab
.Instance
(T
));
316 Tab
.Reset
(Tab
.Instance
(T
));
323 procedure Set
(T
: in out Instance
; K
: Key
; E
: Element
) is
324 Tmp
: constant Elmt_Ptr
:= Tab
.Get
(Tab
.Instance
(T
), K
);
328 Tab
.Set
(Tab
.Instance
(T
), new Element_Wrapper
'(K, E, null));
338 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
344 end GNAT.Dynamic_HTables;