annotate gcc/ada/table.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- T A B L E --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This package provides an implementation of dynamically resizable one
kono
parents:
diff changeset
33 -- dimensional arrays. The idea is to mimic the normal Ada semantics for
kono
parents:
diff changeset
34 -- arrays as closely as possible with the one additional capability of
kono
parents:
diff changeset
35 -- dynamically modifying the value of the Last attribute.
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 -- This package uses a very efficient memory management scheme and any
kono
parents:
diff changeset
38 -- change must be carefully evaluated on compilation of real software.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 -- Note that this interface should remain synchronized with those in
kono
parents:
diff changeset
41 -- GNAT.Table and GNAT.Dynamic_Tables to keep coherency between these
kono
parents:
diff changeset
42 -- three related units.
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 with Types; use Types;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 package Table is
kono
parents:
diff changeset
47 pragma Elaborate_Body;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 generic
kono
parents:
diff changeset
50 type Table_Component_Type is private;
kono
parents:
diff changeset
51 type Table_Index_Type is range <>;
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 Table_Low_Bound : Table_Index_Type;
kono
parents:
diff changeset
54 Table_Initial : Pos;
kono
parents:
diff changeset
55 Table_Increment : Nat;
kono
parents:
diff changeset
56 Table_Name : String;
kono
parents:
diff changeset
57 Release_Threshold : Nat := 0;
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 package Table is
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 -- Table_Component_Type and Table_Index_Type specify the type of the
kono
parents:
diff changeset
62 -- array, Table_Low_Bound is the lower bound. Table_Index_Type must be
kono
parents:
diff changeset
63 -- an integer type. The effect is roughly to declare:
kono
parents:
diff changeset
64
kono
parents:
diff changeset
65 -- Table : array (Table_Index_Type range Table_Low_Bound .. <>)
kono
parents:
diff changeset
66 -- of Table_Component_Type;
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 -- Note: since the upper bound can be one less than the lower
kono
parents:
diff changeset
69 -- bound for an empty array, the table index type must be able
kono
parents:
diff changeset
70 -- to cover this range, e.g. if the lower bound is 1, then the
kono
parents:
diff changeset
71 -- Table_Index_Type should be Natural rather than Positive.
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 -- Table_Component_Type may be any Ada type, except that controlled
kono
parents:
diff changeset
74 -- types are not supported. Note however that default initialization
kono
parents:
diff changeset
75 -- will NOT occur for array components.
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 -- The Table_Initial values controls the allocation of the table when
kono
parents:
diff changeset
78 -- it is first allocated, either by default, or by an explicit Init
kono
parents:
diff changeset
79 -- call. The value used is Opt.Table_Factor * Table_Initial.
kono
parents:
diff changeset
80
kono
parents:
diff changeset
81 -- The Table_Increment value controls the amount of increase, if the
kono
parents:
diff changeset
82 -- table has to be increased in size. The value given is a percentage
kono
parents:
diff changeset
83 -- value (e.g. 100 = increase table size by 100%, i.e. double it).
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 -- The Table_Name parameter is simply use in debug output messages it
kono
parents:
diff changeset
86 -- has no other usage, and is not referenced in non-debugging mode.
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 -- The Last and Set_Last subprograms provide control over the current
kono
parents:
diff changeset
89 -- logical allocation. They are quite efficient, so they can be used
kono
parents:
diff changeset
90 -- freely (expensive reallocation occurs only at major granularity
kono
parents:
diff changeset
91 -- chunks controlled by the allocation parameters).
kono
parents:
diff changeset
92
kono
parents:
diff changeset
93 -- Note: We do not make the table components aliased, since this would
kono
parents:
diff changeset
94 -- restrict the use of table for discriminated types. If it is necessary
kono
parents:
diff changeset
95 -- to take the access of a table element, use Unrestricted_Access.
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 -- WARNING: On HPPA, the virtual addressing approach used in this unit
kono
parents:
diff changeset
98 -- is incompatible with the indexing instructions on the HPPA. So when
kono
parents:
diff changeset
99 -- using this unit, compile your application with -mdisable-indexing.
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 -- WARNING: If the table is reallocated, then the address of all its
kono
parents:
diff changeset
102 -- components will change. So do not capture the address of an element
kono
parents:
diff changeset
103 -- and then use the address later after the table may be reallocated.
kono
parents:
diff changeset
104 -- One tricky case of this is passing an element of the table to a
kono
parents:
diff changeset
105 -- subprogram by reference where the table gets reallocated during
kono
parents:
diff changeset
106 -- the execution of the subprogram. The best rule to follow is never
kono
parents:
diff changeset
107 -- to pass a table element as a parameter except for the case of IN
kono
parents:
diff changeset
108 -- mode parameters with scalar values.
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 type Table_Type is
kono
parents:
diff changeset
111 array (Table_Index_Type range <>) of Table_Component_Type;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 subtype Big_Table_Type is
kono
parents:
diff changeset
114 Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
kono
parents:
diff changeset
115 -- We work with pointers to a bogus array type that is constrained
kono
parents:
diff changeset
116 -- with the maximum possible range bound. This means that the pointer
kono
parents:
diff changeset
117 -- is a thin pointer, which is more efficient. Since subscript checks
kono
parents:
diff changeset
118 -- in any case must be on the logical, rather than physical bounds,
kono
parents:
diff changeset
119 -- safety is not compromised by this approach.
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 type Table_Ptr is access all Big_Table_Type;
kono
parents:
diff changeset
122 for Table_Ptr'Storage_Size use 0;
kono
parents:
diff changeset
123 -- The table is actually represented as a pointer to allow reallocation
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 Table : aliased Table_Ptr := null;
kono
parents:
diff changeset
126 -- The table itself. The lower bound is the value of Low_Bound.
kono
parents:
diff changeset
127 -- Logically the upper bound is the current value of Last (although
kono
parents:
diff changeset
128 -- the actual size of the allocated table may be larger than this).
kono
parents:
diff changeset
129 -- The program may only access and modify Table entries in the range
kono
parents:
diff changeset
130 -- First .. Last.
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 Locked : Boolean := False;
kono
parents:
diff changeset
133 -- Table expansion is permitted only if this switch is set to False. A
kono
parents:
diff changeset
134 -- client may set Locked to True, in which case any attempt to expand
kono
parents:
diff changeset
135 -- the table will cause an assertion failure. Note that while a table
kono
parents:
diff changeset
136 -- is locked, its address in memory remains fixed and unchanging. This
kono
parents:
diff changeset
137 -- feature is used to control table expansion during Gigi processing.
kono
parents:
diff changeset
138 -- Gigi assumes that tables other than the Uint and Ureal tables do
kono
parents:
diff changeset
139 -- not move during processing, which means that they cannot be expanded.
kono
parents:
diff changeset
140 -- The Locked flag is used to enforce this restriction.
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 procedure Init;
kono
parents:
diff changeset
143 -- This procedure allocates a new table of size Initial (freeing any
kono
parents:
diff changeset
144 -- previously allocated larger table). It is not necessary to call
kono
parents:
diff changeset
145 -- Init when a table is first instantiated (since the instantiation does
kono
parents:
diff changeset
146 -- the same initialization steps). However, it is harmless to do so, and
kono
parents:
diff changeset
147 -- Init is convenient in reestablishing a table for new use.
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 function Last return Table_Index_Type;
kono
parents:
diff changeset
150 pragma Inline (Last);
kono
parents:
diff changeset
151 -- Returns the current value of the last used entry in the table, which
kono
parents:
diff changeset
152 -- can then be used as a subscript for Table. Note that the only way to
kono
parents:
diff changeset
153 -- modify Last is to call the Set_Last procedure. Last must always be
kono
parents:
diff changeset
154 -- used to determine the logically last entry.
kono
parents:
diff changeset
155
kono
parents:
diff changeset
156 procedure Release;
kono
parents:
diff changeset
157 -- Storage is allocated in chunks according to the values given in the
kono
parents:
diff changeset
158 -- Initial and Increment parameters. If Release_Threshold is 0 or the
kono
parents:
diff changeset
159 -- length of the table does not exceed this threshold then a call to
kono
parents:
diff changeset
160 -- Release releases all storage that is allocated, but is not logically
kono
parents:
diff changeset
161 -- part of the current array value; otherwise the call to Release leaves
kono
parents:
diff changeset
162 -- the current array value plus 0.1% of the current table length free
kono
parents:
diff changeset
163 -- elements located at the end of the table (this parameter facilitates
kono
parents:
diff changeset
164 -- reopening large tables and adding a few elements without allocating a
kono
parents:
diff changeset
165 -- chunk of memory). In both cases current array values are not affected
kono
parents:
diff changeset
166 -- by this call.
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 procedure Free;
kono
parents:
diff changeset
169 -- Free all allocated memory for the table. A call to init is required
kono
parents:
diff changeset
170 -- before any use of this table after calling Free.
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 First : constant Table_Index_Type := Table_Low_Bound;
kono
parents:
diff changeset
173 -- Export First as synonym for Low_Bound (parallel with use of Last)
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 procedure Set_Last (New_Val : Table_Index_Type);
kono
parents:
diff changeset
176 pragma Inline (Set_Last);
kono
parents:
diff changeset
177 -- This procedure sets Last to the indicated value. If necessary the
kono
parents:
diff changeset
178 -- table is reallocated to accommodate the new value (i.e. on return
kono
parents:
diff changeset
179 -- the allocated table has an upper bound of at least Last). If Set_Last
kono
parents:
diff changeset
180 -- reduces the size of the table, then logically entries are removed
kono
parents:
diff changeset
181 -- from the table. If Set_Last increases the size of the table, then
kono
parents:
diff changeset
182 -- new entries are logically added to the table.
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 procedure Increment_Last;
kono
parents:
diff changeset
185 pragma Inline (Increment_Last);
kono
parents:
diff changeset
186 -- Adds 1 to Last (same as Set_Last (Last + 1)
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 procedure Decrement_Last;
kono
parents:
diff changeset
189 pragma Inline (Decrement_Last);
kono
parents:
diff changeset
190 -- Subtracts 1 from Last (same as Set_Last (Last - 1)
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 procedure Append (New_Val : Table_Component_Type);
kono
parents:
diff changeset
193 pragma Inline (Append);
kono
parents:
diff changeset
194 -- Equivalent to:
kono
parents:
diff changeset
195 -- x.Increment_Last;
kono
parents:
diff changeset
196 -- x.Table (x.Last) := New_Val;
kono
parents:
diff changeset
197 -- i.e. the table size is increased by one, and the given new item
kono
parents:
diff changeset
198 -- stored in the newly created table element.
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 procedure Append_All (New_Vals : Table_Type);
kono
parents:
diff changeset
201 -- Appends all components of New_Vals
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 procedure Set_Item
kono
parents:
diff changeset
204 (Index : Table_Index_Type;
kono
parents:
diff changeset
205 Item : Table_Component_Type);
kono
parents:
diff changeset
206 pragma Inline (Set_Item);
kono
parents:
diff changeset
207 -- Put Item in the table at position Index. The table is expanded if
kono
parents:
diff changeset
208 -- current table length is less than Index and in that case Last is set
kono
parents:
diff changeset
209 -- to Index. Item will replace any value already present in the table
kono
parents:
diff changeset
210 -- at this position.
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 type Saved_Table is private;
kono
parents:
diff changeset
213 -- Type used for Save/Restore subprograms
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 function Save return Saved_Table;
kono
parents:
diff changeset
216 -- Resets table to empty, but saves old contents of table in returned
kono
parents:
diff changeset
217 -- value, for possible later restoration by a call to Restore.
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 procedure Restore (T : Saved_Table);
kono
parents:
diff changeset
220 -- Given a Saved_Table value returned by a prior call to Save, restores
kono
parents:
diff changeset
221 -- the table to the state it was in at the time of the Save call.
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 procedure Tree_Write;
kono
parents:
diff changeset
224 -- Writes out contents of table using Tree_IO
kono
parents:
diff changeset
225
kono
parents:
diff changeset
226 procedure Tree_Read;
kono
parents:
diff changeset
227 -- Initializes table by reading contents previously written with the
kono
parents:
diff changeset
228 -- Tree_Write call (also using Tree_IO).
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 private
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 Last_Val : Int;
kono
parents:
diff changeset
233 -- Current value of Last. Note that we declare this in the private part
kono
parents:
diff changeset
234 -- because we don't want the client to modify Last except through one of
kono
parents:
diff changeset
235 -- the official interfaces (since a modification to Last may require a
kono
parents:
diff changeset
236 -- reallocation of the table).
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 Max : Int;
kono
parents:
diff changeset
239 -- Subscript of the maximum entry in the currently allocated table
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 type Saved_Table is record
kono
parents:
diff changeset
242 Last_Val : Int;
kono
parents:
diff changeset
243 Max : Int;
kono
parents:
diff changeset
244 Table : Table_Ptr;
kono
parents:
diff changeset
245 end record;
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 end Table;
kono
parents:
diff changeset
248 end Table;