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-2003 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 function To_Address is new Unchecked_Conversion (Table_Ptr, Address);
55 function To_Pointer is new Unchecked_Conversion (Address, Table_Ptr);
66 T.P.Last_Val := T.P.Last_Val + Num;
68 if T.P.Last_Val > T.P.Max then
77 procedure Append (T : in out Instance; New_Val : Table_Component_Type) is
80 T.Table (Table_Index_Type (T.P.Last_Val)) := New_Val;
87 procedure Decrement_Last (T : in out Instance) is
89 T.P.Last_Val := T.P.Last_Val - 1;
96 procedure For_Each (Table : Instance) is
97 Quit : Boolean := False;
99 for Index in Table_Low_Bound .. Table_Index_Type (Table.P.Last_Val) loop
100 Action (Index, Table.Table (Index), Quit);
109 procedure Free (T : in out Instance) is
111 Free (To_Address (T.Table));
120 procedure Increment_Last (T : in out Instance) is
122 T.P.Last_Val := T.P.Last_Val + 1;
124 if T.P.Last_Val > T.P.Max then
133 procedure Init (T : in out Instance) is
134 Old_Length : constant Integer := T.P.Length;
137 T.P.Last_Val := Min - 1;
138 T.P.Max := Min + Table_Initial - 1;
139 T.P.Length := T.P.Max - Min + 1;
141 -- If table is same size as before (happens when table is never
142 -- expanded which is a common case), then simply reuse it. Note
143 -- that this also means that an explicit Init call right after
144 -- the implicit one in the package body is harmless.
146 if Old_Length = T.P.Length then
149 -- Otherwise we can use Reallocate to get a table of the right size.
150 -- Note that Reallocate works fine to allocate a table of the right
151 -- initial size when it is first allocated.
162 function Last (T : in Instance) return Table_Index_Type is
164 return Table_Index_Type (T.P.Last_Val);
171 procedure Reallocate (T : in out Instance) is
172 New_Length : Integer;
176 if T.P.Max < T.P.Last_Val then
177 while T.P.Max < T.P.Last_Val loop
178 New_Length := T.P.Length * (100 + Table_Increment) / 100;
180 if New_Length > T.P.Length then
181 T.P.Length := New_Length;
183 T.P.Length := T.P.Length + 1;
186 T.P.Max := Min + T.P.Length - 1;
191 size_t ((T.P.Max - Min + 1) *
192 (Table_Type'Component_Size / Storage_Unit));
194 if T.Table = null then
195 T.Table := To_Pointer (Alloc (New_Size));
197 elsif New_Size > 0 then
199 To_Pointer (Realloc (Ptr => To_Address (T.Table),
203 if T.P.Length /= 0 and then T.Table = null then
212 procedure Release (T : in out Instance) is
214 T.P.Length := T.P.Last_Val - Integer (Table_Low_Bound) + 1;
215 T.P.Max := T.P.Last_Val;
224 (T : in out Instance;
225 Index : Table_Index_Type;
226 Item : Table_Component_Type)
229 if Integer (Index) > T.P.Last_Val then
233 T.Table (Index) := Item;
240 procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type) is
242 if Integer (New_Val) < T.P.Last_Val then
243 T.P.Last_Val := Integer (New_Val);
246 T.P.Last_Val := Integer (New_Val);
248 if T.P.Last_Val > T.P.Max then
258 procedure Sort_Table (Table : in out Instance) is
260 Temp : Table_Component_Type;
261 -- A temporary position to simulate index 0
265 function Index_Of (Idx : Natural) return Table_Index_Type;
266 -- Apply Natural to indexs of the table
268 function Lower_Than (Op1, Op2 : Natural) return Boolean;
269 -- Compare two components
271 procedure Move (From : Natural; To : Natural);
272 -- Move one component
274 package Heap_Sort is new GNAT.Heap_Sort_G (Move, Lower_Than);
280 function Index_Of (Idx : Natural) return Table_Index_Type is
282 return First + Table_Index_Type (Idx) - 1;
289 procedure Move (From : Natural; To : Natural) is
292 Table.Table (Index_Of (To)) := Temp;
295 Temp := Table.Table (Index_Of (From));
298 Table.Table (Index_Of (To)) :=
299 Table.Table (Index_Of (From));
307 function Lower_Than (Op1, Op2 : Natural) return Boolean is
310 return Lt (Temp, Table.Table (Index_Of (Op2)));
313 return Lt (Table.Table (Index_Of (Op1)), Temp);
317 Lt (Table.Table (Index_Of (Op1)),
318 Table.Table (Index_Of (Op2)));
322 -- Start of processing for Sort_Table
326 Heap_Sort.Sort (Natural (Last (Table) - First) + 1);
330 end GNAT.Dynamic_Tables;