1 |
12 |
jlechner |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT COMPILER COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- T A B L E --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1992-2005, Free Software Foundation, 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, 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 |
|
|
-- This package provides an implementation of dynamically resizable one
|
35 |
|
|
-- dimensional arrays. The idea is to mimic the normal Ada semantics for
|
36 |
|
|
-- arrays as closely as possible with the one additional capability of
|
37 |
|
|
-- dynamically modifying the value of the Last attribute.
|
38 |
|
|
|
39 |
|
|
-- Note that this interface should remain synchronized with those in
|
40 |
|
|
-- GNAT.Table and GNAT.Dynamic_Tables to keep coherency between these
|
41 |
|
|
-- three related units.
|
42 |
|
|
|
43 |
|
|
with Types; use Types;
|
44 |
|
|
|
45 |
|
|
package Table is
|
46 |
|
|
pragma Elaborate_Body;
|
47 |
|
|
|
48 |
|
|
generic
|
49 |
|
|
type Table_Component_Type is private;
|
50 |
|
|
type Table_Index_Type is range <>;
|
51 |
|
|
|
52 |
|
|
Table_Low_Bound : Table_Index_Type;
|
53 |
|
|
Table_Initial : Pos;
|
54 |
|
|
Table_Increment : Nat;
|
55 |
|
|
Table_Name : String;
|
56 |
|
|
|
57 |
|
|
package Table is
|
58 |
|
|
|
59 |
|
|
-- Table_Component_Type and Table_Index_Type specify the type of the
|
60 |
|
|
-- array, Table_Low_Bound is the lower bound. Index_type must be an
|
61 |
|
|
-- integer type. The effect is roughly to declare:
|
62 |
|
|
|
63 |
|
|
-- Table : array (Table_Index_Type range Table_Low_Bound .. <>)
|
64 |
|
|
-- of Table_Component_Type;
|
65 |
|
|
|
66 |
|
|
-- Note: since the upper bound can be one less than the lower
|
67 |
|
|
-- bound for an empty array, the table index type must be able
|
68 |
|
|
-- to cover this range, e.g. if the lower bound is 1, then the
|
69 |
|
|
-- Table_Index_Type should be Natural rather than Positive.
|
70 |
|
|
|
71 |
|
|
-- Table_Component_Type may be any Ada type, except that controlled
|
72 |
|
|
-- types are not supported. Note however that default initialization
|
73 |
|
|
-- will NOT occur for array components.
|
74 |
|
|
|
75 |
|
|
-- The Table_Initial values controls the allocation of the table when
|
76 |
|
|
-- it is first allocated, either by default, or by an explicit Init
|
77 |
|
|
-- call. The value used is Opt.Table_Factor * Table_Initial.
|
78 |
|
|
|
79 |
|
|
-- The Table_Increment value controls the amount of increase, if the
|
80 |
|
|
-- table has to be increased in size. The value given is a percentage
|
81 |
|
|
-- value (e.g. 100 = increase table size by 100%, i.e. double it).
|
82 |
|
|
|
83 |
|
|
-- The Table_Name parameter is simply use in debug output messages it
|
84 |
|
|
-- has no other usage, and is not referenced in non-debugging mode.
|
85 |
|
|
|
86 |
|
|
-- The Last and Set_Last subprograms provide control over the current
|
87 |
|
|
-- logical allocation. They are quite efficient, so they can be used
|
88 |
|
|
-- freely (expensive reallocation occurs only at major granularity
|
89 |
|
|
-- chunks controlled by the allocation parameters).
|
90 |
|
|
|
91 |
|
|
-- Note: We do not make the table components aliased, since this would
|
92 |
|
|
-- restict the use of table for discriminated types. If it is necessary
|
93 |
|
|
-- to take the access of a table element, use Unrestricted_Access.
|
94 |
|
|
|
95 |
|
|
-- WARNING: On HPPA, the virtual addressing approach used in this unit
|
96 |
|
|
-- is incompatible with the indexing instructions on the HPPA. So when
|
97 |
|
|
-- using this unit, compile your application with -mdisable-indexing.
|
98 |
|
|
|
99 |
|
|
-- WARNING: If the table is reallocated, then the address of all its
|
100 |
|
|
-- components will change. So do not capture the address of an element
|
101 |
|
|
-- and then use the address later after the table may be reallocated.
|
102 |
|
|
-- One tricky case of this is passing an element of the table to a
|
103 |
|
|
-- subprogram by reference where the table gets reallocated during
|
104 |
|
|
-- the execution of the subprogram. The best rule to follow is never
|
105 |
|
|
-- to pass a table element as a parameter except for the case of IN
|
106 |
|
|
-- mode parameters with scalar values.
|
107 |
|
|
|
108 |
|
|
type Table_Type is
|
109 |
|
|
array (Table_Index_Type range <>) of Table_Component_Type;
|
110 |
|
|
|
111 |
|
|
subtype Big_Table_Type is
|
112 |
|
|
Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
|
113 |
|
|
-- We work with pointers to a bogus array type that is constrained
|
114 |
|
|
-- with the maximum possible range bound. This means that the pointer
|
115 |
|
|
-- is a thin pointer, which is more efficient. Since subscript checks
|
116 |
|
|
-- in any case must be on the logical, rather than physical bounds,
|
117 |
|
|
-- safety is not compromised by this approach.
|
118 |
|
|
|
119 |
|
|
type Table_Ptr is access all Big_Table_Type;
|
120 |
|
|
-- The table is actually represented as a pointer to allow reallocation
|
121 |
|
|
|
122 |
|
|
Table : aliased Table_Ptr := null;
|
123 |
|
|
-- The table itself. The lower bound is the value of Low_Bound.
|
124 |
|
|
-- Logically the upper bound is the current value of Last (although
|
125 |
|
|
-- the actual size of the allocated table may be larger than this).
|
126 |
|
|
-- The program may only access and modify Table entries in the range
|
127 |
|
|
-- First .. Last.
|
128 |
|
|
|
129 |
|
|
Locked : Boolean := False;
|
130 |
|
|
-- Table expansion is permitted only if this switch is set to False. A
|
131 |
|
|
-- client may set Locked to True, in which case any attempt to expand
|
132 |
|
|
-- the table will cause an assertion failure. Note that while a table
|
133 |
|
|
-- is locked, its address in memory remains fixed and unchanging. This
|
134 |
|
|
-- feature is used to control table expansion during Gigi processing.
|
135 |
|
|
-- Gigi assumes that tables other than the Uint and Ureal tables do
|
136 |
|
|
-- not move during processing, which means that they cannot be expanded.
|
137 |
|
|
-- The Locked flag is used to enforce this restriction.
|
138 |
|
|
|
139 |
|
|
procedure Init;
|
140 |
|
|
-- This procedure allocates a new table of size Initial (freeing any
|
141 |
|
|
-- previously allocated larger table). It is not necessary to call
|
142 |
|
|
-- Init when a table is first instantiated (since the instantiation does
|
143 |
|
|
-- the same initialization steps). However, it is harmless to do so, and
|
144 |
|
|
-- Init is convenient in reestablishing a table for new use.
|
145 |
|
|
|
146 |
|
|
function Last return Table_Index_Type;
|
147 |
|
|
pragma Inline (Last);
|
148 |
|
|
-- Returns the current value of the last used entry in the table, which
|
149 |
|
|
-- can then be used as a subscript for Table. Note that the only way to
|
150 |
|
|
-- modify Last is to call the Set_Last procedure. Last must always be
|
151 |
|
|
-- used to determine the logically last entry.
|
152 |
|
|
|
153 |
|
|
procedure Release;
|
154 |
|
|
-- Storage is allocated in chunks according to the values given in the
|
155 |
|
|
-- Initial and Increment parameters. A call to Release releases all
|
156 |
|
|
-- storage that is allocated, but is not logically part of the current
|
157 |
|
|
-- array value. Current array values are not affected by this call.
|
158 |
|
|
|
159 |
|
|
procedure Free;
|
160 |
|
|
-- Free all allocated memory for the table. A call to init is required
|
161 |
|
|
-- before any use of this table after calling Free.
|
162 |
|
|
|
163 |
|
|
First : constant Table_Index_Type := Table_Low_Bound;
|
164 |
|
|
-- Export First as synonym for Low_Bound (parallel with use of Last)
|
165 |
|
|
|
166 |
|
|
procedure Set_Last (New_Val : Table_Index_Type);
|
167 |
|
|
pragma Inline (Set_Last);
|
168 |
|
|
-- This procedure sets Last to the indicated value. If necessary the
|
169 |
|
|
-- table is reallocated to accommodate the new value (i.e. on return
|
170 |
|
|
-- the allocated table has an upper bound of at least Last). If Set_Last
|
171 |
|
|
-- reduces the size of the table, then logically entries are removed
|
172 |
|
|
-- from the table. If Set_Last increases the size of the table, then
|
173 |
|
|
-- new entries are logically added to the table.
|
174 |
|
|
|
175 |
|
|
procedure Increment_Last;
|
176 |
|
|
pragma Inline (Increment_Last);
|
177 |
|
|
-- Adds 1 to Last (same as Set_Last (Last + 1)
|
178 |
|
|
|
179 |
|
|
procedure Decrement_Last;
|
180 |
|
|
pragma Inline (Decrement_Last);
|
181 |
|
|
-- Subtracts 1 from Last (same as Set_Last (Last - 1)
|
182 |
|
|
|
183 |
|
|
procedure Append (New_Val : Table_Component_Type);
|
184 |
|
|
pragma Inline (Append);
|
185 |
|
|
-- Equivalent to:
|
186 |
|
|
-- x.Increment_Last;
|
187 |
|
|
-- x.Table (x.Last) := New_Val;
|
188 |
|
|
-- i.e. the table size is increased by one, and the given new item
|
189 |
|
|
-- stored in the newly created table element.
|
190 |
|
|
|
191 |
|
|
procedure Set_Item
|
192 |
|
|
(Index : Table_Index_Type;
|
193 |
|
|
Item : Table_Component_Type);
|
194 |
|
|
pragma Inline (Set_Item);
|
195 |
|
|
-- Put Item in the table at position Index. The table is expanded if
|
196 |
|
|
-- current table length is less than Index and in that case Last is set
|
197 |
|
|
-- to Index. Item will replace any value already present in the table
|
198 |
|
|
-- at this position.
|
199 |
|
|
|
200 |
|
|
type Saved_Table is private;
|
201 |
|
|
-- Type used for Save/Restore subprograms
|
202 |
|
|
|
203 |
|
|
function Save return Saved_Table;
|
204 |
|
|
-- Resets table to empty, but saves old contents of table in returned
|
205 |
|
|
-- value, for possible later restoration by a call to Restore.
|
206 |
|
|
|
207 |
|
|
procedure Restore (T : Saved_Table);
|
208 |
|
|
-- Given a Saved_Table value returned by a prior call to Save, restores
|
209 |
|
|
-- the table to the state it was in at the time of the Save call.
|
210 |
|
|
|
211 |
|
|
procedure Tree_Write;
|
212 |
|
|
-- Writes out contents of table using Tree_IO
|
213 |
|
|
|
214 |
|
|
procedure Tree_Read;
|
215 |
|
|
-- Initializes table by reading contents previously written
|
216 |
|
|
-- with the Tree_Write call (also using Tree_IO)
|
217 |
|
|
|
218 |
|
|
private
|
219 |
|
|
|
220 |
|
|
Last_Val : Int;
|
221 |
|
|
-- Current value of Last. Note that we declare this in the private part
|
222 |
|
|
-- because we don't want the client to modify Last except through one of
|
223 |
|
|
-- the official interfaces (since a modification to Last may require a
|
224 |
|
|
-- reallocation of the table).
|
225 |
|
|
|
226 |
|
|
Max : Int;
|
227 |
|
|
-- Subscript of the maximum entry in the currently allocated table
|
228 |
|
|
|
229 |
|
|
type Saved_Table is record
|
230 |
|
|
Last_Val : Int;
|
231 |
|
|
Max : Int;
|
232 |
|
|
Table : Table_Ptr;
|
233 |
|
|
end record;
|
234 |
|
|
|
235 |
|
|
end Table;
|
236 |
|
|
end Table;
|