1 |
281 |
jeremybenn |
------------------------------------------------------------------------------
|
2 |
|
|
-- --
|
3 |
|
|
-- GNAT RUN-TIME COMPONENTS --
|
4 |
|
|
-- --
|
5 |
|
|
-- G N A T . T A B L E --
|
6 |
|
|
-- --
|
7 |
|
|
-- S p e c --
|
8 |
|
|
-- --
|
9 |
|
|
-- Copyright (C) 1998-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.Dynamic_Tables,
|
42 |
|
|
-- except that this package declares a single instance of the table type,
|
43 |
|
|
-- while an instantiation of GNAT.Dynamic_Tables creates a type that can be
|
44 |
|
|
-- used to define dynamic instances of the table.
|
45 |
|
|
|
46 |
|
|
-- Note that this interface should remain synchronized with those in
|
47 |
|
|
-- GNAT.Dynamic_Tables and the GNAT compiler source unit Table to keep
|
48 |
|
|
-- as much coherency as possible between these three related units.
|
49 |
|
|
|
50 |
|
|
generic
|
51 |
|
|
type Table_Component_Type is private;
|
52 |
|
|
type Table_Index_Type is range <>;
|
53 |
|
|
|
54 |
|
|
Table_Low_Bound : Table_Index_Type;
|
55 |
|
|
Table_Initial : Positive;
|
56 |
|
|
Table_Increment : Natural;
|
57 |
|
|
|
58 |
|
|
package GNAT.Table is
|
59 |
|
|
pragma Elaborate_Body;
|
60 |
|
|
|
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:
|
64 |
|
|
|
65 |
|
|
-- Table : array (Table_Index_Type range Table_Low_Bound .. <>)
|
66 |
|
|
-- 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 call.
|
79 |
|
|
|
80 |
|
|
-- The Table_Increment value controls the amount of increase, if the
|
81 |
|
|
-- table has to be increased in size. The value given is a percentage
|
82 |
|
|
-- value (e.g. 100 = increase table size by 100%, i.e. double it).
|
83 |
|
|
|
84 |
|
|
-- The Last and Set_Last subprograms provide control over the current
|
85 |
|
|
-- logical allocation. They are quite efficient, so they can be used
|
86 |
|
|
-- freely (expensive reallocation occurs only at major granularity
|
87 |
|
|
-- chunks controlled by the allocation parameters).
|
88 |
|
|
|
89 |
|
|
-- Note: we do not make the table components aliased, since this would
|
90 |
|
|
-- restrict the use of table for discriminated types. If it is necessary
|
91 |
|
|
-- to take the access of a table element, use Unrestricted_Access.
|
92 |
|
|
|
93 |
|
|
-- WARNING: On HPPA, the virtual addressing approach used in this unit
|
94 |
|
|
-- is incompatible with the indexing instructions on the HPPA. So when
|
95 |
|
|
-- using this unit, compile your application with -mdisable-indexing.
|
96 |
|
|
|
97 |
|
|
-- WARNING: If the table is reallocated, then the address of all its
|
98 |
|
|
-- components will change. So do not capture the address of an element
|
99 |
|
|
-- and then use the address later after the table may be reallocated.
|
100 |
|
|
-- One tricky case of this is passing an element of the table to a
|
101 |
|
|
-- subprogram by reference where the table gets reallocated during
|
102 |
|
|
-- the execution of the subprogram. The best rule to follow is never
|
103 |
|
|
-- to pass a table element as a parameter except for the case of IN
|
104 |
|
|
-- mode parameters with scalar values.
|
105 |
|
|
|
106 |
|
|
type Table_Type is
|
107 |
|
|
array (Table_Index_Type range <>) of Table_Component_Type;
|
108 |
|
|
subtype Big_Table_Type is
|
109 |
|
|
Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
|
110 |
|
|
-- We work with pointers to a bogus array type that is constrained
|
111 |
|
|
-- with the maximum possible range bound. This means that the pointer
|
112 |
|
|
-- is a thin pointer, which is more efficient. Since subscript checks
|
113 |
|
|
-- in any case must be on the logical, rather than physical bounds,
|
114 |
|
|
-- safety is not compromised by this approach. These types should never
|
115 |
|
|
-- be used by the client.
|
116 |
|
|
|
117 |
|
|
type Table_Ptr is access all Big_Table_Type;
|
118 |
|
|
for Table_Ptr'Storage_Size use 0;
|
119 |
|
|
-- The table is actually represented as a pointer to allow reallocation.
|
120 |
|
|
-- This type should never be used by the client.
|
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.
|
134 |
|
|
|
135 |
|
|
procedure Init;
|
136 |
|
|
-- This procedure allocates a new table of size Initial (freeing any
|
137 |
|
|
-- previously allocated larger table). It is not necessary to call
|
138 |
|
|
-- Init when a table is first instantiated (since the instantiation does
|
139 |
|
|
-- the same initialization steps). However, it is harmless to do so, and
|
140 |
|
|
-- Init is convenient in reestablishing a table for new use.
|
141 |
|
|
|
142 |
|
|
function Last return Table_Index_Type;
|
143 |
|
|
pragma Inline (Last);
|
144 |
|
|
-- Returns the current value of the last used entry in the table, which
|
145 |
|
|
-- can then be used as a subscript for Table. Note that the only way to
|
146 |
|
|
-- modify Last is to call the Set_Last procedure. Last must always be
|
147 |
|
|
-- used to determine the logically last entry.
|
148 |
|
|
|
149 |
|
|
procedure Release;
|
150 |
|
|
-- Storage is allocated in chunks according to the values given in the
|
151 |
|
|
-- Initial and Increment parameters. A call to Release releases all
|
152 |
|
|
-- storage that is allocated, but is not logically part of the current
|
153 |
|
|
-- array value. Current array values are not affected by this call.
|
154 |
|
|
|
155 |
|
|
procedure Free;
|
156 |
|
|
-- Free all allocated memory for the table. A call to Init is required
|
157 |
|
|
-- before any use of this table after calling Free.
|
158 |
|
|
|
159 |
|
|
First : constant Table_Index_Type := Table_Low_Bound;
|
160 |
|
|
-- Export First as synonym for Low_Bound (parallel with use of Last)
|
161 |
|
|
|
162 |
|
|
procedure Set_Last (New_Val : Table_Index_Type);
|
163 |
|
|
pragma Inline (Set_Last);
|
164 |
|
|
-- This procedure sets Last to the indicated value. If necessary the
|
165 |
|
|
-- table is reallocated to accommodate the new value (i.e. on return
|
166 |
|
|
-- the allocated table has an upper bound of at least Last). If Set_Last
|
167 |
|
|
-- reduces the size of the table, then logically entries are removed
|
168 |
|
|
-- from the table. If Set_Last increases the size of the table, then
|
169 |
|
|
-- new entries are logically added to the table.
|
170 |
|
|
|
171 |
|
|
procedure Increment_Last;
|
172 |
|
|
pragma Inline (Increment_Last);
|
173 |
|
|
-- Adds 1 to Last (same as Set_Last (Last + 1)
|
174 |
|
|
|
175 |
|
|
procedure Decrement_Last;
|
176 |
|
|
pragma Inline (Decrement_Last);
|
177 |
|
|
-- Subtracts 1 from Last (same as Set_Last (Last - 1)
|
178 |
|
|
|
179 |
|
|
procedure Append (New_Val : Table_Component_Type);
|
180 |
|
|
pragma Inline (Append);
|
181 |
|
|
-- Equivalent to:
|
182 |
|
|
-- x.Increment_Last;
|
183 |
|
|
-- x.Table (x.Last) := New_Val;
|
184 |
|
|
-- i.e. the table size is increased by one, and the given new item
|
185 |
|
|
-- stored in the newly created table element.
|
186 |
|
|
|
187 |
|
|
procedure Append_All (New_Vals : Table_Type);
|
188 |
|
|
-- Appends all components of New_Vals
|
189 |
|
|
|
190 |
|
|
procedure Set_Item
|
191 |
|
|
(Index : Table_Index_Type;
|
192 |
|
|
Item : Table_Component_Type);
|
193 |
|
|
pragma Inline (Set_Item);
|
194 |
|
|
-- Put Item in the table at position Index. The table is expanded if the
|
195 |
|
|
-- current table length is less than Index and in that case Last is set to
|
196 |
|
|
-- Index. Item will replace any value already present in the table at this
|
197 |
|
|
-- position.
|
198 |
|
|
|
199 |
|
|
function Allocate (Num : Integer := 1) return Table_Index_Type;
|
200 |
|
|
pragma Inline (Allocate);
|
201 |
|
|
-- Adds Num to Last, and returns the old value of Last + 1. Note that
|
202 |
|
|
-- this function has the possible side effect of reallocating the table.
|
203 |
|
|
-- This means that a reference X.Table (X.Allocate) is incorrect, since
|
204 |
|
|
-- the call to X.Allocate may modify the results of calling X.Table.
|
205 |
|
|
|
206 |
|
|
end GNAT.Table;
|