annotate gcc/ada/libgnat/g-dyntab.adb @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- G N A T . D Y N A M I C _ T A B L E S --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 2000-2018, AdaCore --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 pragma Compiler_Unit_Warning;
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 with GNAT.Heap_Sort_G;
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 with Ada.Unchecked_Deallocation;
kono
parents:
diff changeset
37 with System;
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 package body GNAT.Dynamic_Tables is
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 -----------------------
kono
parents:
diff changeset
42 -- Local Subprograms --
kono
parents:
diff changeset
43 -----------------------
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 function Last_Allocated (T : Instance) return Table_Last_Type;
kono
parents:
diff changeset
46 pragma Inline (Last_Allocated);
kono
parents:
diff changeset
47 -- Return the index of the last allocated element
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 procedure Grow (T : in out Instance; New_Last : Table_Last_Type);
kono
parents:
diff changeset
50 -- This is called when we are about to set the value of Last to a value
kono
parents:
diff changeset
51 -- that is larger than Last_Allocated. This reallocates the table to the
kono
parents:
diff changeset
52 -- larger size, as indicated by New_Last. At the time this is called,
kono
parents:
diff changeset
53 -- Last (T) is still the old value, and this does not modify it.
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 --------------
kono
parents:
diff changeset
56 -- Allocate --
kono
parents:
diff changeset
57 --------------
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 procedure Allocate (T : in out Instance; Num : Integer := 1) is
kono
parents:
diff changeset
60 begin
kono
parents:
diff changeset
61 -- Note that Num can be negative
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 pragma Assert (not T.Locked);
kono
parents:
diff changeset
64 Set_Last (T, Last (T) + Table_Index_Type'Base (Num));
kono
parents:
diff changeset
65 end Allocate;
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 ------------
kono
parents:
diff changeset
68 -- Append --
kono
parents:
diff changeset
69 ------------
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 procedure Append (T : in out Instance; New_Val : Table_Component_Type) is
kono
parents:
diff changeset
72 pragma Assert (not T.Locked);
kono
parents:
diff changeset
73 New_Last : constant Table_Last_Type := Last (T) + 1;
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 begin
kono
parents:
diff changeset
76 if New_Last <= Last_Allocated (T) then
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 -- Fast path
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 T.P.Last := New_Last;
kono
parents:
diff changeset
81 T.Table (New_Last) := New_Val;
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 else
kono
parents:
diff changeset
84 Set_Item (T, New_Last, New_Val);
kono
parents:
diff changeset
85 end if;
kono
parents:
diff changeset
86 end Append;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 ----------------
kono
parents:
diff changeset
89 -- Append_All --
kono
parents:
diff changeset
90 ----------------
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 procedure Append_All (T : in out Instance; New_Vals : Table_Type) is
kono
parents:
diff changeset
93 begin
kono
parents:
diff changeset
94 for J in New_Vals'Range loop
kono
parents:
diff changeset
95 Append (T, New_Vals (J));
kono
parents:
diff changeset
96 end loop;
kono
parents:
diff changeset
97 end Append_All;
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 --------------------
kono
parents:
diff changeset
100 -- Decrement_Last --
kono
parents:
diff changeset
101 --------------------
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 procedure Decrement_Last (T : in out Instance) is
kono
parents:
diff changeset
104 begin
kono
parents:
diff changeset
105 pragma Assert (not T.Locked);
kono
parents:
diff changeset
106 Allocate (T, -1);
kono
parents:
diff changeset
107 end Decrement_Last;
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 -----------
kono
parents:
diff changeset
110 -- First --
kono
parents:
diff changeset
111 -----------
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 function First return Table_Index_Type is
kono
parents:
diff changeset
114 begin
kono
parents:
diff changeset
115 return Table_Low_Bound;
kono
parents:
diff changeset
116 end First;
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 --------------
kono
parents:
diff changeset
119 -- For_Each --
kono
parents:
diff changeset
120 --------------
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 procedure For_Each (Table : Instance) is
kono
parents:
diff changeset
123 Quit : Boolean := False;
kono
parents:
diff changeset
124 begin
kono
parents:
diff changeset
125 for Index in First .. Last (Table) loop
kono
parents:
diff changeset
126 Action (Index, Table.Table (Index), Quit);
kono
parents:
diff changeset
127 exit when Quit;
kono
parents:
diff changeset
128 end loop;
kono
parents:
diff changeset
129 end For_Each;
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 ----------
kono
parents:
diff changeset
132 -- Grow --
kono
parents:
diff changeset
133 ----------
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 procedure Grow (T : in out Instance; New_Last : Table_Last_Type) is
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 -- Note: Type Alloc_Ptr below needs to be declared locally so we know
kono
parents:
diff changeset
138 -- the bounds. That means that the collection is local, so is finalized
kono
parents:
diff changeset
139 -- when leaving Grow. That's why this package doesn't support controlled
kono
parents:
diff changeset
140 -- types; the table elements would be finalized prematurely. An Ada
kono
parents:
diff changeset
141 -- implementation would also be within its rights to reclaim the
kono
parents:
diff changeset
142 -- storage. Fortunately, GNAT doesn't do that.
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 pragma Assert (not T.Locked);
kono
parents:
diff changeset
145 pragma Assert (New_Last > Last_Allocated (T));
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 subtype Table_Length_Type is Table_Index_Type'Base
kono
parents:
diff changeset
148 range 0 .. Table_Index_Type'Base'Last;
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
kono
parents:
diff changeset
151 Old_Allocated_Length : constant Table_Length_Type :=
kono
parents:
diff changeset
152 Old_Last_Allocated - First + 1;
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 New_Length : constant Table_Length_Type := New_Last - First + 1;
kono
parents:
diff changeset
155 New_Allocated_Length : Table_Length_Type;
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 begin
kono
parents:
diff changeset
158 if T.Table = Empty_Table_Ptr then
kono
parents:
diff changeset
159 New_Allocated_Length := Table_Length_Type (Table_Initial);
kono
parents:
diff changeset
160 else
kono
parents:
diff changeset
161 New_Allocated_Length :=
kono
parents:
diff changeset
162 Table_Length_Type
kono
parents:
diff changeset
163 (Long_Long_Integer (Old_Allocated_Length) *
kono
parents:
diff changeset
164 (100 + Long_Long_Integer (Table_Increment)) / 100);
kono
parents:
diff changeset
165 end if;
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 -- Make sure it really did grow
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 if New_Allocated_Length <= Old_Allocated_Length then
kono
parents:
diff changeset
170 New_Allocated_Length := Old_Allocated_Length + 10;
kono
parents:
diff changeset
171 end if;
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 if New_Allocated_Length <= New_Length then
kono
parents:
diff changeset
174 New_Allocated_Length := New_Length + 10;
kono
parents:
diff changeset
175 end if;
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 pragma Assert (New_Allocated_Length > Old_Allocated_Length);
kono
parents:
diff changeset
178 pragma Assert (New_Allocated_Length > New_Length);
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 T.P.Last_Allocated := First + New_Allocated_Length - 1;
kono
parents:
diff changeset
181
kono
parents:
diff changeset
182 declare
kono
parents:
diff changeset
183 subtype Old_Alloc_Type is Table_Type (First .. Old_Last_Allocated);
kono
parents:
diff changeset
184 type Old_Alloc_Ptr is access all Old_Alloc_Type;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 procedure Free is
kono
parents:
diff changeset
187 new Ada.Unchecked_Deallocation (Old_Alloc_Type, Old_Alloc_Ptr);
kono
parents:
diff changeset
188 function To_Old_Alloc_Ptr is
kono
parents:
diff changeset
189 new Ada.Unchecked_Conversion (Table_Ptr, Old_Alloc_Ptr);
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 subtype Alloc_Type is
kono
parents:
diff changeset
192 Table_Type (First .. First + New_Allocated_Length - 1);
kono
parents:
diff changeset
193 type Alloc_Ptr is access all Alloc_Type;
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 function To_Table_Ptr is
kono
parents:
diff changeset
196 new Ada.Unchecked_Conversion (Alloc_Ptr, Table_Ptr);
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 Old_Table : Old_Alloc_Ptr := To_Old_Alloc_Ptr (T.Table);
kono
parents:
diff changeset
199 New_Table : constant Alloc_Ptr := new Alloc_Type;
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 begin
kono
parents:
diff changeset
202 if T.Table /= Empty_Table_Ptr then
kono
parents:
diff changeset
203 New_Table (First .. Last (T)) := Old_Table (First .. Last (T));
kono
parents:
diff changeset
204 Free (Old_Table);
kono
parents:
diff changeset
205 end if;
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 T.Table := To_Table_Ptr (New_Table);
kono
parents:
diff changeset
208 end;
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 pragma Assert (New_Last <= Last_Allocated (T));
kono
parents:
diff changeset
211 pragma Assert (T.Table /= null);
kono
parents:
diff changeset
212 pragma Assert (T.Table /= Empty_Table_Ptr);
kono
parents:
diff changeset
213 end Grow;
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 --------------------
kono
parents:
diff changeset
216 -- Increment_Last --
kono
parents:
diff changeset
217 --------------------
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 procedure Increment_Last (T : in out Instance) is
kono
parents:
diff changeset
220 begin
kono
parents:
diff changeset
221 pragma Assert (not T.Locked);
kono
parents:
diff changeset
222 Allocate (T, 1);
kono
parents:
diff changeset
223 end Increment_Last;
kono
parents:
diff changeset
224
kono
parents:
diff changeset
225 ----------
kono
parents:
diff changeset
226 -- Init --
kono
parents:
diff changeset
227 ----------
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 procedure Init (T : in out Instance) is
kono
parents:
diff changeset
230 pragma Assert (not T.Locked);
kono
parents:
diff changeset
231 subtype Alloc_Type is Table_Type (First .. Last_Allocated (T));
kono
parents:
diff changeset
232 type Alloc_Ptr is access all Alloc_Type;
kono
parents:
diff changeset
233
kono
parents:
diff changeset
234 procedure Free is new Ada.Unchecked_Deallocation (Alloc_Type, Alloc_Ptr);
kono
parents:
diff changeset
235 function To_Alloc_Ptr is
kono
parents:
diff changeset
236 new Ada.Unchecked_Conversion (Table_Ptr, Alloc_Ptr);
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 Temp : Alloc_Ptr := To_Alloc_Ptr (T.Table);
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 begin
kono
parents:
diff changeset
241 if T.Table = Empty_Table_Ptr then
kono
parents:
diff changeset
242 pragma Assert (T.P = (Last_Allocated | Last => First - 1));
kono
parents:
diff changeset
243 null;
kono
parents:
diff changeset
244 else
kono
parents:
diff changeset
245 Free (Temp);
kono
parents:
diff changeset
246 T.Table := Empty_Table_Ptr;
kono
parents:
diff changeset
247 T.P := (Last_Allocated | Last => First - 1);
kono
parents:
diff changeset
248 end if;
kono
parents:
diff changeset
249 end Init;
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 --------------
kono
parents:
diff changeset
252 -- Is_Empty --
kono
parents:
diff changeset
253 --------------
kono
parents:
diff changeset
254
kono
parents:
diff changeset
255 function Is_Empty (T : Instance) return Boolean is
kono
parents:
diff changeset
256 begin
kono
parents:
diff changeset
257 return Last (T) = First - 1;
kono
parents:
diff changeset
258 end Is_Empty;
kono
parents:
diff changeset
259
kono
parents:
diff changeset
260 ----------
kono
parents:
diff changeset
261 -- Last --
kono
parents:
diff changeset
262 ----------
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 function Last (T : Instance) return Table_Last_Type is
kono
parents:
diff changeset
265 begin
kono
parents:
diff changeset
266 return T.P.Last;
kono
parents:
diff changeset
267 end Last;
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 --------------------
kono
parents:
diff changeset
270 -- Last_Allocated --
kono
parents:
diff changeset
271 --------------------
kono
parents:
diff changeset
272
kono
parents:
diff changeset
273 function Last_Allocated (T : Instance) return Table_Last_Type is
kono
parents:
diff changeset
274 begin
kono
parents:
diff changeset
275 return T.P.Last_Allocated;
kono
parents:
diff changeset
276 end Last_Allocated;
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 ----------
kono
parents:
diff changeset
279 -- Move --
kono
parents:
diff changeset
280 ----------
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 procedure Move (From, To : in out Instance) is
kono
parents:
diff changeset
283 begin
kono
parents:
diff changeset
284 pragma Assert (not From.Locked);
kono
parents:
diff changeset
285 pragma Assert (not To.Locked);
kono
parents:
diff changeset
286 pragma Assert (Is_Empty (To));
kono
parents:
diff changeset
287 To := From;
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 From.Table := Empty_Table_Ptr;
kono
parents:
diff changeset
290 From.Locked := False;
kono
parents:
diff changeset
291 From.P.Last_Allocated := First - 1;
kono
parents:
diff changeset
292 From.P.Last := First - 1;
kono
parents:
diff changeset
293 pragma Assert (Is_Empty (From));
kono
parents:
diff changeset
294 end Move;
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 -------------
kono
parents:
diff changeset
297 -- Release --
kono
parents:
diff changeset
298 -------------
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 procedure Release (T : in out Instance) is
kono
parents:
diff changeset
301 pragma Assert (not T.Locked);
kono
parents:
diff changeset
302 Old_Last_Allocated : constant Table_Last_Type := Last_Allocated (T);
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 function New_Last_Allocated return Table_Last_Type;
kono
parents:
diff changeset
305 -- Compute the new value of Last_Allocated. This is normally equal to
kono
parents:
diff changeset
306 -- Last, but if Release_Threshold /= 0, then we need to take that into
kono
parents:
diff changeset
307 -- account.
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 ------------------------
kono
parents:
diff changeset
310 -- New_Last_Allocated --
kono
parents:
diff changeset
311 ------------------------
kono
parents:
diff changeset
312
kono
parents:
diff changeset
313 function New_Last_Allocated return Table_Last_Type is
kono
parents:
diff changeset
314 subtype Table_Length_Type is Table_Index_Type'Base
kono
parents:
diff changeset
315 range 0 .. Table_Index_Type'Base'Last;
kono
parents:
diff changeset
316
kono
parents:
diff changeset
317 Length : constant Table_Length_Type := Last (T) - First + 1;
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 Comp_Size_In_Bytes : constant Table_Length_Type :=
kono
parents:
diff changeset
320 Table_Type'Component_Size / System.Storage_Unit;
kono
parents:
diff changeset
321
kono
parents:
diff changeset
322 Length_Threshold : constant Table_Length_Type :=
kono
parents:
diff changeset
323 Table_Length_Type (Release_Threshold) / Comp_Size_In_Bytes;
kono
parents:
diff changeset
324
kono
parents:
diff changeset
325 begin
kono
parents:
diff changeset
326 if Release_Threshold = 0 or else Length < Length_Threshold then
kono
parents:
diff changeset
327 return Last (T);
kono
parents:
diff changeset
328 else
kono
parents:
diff changeset
329 declare
kono
parents:
diff changeset
330 Extra_Length : constant Table_Length_Type := Length / 1000;
kono
parents:
diff changeset
331 begin
kono
parents:
diff changeset
332 return (Length + Extra_Length) - 1 + First;
kono
parents:
diff changeset
333 end;
kono
parents:
diff changeset
334 end if;
kono
parents:
diff changeset
335 end New_Last_Allocated;
kono
parents:
diff changeset
336
kono
parents:
diff changeset
337 -- Local variables
kono
parents:
diff changeset
338
kono
parents:
diff changeset
339 New_Last_Alloc : constant Table_Last_Type := New_Last_Allocated;
kono
parents:
diff changeset
340
kono
parents:
diff changeset
341 -- Start of processing for Release
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 begin
kono
parents:
diff changeset
344 if New_Last_Alloc < Last_Allocated (T) then
kono
parents:
diff changeset
345 pragma Assert (Last (T) < Last_Allocated (T));
kono
parents:
diff changeset
346 pragma Assert (T.Table /= Empty_Table_Ptr);
kono
parents:
diff changeset
347
kono
parents:
diff changeset
348 declare
kono
parents:
diff changeset
349 subtype Old_Alloc_Type is Table_Type (First .. Old_Last_Allocated);
kono
parents:
diff changeset
350 type Old_Alloc_Ptr is access all Old_Alloc_Type;
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 procedure Free is
kono
parents:
diff changeset
353 new Ada.Unchecked_Deallocation (Old_Alloc_Type, Old_Alloc_Ptr);
kono
parents:
diff changeset
354 function To_Old_Alloc_Ptr is
kono
parents:
diff changeset
355 new Ada.Unchecked_Conversion (Table_Ptr, Old_Alloc_Ptr);
kono
parents:
diff changeset
356
kono
parents:
diff changeset
357 subtype Alloc_Type is Table_Type (First .. New_Last_Alloc);
kono
parents:
diff changeset
358 type Alloc_Ptr is access all Alloc_Type;
kono
parents:
diff changeset
359
kono
parents:
diff changeset
360 function To_Table_Ptr is
kono
parents:
diff changeset
361 new Ada.Unchecked_Conversion (Alloc_Ptr, Table_Ptr);
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 Old_Table : Old_Alloc_Ptr := To_Old_Alloc_Ptr (T.Table);
kono
parents:
diff changeset
364 New_Table : constant Alloc_Ptr := new Alloc_Type;
kono
parents:
diff changeset
365
kono
parents:
diff changeset
366 begin
kono
parents:
diff changeset
367 New_Table (First .. Last (T)) := Old_Table (First .. Last (T));
kono
parents:
diff changeset
368 T.P.Last_Allocated := New_Last_Alloc;
kono
parents:
diff changeset
369 Free (Old_Table);
kono
parents:
diff changeset
370 T.Table := To_Table_Ptr (New_Table);
kono
parents:
diff changeset
371 end;
kono
parents:
diff changeset
372 end if;
kono
parents:
diff changeset
373 end Release;
kono
parents:
diff changeset
374
kono
parents:
diff changeset
375 --------------
kono
parents:
diff changeset
376 -- Set_Item --
kono
parents:
diff changeset
377 --------------
kono
parents:
diff changeset
378
kono
parents:
diff changeset
379 procedure Set_Item
kono
parents:
diff changeset
380 (T : in out Instance;
kono
parents:
diff changeset
381 Index : Valid_Table_Index_Type;
kono
parents:
diff changeset
382 Item : Table_Component_Type)
kono
parents:
diff changeset
383 is
kono
parents:
diff changeset
384 begin
kono
parents:
diff changeset
385 pragma Assert (not T.Locked);
kono
parents:
diff changeset
386
kono
parents:
diff changeset
387 -- If Set_Last is going to reallocate the table, we make a copy of Item,
kono
parents:
diff changeset
388 -- in case the call was "Set_Item (T, X, T.Table (Y));", and Item is
kono
parents:
diff changeset
389 -- passed by reference. Without the copy, we would deallocate the array
kono
parents:
diff changeset
390 -- containing Item, leaving a dangling pointer.
kono
parents:
diff changeset
391
kono
parents:
diff changeset
392 if Index > Last_Allocated (T) then
kono
parents:
diff changeset
393 declare
kono
parents:
diff changeset
394 Item_Copy : constant Table_Component_Type := Item;
kono
parents:
diff changeset
395 begin
kono
parents:
diff changeset
396 Set_Last (T, Index);
kono
parents:
diff changeset
397 T.Table (Index) := Item_Copy;
kono
parents:
diff changeset
398 end;
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 else
kono
parents:
diff changeset
401 if Index > Last (T) then
kono
parents:
diff changeset
402 Set_Last (T, Index);
kono
parents:
diff changeset
403 end if;
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 T.Table (Index) := Item;
kono
parents:
diff changeset
406 end if;
kono
parents:
diff changeset
407 end Set_Item;
kono
parents:
diff changeset
408
kono
parents:
diff changeset
409 --------------
kono
parents:
diff changeset
410 -- Set_Last --
kono
parents:
diff changeset
411 --------------
kono
parents:
diff changeset
412
kono
parents:
diff changeset
413 procedure Set_Last (T : in out Instance; New_Val : Table_Last_Type) is
kono
parents:
diff changeset
414 begin
kono
parents:
diff changeset
415 pragma Assert (not T.Locked);
kono
parents:
diff changeset
416 if New_Val > Last_Allocated (T) then
kono
parents:
diff changeset
417 Grow (T, New_Val);
kono
parents:
diff changeset
418 end if;
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 T.P.Last := New_Val;
kono
parents:
diff changeset
421 end Set_Last;
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 ----------------
kono
parents:
diff changeset
424 -- Sort_Table --
kono
parents:
diff changeset
425 ----------------
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 procedure Sort_Table (Table : in out Instance) is
kono
parents:
diff changeset
428 Temp : Table_Component_Type;
kono
parents:
diff changeset
429 -- A temporary position to simulate index 0
kono
parents:
diff changeset
430
kono
parents:
diff changeset
431 -- Local subprograms
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 function Index_Of (Idx : Natural) return Table_Index_Type'Base;
kono
parents:
diff changeset
434 -- Return index of Idx'th element of table
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 function Lower_Than (Op1, Op2 : Natural) return Boolean;
kono
parents:
diff changeset
437 -- Compare two components
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 procedure Move (From : Natural; To : Natural);
kono
parents:
diff changeset
440 -- Move one component
kono
parents:
diff changeset
441
kono
parents:
diff changeset
442 package Heap_Sort is new GNAT.Heap_Sort_G (Move, Lower_Than);
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 --------------
kono
parents:
diff changeset
445 -- Index_Of --
kono
parents:
diff changeset
446 --------------
kono
parents:
diff changeset
447
kono
parents:
diff changeset
448 function Index_Of (Idx : Natural) return Table_Index_Type'Base is
kono
parents:
diff changeset
449 J : constant Integer'Base :=
kono
parents:
diff changeset
450 Table_Index_Type'Base'Pos (First) + Idx - 1;
kono
parents:
diff changeset
451 begin
kono
parents:
diff changeset
452 return Table_Index_Type'Base'Val (J);
kono
parents:
diff changeset
453 end Index_Of;
kono
parents:
diff changeset
454
kono
parents:
diff changeset
455 ----------
kono
parents:
diff changeset
456 -- Move --
kono
parents:
diff changeset
457 ----------
kono
parents:
diff changeset
458
kono
parents:
diff changeset
459 procedure Move (From : Natural; To : Natural) is
kono
parents:
diff changeset
460 begin
kono
parents:
diff changeset
461 if From = 0 then
kono
parents:
diff changeset
462 Table.Table (Index_Of (To)) := Temp;
kono
parents:
diff changeset
463
kono
parents:
diff changeset
464 elsif To = 0 then
kono
parents:
diff changeset
465 Temp := Table.Table (Index_Of (From));
kono
parents:
diff changeset
466
kono
parents:
diff changeset
467 else
kono
parents:
diff changeset
468 Table.Table (Index_Of (To)) :=
kono
parents:
diff changeset
469 Table.Table (Index_Of (From));
kono
parents:
diff changeset
470 end if;
kono
parents:
diff changeset
471 end Move;
kono
parents:
diff changeset
472
kono
parents:
diff changeset
473 ----------------
kono
parents:
diff changeset
474 -- Lower_Than --
kono
parents:
diff changeset
475 ----------------
kono
parents:
diff changeset
476
kono
parents:
diff changeset
477 function Lower_Than (Op1, Op2 : Natural) return Boolean is
kono
parents:
diff changeset
478 begin
kono
parents:
diff changeset
479 if Op1 = 0 then
kono
parents:
diff changeset
480 return Lt (Temp, Table.Table (Index_Of (Op2)));
kono
parents:
diff changeset
481
kono
parents:
diff changeset
482 elsif Op2 = 0 then
kono
parents:
diff changeset
483 return Lt (Table.Table (Index_Of (Op1)), Temp);
kono
parents:
diff changeset
484
kono
parents:
diff changeset
485 else
kono
parents:
diff changeset
486 return
kono
parents:
diff changeset
487 Lt (Table.Table (Index_Of (Op1)), Table.Table (Index_Of (Op2)));
kono
parents:
diff changeset
488 end if;
kono
parents:
diff changeset
489 end Lower_Than;
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 -- Start of processing for Sort_Table
kono
parents:
diff changeset
492
kono
parents:
diff changeset
493 begin
kono
parents:
diff changeset
494 Heap_Sort.Sort (Natural (Last (Table) - First) + 1);
kono
parents:
diff changeset
495 end Sort_Table;
kono
parents:
diff changeset
496
kono
parents:
diff changeset
497 end GNAT.Dynamic_Tables;