Daily bump.
[official-gcc.git] / gcc / ada / g-dyntab.ads
blob147ab4103b51f275e1d1c7b4be47fffb9cfd2dae
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . D Y N A M I C _ T A B L E S --
6 -- --
7 -- S p e c --
8 -- --
9 -- Copyright (C) 2000-2010, 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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- Resizable one dimensional array support
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.
39 -- This package provides a facility similar to that of GNAT.Table, except
40 -- that this package declares a type that can be used to define dynamic
41 -- instances of the table, while an instantiation of GNAT.Table creates a
42 -- single instance of the table type.
44 -- Note that this interface should remain synchronized with those in
45 -- GNAT.Table and the GNAT compiler source unit Table to keep as much
46 -- coherency as possible between these three related units.
48 pragma Compiler_Unit;
50 generic
51 type Table_Component_Type is private;
52 type Table_Index_Type is range <>;
54 Table_Low_Bound : Table_Index_Type;
55 Table_Initial : Positive;
56 Table_Increment : Natural;
58 package GNAT.Dynamic_Tables is
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_Low_Bound .. <>) of Table_Component_Type;
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.
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.
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.
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.
92 type Table_Type is
93 array (Table_Index_Type range <>) of Table_Component_Type;
94 subtype Big_Table_Type is
95 Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
96 -- We work with pointers to a bogus array type that is constrained with
97 -- the maximum possible range bound. This means that the pointer is a thin
98 -- pointer, which is more efficient. Since subscript checks in any case
99 -- must be on the logical, rather than physical bounds, safety is not
100 -- compromised by this approach. These types should not be used by the
101 -- client.
103 type Table_Ptr is access all Big_Table_Type;
104 for Table_Ptr'Storage_Size use 0;
105 -- The table is actually represented as a pointer to allow reallocation.
106 -- This type should not be used by the client.
108 type Table_Private is private;
109 -- Table private data that is not exported in Instance
111 type Instance is record
112 Table : aliased Table_Ptr := null;
113 -- The table itself. The lower bound is the value of Low_Bound.
114 -- Logically the upper bound is the current value of Last (although
115 -- the actual size of the allocated table may be larger than this).
116 -- The program may only access and modify Table entries in the
117 -- range First .. Last.
119 P : Table_Private;
120 end record;
122 procedure Init (T : in out Instance);
123 -- This procedure allocates a new table of size Initial (freeing any
124 -- previously allocated larger table). Init must be called before using
125 -- the table. Init is convenient in reestablishing a table for new use.
127 function Last (T : Instance) return Table_Index_Type;
128 pragma Inline (Last);
129 -- Returns the current value of the last used entry in the table,
130 -- which can then be used as a subscript for Table. Note that the
131 -- only way to modify Last is to call the Set_Last procedure. Last
132 -- must always be used to determine the logically last entry.
134 procedure Release (T : in out Instance);
135 -- Storage is allocated in chunks according to the values given in the
136 -- Initial and Increment parameters. A call to Release releases all
137 -- storage that is allocated, but is not logically part of the current
138 -- array value. Current array values are not affected by this call.
140 procedure Free (T : in out Instance);
141 -- Free all allocated memory for the table. A call to init is required
142 -- before any use of this table after calling Free.
144 First : constant Table_Index_Type := Table_Low_Bound;
145 -- Export First as synonym for Low_Bound (parallel with use of Last)
147 procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type);
148 pragma Inline (Set_Last);
149 -- This procedure sets Last to the indicated value. If necessary the
150 -- table is reallocated to accommodate the new value (i.e. on return
151 -- the allocated table has an upper bound of at least Last). If
152 -- Set_Last reduces the size of the table, then logically entries are
153 -- removed from the table. If Set_Last increases the size of the
154 -- table, then new entries are logically added to the table.
156 procedure Increment_Last (T : in out Instance);
157 pragma Inline (Increment_Last);
158 -- Adds 1 to Last (same as Set_Last (Last + 1)
160 procedure Decrement_Last (T : in out Instance);
161 pragma Inline (Decrement_Last);
162 -- Subtracts 1 from Last (same as Set_Last (Last - 1)
164 procedure Append (T : in out Instance; New_Val : Table_Component_Type);
165 pragma Inline (Append);
166 -- Equivalent to:
167 -- Increment_Last (T);
168 -- T.Table (T.Last) := New_Val;
169 -- i.e. the table size is increased by one, and the given new item
170 -- stored in the newly created table element.
172 procedure Append_All (T : in out Instance; New_Vals : Table_Type);
173 -- Appends all components of New_Vals
175 procedure Set_Item
176 (T : in out Instance;
177 Index : Table_Index_Type;
178 Item : Table_Component_Type);
179 pragma Inline (Set_Item);
180 -- Put Item in the table at position Index. The table is expanded if
181 -- current table length is less than Index and in that case Last is set to
182 -- Index. Item will replace any value already present in the table at this
183 -- position.
185 procedure Allocate (T : in out Instance; Num : Integer := 1);
186 pragma Inline (Allocate);
187 -- Adds Num to Last
189 generic
190 with procedure Action
191 (Index : Table_Index_Type;
192 Item : Table_Component_Type;
193 Quit : in out Boolean) is <>;
194 procedure For_Each (Table : Instance);
195 -- Calls procedure Action for each component of the table Table, or until
196 -- one of these calls set Quit to True.
198 generic
199 with function Lt (Comp1, Comp2 : Table_Component_Type) return Boolean;
200 procedure Sort_Table (Table : in out Instance);
201 -- This procedure sorts the components of table Table into ascending
202 -- order making calls to Lt to do required comparisons, and using
203 -- assignments to move components around. The Lt function returns True
204 -- if Comp1 is less than Comp2 (in the sense of the desired sort), and
205 -- False if Comp1 is greater than Comp2. For equal objects it does not
206 -- matter if True or False is returned (it is slightly more efficient
207 -- to return False). The sort is not stable (the order of equal items
208 -- in the table is not preserved).
210 private
211 type Table_Private is record
212 Max : Integer;
213 -- Subscript of the maximum entry in the currently allocated table
215 Length : Integer := 0;
216 -- Number of entries in currently allocated table. The value of zero
217 -- ensures that we initially allocate the table.
219 Last_Val : Integer;
220 -- Current value of Last
221 end record;
223 end GNAT.Dynamic_Tables;