1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- G N A T . D Y N A M I C _ H T A B L E S --
9 -- Copyright (C) 2002-2006, 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 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 package body GNAT
.Dynamic_HTables
is
40 package body Static_HTable
is
42 type Table_Type
is array (Header_Num
) of Elmt_Ptr
;
44 type Instance_Data
is record
46 Iterator_Index
: Header_Num
;
47 Iterator_Ptr
: Elmt_Ptr
;
48 Iterator_Started
: Boolean := False;
51 function Get_Non_Null
(T
: Instance
) return Elmt_Ptr
;
52 -- Returns Null_Ptr if Iterator_Started is False or if the Table is
53 -- empty. Returns Iterator_Ptr if non null, or the next non null
54 -- element in table if any.
60 function Get
(T
: Instance
; K
: Key
) return Elmt_Ptr
is
68 Elmt
:= T
.Table
(Hash
(K
));
71 if Elmt
= Null_Ptr
then
74 elsif Equal
(Get_Key
(Elmt
), K
) then
87 function Get_First
(T
: Instance
) return Elmt_Ptr
is
93 T
.Iterator_Started
:= True;
94 T
.Iterator_Index
:= T
.Table
'First;
95 T
.Iterator_Ptr
:= T
.Table
(T
.Iterator_Index
);
96 return Get_Non_Null
(T
);
103 function Get_Next
(T
: Instance
) return Elmt_Ptr
is
105 if T
= null or else not T
.Iterator_Started
then
109 T
.Iterator_Ptr
:= Next
(T
.Iterator_Ptr
);
110 return Get_Non_Null
(T
);
117 function Get_Non_Null
(T
: Instance
) return Elmt_Ptr
is
123 while T
.Iterator_Ptr
= Null_Ptr
loop
124 if T
.Iterator_Index
= T
.Table
'Last then
125 T
.Iterator_Started
:= False;
129 T
.Iterator_Index
:= T
.Iterator_Index
+ 1;
130 T
.Iterator_Ptr
:= T
.Table
(T
.Iterator_Index
);
133 return T
.Iterator_Ptr
;
140 procedure Remove
(T
: Instance
; K
: Key
) is
141 Index
: constant Header_Num
:= Hash
(K
);
143 Next_Elmt
: Elmt_Ptr
;
150 Elmt
:= T
.Table
(Index
);
152 if Elmt
= Null_Ptr
then
155 elsif Equal
(Get_Key
(Elmt
), K
) then
156 T
.Table
(Index
) := Next
(Elmt
);
160 Next_Elmt
:= Next
(Elmt
);
162 if Next_Elmt
= Null_Ptr
then
165 elsif Equal
(Get_Key
(Next_Elmt
), K
) then
166 Set_Next
(Elmt
, Next
(Next_Elmt
));
180 procedure Reset
(T
: in out Instance
) is
182 new Ada
.Unchecked_Deallocation
(Instance_Data
, Instance
);
189 for J
in T
.Table
'Range loop
190 T
.Table
(J
) := Null_Ptr
;
200 procedure Set
(T
: in out Instance
; E
: Elmt_Ptr
) is
205 T
:= new Instance_Data
;
208 Index
:= Hash
(Get_Key
(E
));
209 Set_Next
(E
, T
.Table
(Index
));
210 T
.Table
(Index
) := E
;
219 package body Simple_HTable
is
225 function Get
(T
: Instance
; K
: Key
) return Element
is
233 Tmp
:= Tab
.Get
(Tab
.Instance
(T
), K
);
246 function Get_First
(T
: Instance
) return Element
is
247 Tmp
: constant Elmt_Ptr
:= Tab
.Get_First
(Tab
.Instance
(T
));
261 function Get_Key
(E
: Elmt_Ptr
) return Key
is
270 function Get_Next
(T
: Instance
) return Element
is
271 Tmp
: constant Elmt_Ptr
:= Tab
.Get_Next
(Tab
.Instance
(T
));
284 function Next
(E
: Elmt_Ptr
) return Elmt_Ptr
is
293 procedure Remove
(T
: Instance
; K
: Key
) is
297 Tmp
:= Tab
.Get
(Tab
.Instance
(T
), K
);
300 Tab
.Remove
(Tab
.Instance
(T
), K
);
309 procedure Reset
(T
: in out Instance
) is
313 E1
:= Tab
.Get_First
(Tab
.Instance
(T
));
314 while E1
/= null loop
315 E2
:= Tab
.Get_Next
(Tab
.Instance
(T
));
320 Tab
.Reset
(Tab
.Instance
(T
));
327 procedure Set
(T
: in out Instance
; K
: Key
; E
: Element
) is
328 Tmp
: constant Elmt_Ptr
:= Tab
.Get
(Tab
.Instance
(T
), K
);
331 Tab
.Set
(Tab
.Instance
(T
), new Element_Wrapper
'(K, E, null));
341 procedure Set_Next (E : Elmt_Ptr; Next : Elmt_Ptr) is
348 end GNAT.Dynamic_HTables;