1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
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 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 2000-2009, AdaCore --
|
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, 51 Franklin Street, Fifth Floor, --
|
20 |
|
|
-- Boston, MA 02110-1301, 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 |
|
|
------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
-- Resizable one dimensional array support
|
35 |
|
|
|
36 |
|
|
-- This package provides an implementation of dynamically resizable one
|
37 |
|
|
-- dimensional arrays. The idea is to mimic the normal Ada semantics for
|
38 |
|
|
-- arrays as closely as possible with the one additional capability of
|
39 |
|
|
-- dynamically modifying the value of the Last attribute.
|
40 |
|
|
|
41 |
|
|
-- This package provides a facility similar to that of GNAT.Table, except
|
42 |
|
|
-- that this package declares a type that can be used to define dynamic
|
43 |
|
|
-- instances of the table, while an instantiation of GNAT.Table creates a
|
44 |
|
|
-- single instance of the table type.
|
45 |
|
|
|
46 |
|
|
-- Note that this interface should remain synchronized with those in
|
47 |
|
|
-- GNAT.Table and the GNAT compiler source unit Table to keep as much
|
48 |
|
|
-- coherency as possible between these three related units.
|
49 |
|
|
|
50 |
|
|
pragma Compiler_Unit;
|
51 |
|
|
|
52 |
|
|
generic
|
53 |
|
|
type Table_Component_Type is private;
|
54 |
|
|
type Table_Index_Type is range <>;
|
55 |
|
|
|
56 |
|
|
Table_Low_Bound : Table_Index_Type;
|
57 |
|
|
Table_Initial : Positive;
|
58 |
|
|
Table_Increment : Natural;
|
59 |
|
|
|
60 |
|
|
package GNAT.Dynamic_Tables is
|
61 |
|
|
|
62 |
|
|
-- Table_Component_Type and Table_Index_Type specify the type of the
|
63 |
|
|
-- array, Table_Low_Bound is the lower bound. Index_type must be an
|
64 |
|
|
-- integer type. The effect is roughly to declare:
|
65 |
|
|
|
66 |
|
|
-- Table : array (Table_Low_Bound .. <>) of Table_Component_Type;
|
67 |
|
|
|
68 |
|
|
-- Note: since the upper bound can be one less than the lower
|
69 |
|
|
-- bound for an empty array, the table index type must be able
|
70 |
|
|
-- to cover this range, e.g. if the lower bound is 1, then the
|
71 |
|
|
-- Table_Index_Type should be Natural rather than Positive.
|
72 |
|
|
|
73 |
|
|
-- Table_Component_Type may be any Ada type, except that controlled
|
74 |
|
|
-- types are not supported. Note however that default initialization
|
75 |
|
|
-- will NOT occur for array components.
|
76 |
|
|
|
77 |
|
|
-- The Table_Initial values controls the allocation of the table when
|
78 |
|
|
-- it is first allocated, either by default, or by an explicit Init
|
79 |
|
|
-- call.
|
80 |
|
|
|
81 |
|
|
-- The Table_Increment value controls the amount of increase, if the
|
82 |
|
|
-- table has to be increased in size. The value given is a percentage
|
83 |
|
|
-- value (e.g. 100 = increase table size by 100%, i.e. double it).
|
84 |
|
|
|
85 |
|
|
-- The Last and Set_Last subprograms provide control over the current
|
86 |
|
|
-- logical allocation. They are quite efficient, so they can be used
|
87 |
|
|
-- freely (expensive reallocation occurs only at major granularity
|
88 |
|
|
-- chunks controlled by the allocation parameters).
|
89 |
|
|
|
90 |
|
|
-- Note: we do not make the table components aliased, since this would
|
91 |
|
|
-- restrict the use of table for discriminated types. If it is necessary
|
92 |
|
|
-- to take the access of a table element, use Unrestricted_Access.
|
93 |
|
|
|
94 |
|
|
type Table_Type is
|
95 |
|
|
array (Table_Index_Type range <>) of Table_Component_Type;
|
96 |
|
|
subtype Big_Table_Type is
|
97 |
|
|
Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
|
98 |
|
|
-- We work with pointers to a bogus array type that is constrained with
|
99 |
|
|
-- the maximum possible range bound. This means that the pointer is a thin
|
100 |
|
|
-- pointer, which is more efficient. Since subscript checks in any case
|
101 |
|
|
-- must be on the logical, rather than physical bounds, safety is not
|
102 |
|
|
-- compromised by this approach. These types should not be used by the
|
103 |
|
|
-- client.
|
104 |
|
|
|
105 |
|
|
type Table_Ptr is access all Big_Table_Type;
|
106 |
|
|
for Table_Ptr'Storage_Size use 0;
|
107 |
|
|
-- The table is actually represented as a pointer to allow reallocation.
|
108 |
|
|
-- This type should not be used by the client.
|
109 |
|
|
|
110 |
|
|
type Table_Private is private;
|
111 |
|
|
-- Table private data that is not exported in Instance
|
112 |
|
|
|
113 |
|
|
type Instance is record
|
114 |
|
|
Table : aliased Table_Ptr := null;
|
115 |
|
|
-- The table itself. The lower bound is the value of Low_Bound.
|
116 |
|
|
-- Logically the upper bound is the current value of Last (although
|
117 |
|
|
-- the actual size of the allocated table may be larger than this).
|
118 |
|
|
-- The program may only access and modify Table entries in the
|
119 |
|
|
-- range First .. Last.
|
120 |
|
|
|
121 |
|
|
P : Table_Private;
|
122 |
|
|
end record;
|
123 |
|
|
|
124 |
|
|
procedure Init (T : in out Instance);
|
125 |
|
|
-- This procedure allocates a new table of size Initial (freeing any
|
126 |
|
|
-- previously allocated larger table). Init must be called before using
|
127 |
|
|
-- the table. Init is convenient in reestablishing a table for new use.
|
128 |
|
|
|
129 |
|
|
function Last (T : Instance) return Table_Index_Type;
|
130 |
|
|
pragma Inline (Last);
|
131 |
|
|
-- Returns the current value of the last used entry in the table,
|
132 |
|
|
-- which can then be used as a subscript for Table. Note that the
|
133 |
|
|
-- only way to modify Last is to call the Set_Last procedure. Last
|
134 |
|
|
-- must always be used to determine the logically last entry.
|
135 |
|
|
|
136 |
|
|
procedure Release (T : in out Instance);
|
137 |
|
|
-- Storage is allocated in chunks according to the values given in the
|
138 |
|
|
-- Initial and Increment parameters. A call to Release releases all
|
139 |
|
|
-- storage that is allocated, but is not logically part of the current
|
140 |
|
|
-- array value. Current array values are not affected by this call.
|
141 |
|
|
|
142 |
|
|
procedure Free (T : in out Instance);
|
143 |
|
|
-- Free all allocated memory for the table. A call to init is required
|
144 |
|
|
-- before any use of this table after calling Free.
|
145 |
|
|
|
146 |
|
|
First : constant Table_Index_Type := Table_Low_Bound;
|
147 |
|
|
-- Export First as synonym for Low_Bound (parallel with use of Last)
|
148 |
|
|
|
149 |
|
|
procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type);
|
150 |
|
|
pragma Inline (Set_Last);
|
151 |
|
|
-- This procedure sets Last to the indicated value. If necessary the
|
152 |
|
|
-- table is reallocated to accommodate the new value (i.e. on return
|
153 |
|
|
-- the allocated table has an upper bound of at least Last). If
|
154 |
|
|
-- Set_Last reduces the size of the table, then logically entries are
|
155 |
|
|
-- removed from the table. If Set_Last increases the size of the
|
156 |
|
|
-- table, then new entries are logically added to the table.
|
157 |
|
|
|
158 |
|
|
procedure Increment_Last (T : in out Instance);
|
159 |
|
|
pragma Inline (Increment_Last);
|
160 |
|
|
-- Adds 1 to Last (same as Set_Last (Last + 1)
|
161 |
|
|
|
162 |
|
|
procedure Decrement_Last (T : in out Instance);
|
163 |
|
|
pragma Inline (Decrement_Last);
|
164 |
|
|
-- Subtracts 1 from Last (same as Set_Last (Last - 1)
|
165 |
|
|
|
166 |
|
|
procedure Append (T : in out Instance; New_Val : Table_Component_Type);
|
167 |
|
|
pragma Inline (Append);
|
168 |
|
|
-- Equivalent to:
|
169 |
|
|
-- Increment_Last (T);
|
170 |
|
|
-- T.Table (T.Last) := New_Val;
|
171 |
|
|
-- i.e. the table size is increased by one, and the given new item
|
172 |
|
|
-- stored in the newly created table element.
|
173 |
|
|
|
174 |
|
|
procedure Append_All (T : in out Instance; New_Vals : Table_Type);
|
175 |
|
|
-- Appends all components of New_Vals
|
176 |
|
|
|
177 |
|
|
procedure Set_Item
|
178 |
|
|
(T : in out Instance;
|
179 |
|
|
Index : Table_Index_Type;
|
180 |
|
|
Item : Table_Component_Type);
|
181 |
|
|
pragma Inline (Set_Item);
|
182 |
|
|
-- Put Item in the table at position Index. The table is expanded if
|
183 |
|
|
-- current table length is less than Index and in that case Last is set to
|
184 |
|
|
-- Index. Item will replace any value already present in the table at this
|
185 |
|
|
-- position.
|
186 |
|
|
|
187 |
|
|
procedure Allocate (T : in out Instance; Num : Integer := 1);
|
188 |
|
|
pragma Inline (Allocate);
|
189 |
|
|
-- Adds Num to Last
|
190 |
|
|
|
191 |
|
|
generic
|
192 |
|
|
with procedure Action
|
193 |
|
|
(Index : Table_Index_Type;
|
194 |
|
|
Item : Table_Component_Type;
|
195 |
|
|
Quit : in out Boolean) is <>;
|
196 |
|
|
procedure For_Each (Table : Instance);
|
197 |
|
|
-- Calls procedure Action for each component of the table Table, or until
|
198 |
|
|
-- one of these calls set Quit to True.
|
199 |
|
|
|
200 |
|
|
generic
|
201 |
|
|
with function Lt (Comp1, Comp2 : Table_Component_Type) return Boolean;
|
202 |
|
|
procedure Sort_Table (Table : in out Instance);
|
203 |
|
|
-- This procedure sorts the components of table Table into ascending
|
204 |
|
|
-- order making calls to Lt to do required comparisons, and using
|
205 |
|
|
-- assignments to move components around. The Lt function returns True
|
206 |
|
|
-- if Comp1 is less than Comp2 (in the sense of the desired sort), and
|
207 |
|
|
-- False if Comp1 is greater than Comp2. For equal objects it does not
|
208 |
|
|
-- matter if True or False is returned (it is slightly more efficient
|
209 |
|
|
-- to return False). The sort is not stable (the order of equal items
|
210 |
|
|
-- in the table is not preserved).
|
211 |
|
|
|
212 |
|
|
private
|
213 |
|
|
type Table_Private is record
|
214 |
|
|
Max : Integer;
|
215 |
|
|
-- Subscript of the maximum entry in the currently allocated table
|
216 |
|
|
|
217 |
|
|
Length : Integer := 0;
|
218 |
|
|
-- Number of entries in currently allocated table. The value of zero
|
219 |
|
|
-- ensures that we initially allocate the table.
|
220 |
|
|
|
221 |
|
|
Last_Val : Integer;
|
222 |
|
|
-- Current value of Last
|
223 |
|
|
end record;
|
224 |
|
|
|
225 |
|
|
end GNAT.Dynamic_Tables;
|