2005-05-11 Kenneth Zadeck <zadeck@naturalbridge.com>
[official-gcc.git] / gcc / ada / g-dyntab.adb
blob25320dca7de764222b8a6797aa106f13d70d294d
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D Y N A M I C _ T A B L E S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2000-2004 Ada Core Technologies, Inc. --
10 -- --
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. --
21 -- --
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. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
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
52 -- is currently null.
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);
61 pragma Warnings (On);
63 --------------
64 -- Allocate --
65 --------------
67 procedure Allocate
68 (T : in out Instance;
69 Num : Integer := 1)
71 begin
72 T.P.Last_Val := T.P.Last_Val + Num;
74 if T.P.Last_Val > T.P.Max then
75 Reallocate (T);
76 end if;
77 end Allocate;
79 ------------
80 -- Append --
81 ------------
83 procedure Append (T : in out Instance; New_Val : Table_Component_Type) is
84 begin
85 Increment_Last (T);
86 T.Table (Table_Index_Type (T.P.Last_Val)) := New_Val;
87 end Append;
89 --------------------
90 -- Decrement_Last --
91 --------------------
93 procedure Decrement_Last (T : in out Instance) is
94 begin
95 T.P.Last_Val := T.P.Last_Val - 1;
96 end Decrement_Last;
98 --------------
99 -- For_Each --
100 --------------
102 procedure For_Each (Table : Instance) is
103 Quit : Boolean := False;
104 begin
105 for Index in Table_Low_Bound .. Table_Index_Type (Table.P.Last_Val) loop
106 Action (Index, Table.Table (Index), Quit);
107 exit when Quit;
108 end loop;
109 end For_Each;
111 ----------
112 -- Free --
113 ----------
115 procedure Free (T : in out Instance) is
116 begin
117 Free (To_Address (T.Table));
118 T.Table := null;
119 T.P.Length := 0;
120 end Free;
122 --------------------
123 -- Increment_Last --
124 --------------------
126 procedure Increment_Last (T : in out Instance) is
127 begin
128 T.P.Last_Val := T.P.Last_Val + 1;
130 if T.P.Last_Val > T.P.Max then
131 Reallocate (T);
132 end if;
133 end Increment_Last;
135 ----------
136 -- Init --
137 ----------
139 procedure Init (T : in out Instance) is
140 Old_Length : constant Integer := T.P.Length;
142 begin
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
153 return;
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.
159 else
160 Reallocate (T);
161 end if;
162 end Init;
164 ----------
165 -- Last --
166 ----------
168 function Last (T : in Instance) return Table_Index_Type is
169 begin
170 return Table_Index_Type (T.P.Last_Val);
171 end Last;
173 ----------------
174 -- Reallocate --
175 ----------------
177 procedure Reallocate (T : in out Instance) is
178 New_Length : Integer;
179 New_Size : size_t;
181 begin
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;
188 else
189 T.P.Length := T.P.Length + 1;
190 end if;
192 T.P.Max := Min + T.P.Length - 1;
193 end loop;
194 end if;
196 New_Size :=
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
204 T.Table :=
205 To_Pointer (Realloc (Ptr => To_Address (T.Table),
206 Size => New_Size));
207 end if;
209 if T.P.Length /= 0 and then T.Table = null then
210 raise Storage_Error;
211 end if;
212 end Reallocate;
214 -------------
215 -- Release --
216 -------------
218 procedure Release (T : in out Instance) is
219 begin
220 T.P.Length := T.P.Last_Val - Integer (Table_Low_Bound) + 1;
221 T.P.Max := T.P.Last_Val;
222 Reallocate (T);
223 end Release;
225 --------------
226 -- Set_Item --
227 --------------
229 procedure Set_Item
230 (T : in out Instance;
231 Index : Table_Index_Type;
232 Item : Table_Component_Type)
234 begin
235 if Integer (Index) > T.P.Last_Val then
236 Set_Last (T, Index);
237 end if;
239 T.Table (Index) := Item;
240 end Set_Item;
242 --------------
243 -- Set_Last --
244 --------------
246 procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type) is
247 begin
248 if Integer (New_Val) < T.P.Last_Val then
249 T.P.Last_Val := Integer (New_Val);
251 else
252 T.P.Last_Val := Integer (New_Val);
254 if T.P.Last_Val > T.P.Max then
255 Reallocate (T);
256 end if;
257 end if;
258 end Set_Last;
260 ----------------
261 -- Sort_Table --
262 ----------------
264 procedure Sort_Table (Table : in out Instance) is
266 Temp : Table_Component_Type;
267 -- A temporary position to simulate index 0
269 -- Local subprograms
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);
282 --------------
283 -- Index_Of --
284 --------------
286 function Index_Of (Idx : Natural) return Table_Index_Type is
287 begin
288 return First + Table_Index_Type (Idx) - 1;
289 end Index_Of;
291 ----------
292 -- Move --
293 ----------
295 procedure Move (From : Natural; To : Natural) is
296 begin
297 if From = 0 then
298 Table.Table (Index_Of (To)) := Temp;
300 elsif To = 0 then
301 Temp := Table.Table (Index_Of (From));
303 else
304 Table.Table (Index_Of (To)) :=
305 Table.Table (Index_Of (From));
306 end if;
307 end Move;
309 ----------------
310 -- Lower_Than --
311 ----------------
313 function Lower_Than (Op1, Op2 : Natural) return Boolean is
314 begin
315 if Op1 = 0 then
316 return Lt (Temp, Table.Table (Index_Of (Op2)));
318 elsif Op2 = 0 then
319 return Lt (Table.Table (Index_Of (Op1)), Temp);
321 else
322 return
323 Lt (Table.Table (Index_Of (Op1)),
324 Table.Table (Index_Of (Op2)));
325 end if;
326 end Lower_Than;
328 -- Start of processing for Sort_Table
330 begin
332 Heap_Sort.Sort (Natural (Last (Table) - First) + 1);
334 end Sort_Table;
336 end GNAT.Dynamic_Tables;