1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- G N A T . D Y N A M I C _ T A B L E S --
9 -- Copyright (C) 2000-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 GNAT
.Heap_Sort_G
;
35 with System
; use System
;
36 with System
.Memory
; use System
.Memory
;
38 with Unchecked_Conversion
;
40 package body GNAT
.Dynamic_Tables
is
42 Min
: constant Integer := Integer (Table_Low_Bound
);
43 -- Subscript of the minimum entry in the currently allocated table
45 -----------------------
46 -- Local Subprograms --
47 -----------------------
49 procedure Reallocate
(T
: in out Instance
);
50 -- Reallocate the existing table according to the current value stored
51 -- in Max. Works correctly to do an initial allocation if the table
54 pragma Warnings
(Off
);
55 -- These unchecked conversions are in fact safe, since they never
56 -- generate improperly aliased pointer values.
58 function To_Address
is new Unchecked_Conversion
(Table_Ptr
, Address
);
59 function To_Pointer
is new Unchecked_Conversion
(Address
, Table_Ptr
);
72 T
.P
.Last_Val
:= T
.P
.Last_Val
+ Num
;
74 if T
.P
.Last_Val
> T
.P
.Max
then
83 procedure Append
(T
: in out Instance
; New_Val
: Table_Component_Type
) is
86 T
.Table
(Table_Index_Type
(T
.P
.Last_Val
)) := New_Val
;
93 procedure Decrement_Last
(T
: in out Instance
) is
95 T
.P
.Last_Val
:= T
.P
.Last_Val
- 1;
102 procedure For_Each
(Table
: Instance
) is
103 Quit
: Boolean := False;
105 for Index
in Table_Low_Bound
.. Table_Index_Type
(Table
.P
.Last_Val
) loop
106 Action
(Index
, Table
.Table
(Index
), Quit
);
115 procedure Free
(T
: in out Instance
) is
117 Free
(To_Address
(T
.Table
));
126 procedure Increment_Last
(T
: in out Instance
) is
128 T
.P
.Last_Val
:= T
.P
.Last_Val
+ 1;
130 if T
.P
.Last_Val
> T
.P
.Max
then
139 procedure Init
(T
: in out Instance
) is
140 Old_Length
: constant Integer := T
.P
.Length
;
143 T
.P
.Last_Val
:= Min
- 1;
144 T
.P
.Max
:= Min
+ Table_Initial
- 1;
145 T
.P
.Length
:= T
.P
.Max
- Min
+ 1;
147 -- If table is same size as before (happens when table is never
148 -- expanded which is a common case), then simply reuse it. Note
149 -- that this also means that an explicit Init call right after
150 -- the implicit one in the package body is harmless.
152 if Old_Length
= T
.P
.Length
then
155 -- Otherwise we can use Reallocate to get a table of the right size.
156 -- Note that Reallocate works fine to allocate a table of the right
157 -- initial size when it is first allocated.
168 function Last
(T
: in Instance
) return Table_Index_Type
is
170 return Table_Index_Type
(T
.P
.Last_Val
);
177 procedure Reallocate
(T
: in out Instance
) is
178 New_Length
: Integer;
182 if T
.P
.Max
< T
.P
.Last_Val
then
183 while T
.P
.Max
< T
.P
.Last_Val
loop
184 New_Length
:= T
.P
.Length
* (100 + Table_Increment
) / 100;
186 if New_Length
> T
.P
.Length
then
187 T
.P
.Length
:= New_Length
;
189 T
.P
.Length
:= T
.P
.Length
+ 1;
192 T
.P
.Max
:= Min
+ T
.P
.Length
- 1;
197 size_t
((T
.P
.Max
- Min
+ 1) *
198 (Table_Type
'Component_Size / Storage_Unit
));
200 if T
.Table
= null then
201 T
.Table
:= To_Pointer
(Alloc
(New_Size
));
203 elsif New_Size
> 0 then
205 To_Pointer
(Realloc
(Ptr
=> To_Address
(T
.Table
),
209 if T
.P
.Length
/= 0 and then T
.Table
= null then
218 procedure Release
(T
: in out Instance
) is
220 T
.P
.Length
:= T
.P
.Last_Val
- Integer (Table_Low_Bound
) + 1;
221 T
.P
.Max
:= T
.P
.Last_Val
;
230 (T
: in out Instance
;
231 Index
: Table_Index_Type
;
232 Item
: Table_Component_Type
)
235 if Integer (Index
) > T
.P
.Last_Val
then
239 T
.Table
(Index
) := Item
;
246 procedure Set_Last
(T
: in out Instance
; New_Val
: Table_Index_Type
) is
248 if Integer (New_Val
) < T
.P
.Last_Val
then
249 T
.P
.Last_Val
:= Integer (New_Val
);
252 T
.P
.Last_Val
:= Integer (New_Val
);
254 if T
.P
.Last_Val
> T
.P
.Max
then
264 procedure Sort_Table
(Table
: in out Instance
) is
266 Temp
: Table_Component_Type
;
267 -- A temporary position to simulate index 0
271 function Index_Of
(Idx
: Natural) return Table_Index_Type
;
272 -- Apply Natural to indexs of the table
274 function Lower_Than
(Op1
, Op2
: Natural) return Boolean;
275 -- Compare two components
277 procedure Move
(From
: Natural; To
: Natural);
278 -- Move one component
280 package Heap_Sort
is new GNAT
.Heap_Sort_G
(Move
, Lower_Than
);
286 function Index_Of
(Idx
: Natural) return Table_Index_Type
is
288 return First
+ Table_Index_Type
(Idx
) - 1;
295 procedure Move
(From
: Natural; To
: Natural) is
298 Table
.Table
(Index_Of
(To
)) := Temp
;
301 Temp
:= Table
.Table
(Index_Of
(From
));
304 Table
.Table
(Index_Of
(To
)) :=
305 Table
.Table
(Index_Of
(From
));
313 function Lower_Than
(Op1
, Op2
: Natural) return Boolean is
316 return Lt
(Temp
, Table
.Table
(Index_Of
(Op2
)));
319 return Lt
(Table
.Table
(Index_Of
(Op1
)), Temp
);
323 Lt
(Table
.Table
(Index_Of
(Op1
)),
324 Table
.Table
(Index_Of
(Op2
)));
328 -- Start of processing for Sort_Table
332 Heap_Sort
.Sort
(Natural (Last
(Table
) - First
) + 1);
336 end GNAT
.Dynamic_Tables
;