1 ------------------------------------------------------------------------------
3 -- GNAT RUNTIME COMPONENTS --
5 -- G N A T . T A B L E --
9 -- Copyright (C) 1998-2001 Ada Core Technologies, Inc. --
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. --
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. --
29 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com). --
31 ------------------------------------------------------------------------------
33 -- Resizable one dimensional array support
35 -- This package provides an implementation of dynamically resizable one
36 -- dimensional arrays. The idea is to mimic the normal Ada semantics for
37 -- arrays as closely as possible with the one additional capability of
38 -- dynamically modifying the value of the Last attribute.
40 -- This package provides a facility similar to that of GNAT.Dynamic_Tables,
41 -- except that this package declares a single instance of the table type,
42 -- while an instantiation of GNAT.Dynamic_Tables creates a type that can be
43 -- used to define dynamic instances of the table.
45 -- Note that this interface should remain synchronized with those in
46 -- GNAT.Dynamic_Tables and the GNAT compiler source unit Table to keep
47 -- as much coherency as possible between these three related units.
50 type Table_Component_Type
is private;
51 type Table_Index_Type
is range <>;
53 Table_Low_Bound
: Table_Index_Type
;
54 Table_Initial
: Positive;
55 Table_Increment
: Natural;
58 pragma Elaborate_Body
(Table
);
60 -- Table_Component_Type and Table_Index_Type specify the type of the
61 -- array, Table_Low_Bound is the lower bound. Index_type must be an
62 -- integer type. The effect is roughly to declare:
64 -- Table : array (Table_Index_Type range Table_Low_Bound .. <>)
65 -- of Table_Component_Type;
67 -- Note: since the upper bound can be one less than the lower
68 -- bound for an empty array, the table index type must be able
69 -- to cover this range, e.g. if the lower bound is 1, then the
70 -- Table_Index_Type should be Natural rather than Positive.
72 -- Table_Component_Type may be any Ada type, except that controlled
73 -- types are not supported. Note however that default initialization
74 -- will NOT occur for array components.
76 -- The Table_Initial values controls the allocation of the table when
77 -- it is first allocated, either by default, or by an explicit Init call.
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).
83 -- The Last and Set_Last subprograms provide control over the current
84 -- logical allocation. They are quite efficient, so they can be used
85 -- freely (expensive reallocation occurs only at major granularity
86 -- chunks controlled by the allocation parameters).
88 -- Note: we do not make the table components aliased, since this would
89 -- restrict the use of table for discriminated types. If it is necessary
90 -- to take the access of a table element, use Unrestricted_Access.
93 array (Table_Index_Type
range <>) of Table_Component_Type
;
95 subtype Big_Table_Type
is
96 Table_Type
(Table_Low_Bound
.. Table_Index_Type
'Last);
97 -- We work with pointers to a bogus array type that is constrained
98 -- with the maximum possible range bound. This means that the pointer
99 -- is a thin pointer, which is more efficient. Since subscript checks
100 -- in any case must be on the logical, rather than physical bounds,
101 -- safety is not compromised by this approach.
103 type Table_Ptr
is access all Big_Table_Type
;
104 -- The table is actually represented as a pointer to allow reallocation
106 Table
: aliased Table_Ptr
:= null;
107 -- The table itself. The lower bound is the value of Low_Bound.
108 -- Logically the upper bound is the current value of Last (although
109 -- the actual size of the allocated table may be larger than this).
110 -- The program may only access and modify Table entries in the range
113 Locked
: Boolean := False;
114 -- Table expansion is permitted only if this switch is set to False. A
115 -- client may set Locked to True, in which case any attempt to expand
116 -- the table will cause an assertion failure. Note that while a table
117 -- is locked, its address in memory remains fixed and unchanging.
120 -- This procedure allocates a new table of size Initial (freeing any
121 -- previously allocated larger table). It is not necessary to call
122 -- Init when a table is first instantiated (since the instantiation does
123 -- the same initialization steps). However, it is harmless to do so, and
124 -- Init is convenient in reestablishing a table for new use.
126 function Last
return Table_Index_Type
;
127 pragma Inline
(Last
);
128 -- Returns the current value of the last used entry in the table, which
129 -- can then be used as a subscript for Table. Note that the only way to
130 -- modify Last is to call the Set_Last procedure. Last must always be
131 -- used to determine the logically last entry.
134 -- Storage is allocated in chunks according to the values given in the
135 -- Initial and Increment parameters. A call to Release releases all
136 -- storage that is allocated, but is not logically part of the current
137 -- array value. Current array values are not affected by this call.
140 -- Free all allocated memory for the table. A call to init is required
141 -- before any use of this table after calling Free.
143 First
: constant Table_Index_Type
:= Table_Low_Bound
;
144 -- Export First as synonym for Low_Bound (parallel with use of Last)
146 procedure Set_Last
(New_Val
: Table_Index_Type
);
147 pragma Inline
(Set_Last
);
148 -- This procedure sets Last to the indicated value. If necessary the
149 -- table is reallocated to accommodate the new value (i.e. on return
150 -- the allocated table has an upper bound of at least Last). If Set_Last
151 -- reduces the size of the table, then logically entries are removed
152 -- from the table. If Set_Last increases the size of the table, then
153 -- new entries are logically added to the table.
155 procedure Increment_Last
;
156 pragma Inline
(Increment_Last
);
157 -- Adds 1 to Last (same as Set_Last (Last + 1).
159 procedure Decrement_Last
;
160 pragma Inline
(Decrement_Last
);
161 -- Subtracts 1 from Last (same as Set_Last (Last - 1).
163 procedure Append
(New_Val
: Table_Component_Type
);
164 pragma Inline
(Append
);
167 -- x.Table (x.Last) := New_Val;
168 -- i.e. the table size is increased by one, and the given new item
169 -- stored in the newly created table element.
172 (Index
: Table_Index_Type
;
173 Item
: Table_Component_Type
);
174 pragma Inline
(Set_Item
);
175 -- Put Item in the table at position Index. The table is expanded if the
176 -- current table length is less than Index and in that case Last is set to
177 -- Index. Item will replace any value already present in the table at this
180 function Allocate
(Num
: Integer := 1) return Table_Index_Type
;
181 pragma Inline
(Allocate
);
182 -- Adds Num to Last, and returns the old value of Last + 1. Note that
183 -- this function has the possible side effect of reallocating the table.
184 -- This means that a reference X.Table (X.Allocate) is incorrect, since
185 -- the call to X.Allocate may modify the results of calling X.Table.