1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
13 -- GNAT is free software; you can redistribute it and/or modify it under --
14 -- terms of the GNU General Public License as published by the Free Soft- --
15 -- ware Foundation; either version 2, or (at your option) any later ver- --
16 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
19 -- for more details. You should have received a copy of the GNU General --
20 -- Public License distributed with GNAT; see file COPYING. If not, write --
21 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
22 -- MA 02111-1307, USA. --
24 -- As a special exception, if other files instantiate generics from this --
25 -- unit, or you link this unit with other files to produce an executable, --
26 -- this unit does not by itself cause the resulting executable to be --
27 -- covered by the GNU General Public License. This exception does not --
28 -- however invalidate any other reasons why the executable file might be --
29 -- covered by the GNU Public License. --
31 -- GNAT was originally developed by the GNAT team at New York University. --
32 -- Extensive contributions were provided by Ada Core Technologies Inc. --
34 ------------------------------------------------------------------------------
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.
41 -- Note that this interface should remain synchronized with those in
42 -- GNAT.Table and GNAT.Dynamic_Tables to keep coherency between these
43 -- three related units.
45 with Types
; use Types
;
48 pragma Elaborate_Body
(Table
);
51 type Table_Component_Type
is private;
52 type Table_Index_Type
is range <>;
54 Table_Low_Bound
: Table_Index_Type
;
56 Table_Increment
: Nat
;
61 -- Table_Component_Type and Table_Index_Type specify the type of the
62 -- array, Table_Low_Bound is the lower bound. Index_type must be an
63 -- integer type. The effect is roughly to declare:
65 -- Table : array (Table_Index_Type range Table_Low_Bound .. <>)
66 -- of Table_Component_Type;
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.
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.
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. The value used is Opt.Table_Factor * Table_Initial.
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).
85 -- The Table_Name parameter is simply use in debug output messages it
86 -- has no other usage, and is not referenced in non-debugging mode.
88 -- The Last and Set_Last subprograms provide control over the current
89 -- logical allocation. They are quite efficient, so they can be used
90 -- freely (expensive reallocation occurs only at major granularity
91 -- chunks controlled by the allocation parameters).
93 -- Note: we do not make the table components aliased, since this would
94 -- restict the use of table for discriminated types. If it is necessary
95 -- to take the access of a table element, use Unrestricted_Access.
98 array (Table_Index_Type
range <>) of Table_Component_Type
;
100 subtype Big_Table_Type
is
101 Table_Type
(Table_Low_Bound
.. Table_Index_Type
'Last);
102 -- We work with pointers to a bogus array type that is constrained
103 -- with the maximum possible range bound. This means that the pointer
104 -- is a thin pointer, which is more efficient. Since subscript checks
105 -- in any case must be on the logical, rather than physical bounds,
106 -- safety is not compromised by this approach.
108 type Table_Ptr
is access all Big_Table_Type
;
109 -- The table is actually represented as a pointer to allow reallocation
111 Table
: aliased Table_Ptr
:= null;
112 -- The table itself. The lower bound is the value of Low_Bound.
113 -- Logically the upper bound is the current value of Last (although
114 -- the actual size of the allocated table may be larger than this).
115 -- The program may only access and modify Table entries in the range
118 Locked
: Boolean := False;
119 -- Table expansion is permitted only if this switch is set to False. A
120 -- client may set Locked to True, in which case any attempt to expand
121 -- the table will cause an assertion failure. Note that while a table
122 -- is locked, its address in memory remains fixed and unchanging. This
123 -- feature is used to control table expansion during Gigi processing.
124 -- Gigi assumes that tables other than the Uint and Ureal tables do
125 -- not move during processing, which means that they cannot be expanded.
126 -- The Locked flag is used to enforce this restriction.
129 -- This procedure allocates a new table of size Initial (freeing any
130 -- previously allocated larger table). It is not necessary to call
131 -- Init when a table is first instantiated (since the instantiation does
132 -- the same initialization steps). However, it is harmless to do so, and
133 -- Init is convenient in reestablishing a table for new use.
135 function Last
return Table_Index_Type
;
136 pragma Inline
(Last
);
137 -- Returns the current value of the last used entry in the table, which
138 -- can then be used as a subscript for Table. Note that the only way to
139 -- modify Last is to call the Set_Last procedure. Last must always be
140 -- used to determine the logically last entry.
143 -- Storage is allocated in chunks according to the values given in the
144 -- Initial and Increment parameters. A call to Release releases all
145 -- storage that is allocated, but is not logically part of the current
146 -- array value. Current array values are not affected by this call.
149 -- Free all allocated memory for the table. A call to init is required
150 -- before any use of this table after calling Free.
152 First
: constant Table_Index_Type
:= Table_Low_Bound
;
153 -- Export First as synonym for Low_Bound (parallel with use of Last)
155 procedure Set_Last
(New_Val
: Table_Index_Type
);
156 pragma Inline
(Set_Last
);
157 -- This procedure sets Last to the indicated value. If necessary the
158 -- table is reallocated to accommodate the new value (i.e. on return
159 -- the allocated table has an upper bound of at least Last). If Set_Last
160 -- reduces the size of the table, then logically entries are removed
161 -- from the table. If Set_Last increases the size of the table, then
162 -- new entries are logically added to the table.
164 procedure Increment_Last
;
165 pragma Inline
(Increment_Last
);
166 -- Adds 1 to Last (same as Set_Last (Last + 1).
168 procedure Decrement_Last
;
169 pragma Inline
(Decrement_Last
);
170 -- Subtracts 1 from Last (same as Set_Last (Last - 1).
172 procedure Append
(New_Val
: Table_Component_Type
);
173 pragma Inline
(Append
);
176 -- x.Table (x.Last) := New_Val;
177 -- i.e. the table size is increased by one, and the given new item
178 -- stored in the newly created table element.
181 (Index
: Table_Index_Type
;
182 Item
: Table_Component_Type
);
183 pragma Inline
(Set_Item
);
184 -- Put Item in the table at position Index. The table is expanded if
185 -- current table length is less than Index and in that case Last is set
186 -- to Index. Item will replace any value already present in the table
189 type Saved_Table
is private;
190 -- Type used for Save/Restore subprograms
192 function Save
return Saved_Table
;
193 -- Resets table to empty, but saves old contents of table in returned
194 -- value, for possible later restoration by a call to Restore.
196 procedure Restore
(T
: Saved_Table
);
197 -- Given a Saved_Table value returned by a prior call to Save, restores
198 -- the table to the state it was in at the time of the Save call.
200 procedure Tree_Write
;
201 -- Writes out contents of table using Tree_IO
204 -- Initializes table by reading contents previously written
205 -- with the Tree_Write call (also using Tree_IO)
210 -- Current value of Last. Note that we declare this in the private part
211 -- because we don't want the client to modify Last except through one of
212 -- the official interfaces (since a modification to Last may require a
213 -- reallocation of the table).
216 -- Subscript of the maximum entry in the currently allocated table
218 type Saved_Table
is record