1------------------------------------------------------------------------------
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-2013, 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------------------------------------------------------------------------------
31
32--  Resizable one dimensional array support
33
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.
38
39--  This package provides a facility similar to that of GNAT.Dynamic_Tables,
40--  except that this package declares a single instance of the table type,
41--  while an instantiation of GNAT.Dynamic_Tables creates a type that can be
42--  used to define dynamic instances of the table.
43
44--  Note that this interface should remain synchronized with those in
45--  GNAT.Dynamic_Tables and the GNAT compiler source unit Table to keep
46--  as much coherency as possible between these three related units.
47
48generic
49   type Table_Component_Type is private;
50   type Table_Index_Type     is range <>;
51
52   Table_Low_Bound : Table_Index_Type;
53   Table_Initial   : Positive;
54   Table_Increment : Natural;
55
56package GNAT.Table is
57   pragma Elaborate_Body;
58
59   --  Table_Component_Type and Table_Index_Type specify the type of the
60   --  array, Table_Low_Bound is the lower bound. Index_type must be an
61   --  integer type. The effect is roughly to declare:
62
63   --    Table : array (Table_Index_Type range Table_Low_Bound .. <>)
64   --                       of Table_Component_Type;
65
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.
70
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.
74
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 call.
77
78   --  The Table_Increment value controls the amount of increase, if the
79   --  table has to be increased in size. The value given is a percentage
80   --  value (e.g. 100 = increase table size by 100%, i.e. double it).
81
82   --  The Last and Set_Last subprograms provide control over the current
83   --  logical allocation. They are quite efficient, so they can be used
84   --  freely (expensive reallocation occurs only at major granularity
85   --  chunks controlled by the allocation parameters).
86
87   --  Note: we do not make the table components aliased, since this would
88   --  restrict the use of table for discriminated types. If it is necessary
89   --  to take the access of a table element, use Unrestricted_Access.
90
91   --  WARNING: On HPPA, the virtual addressing approach used in this unit
92   --  is incompatible with the indexing instructions on the HPPA. So when
93   --  using this unit, compile your application with -mdisable-indexing.
94
95   --  WARNING: If the table is reallocated, then the address of all its
96   --  components will change. So do not capture the address of an element
97   --  and then use the address later after the table may be reallocated.
98   --  One tricky case of this is passing an element of the table to a
99   --  subprogram by reference where the table gets reallocated during
100   --  the execution of the subprogram. The best rule to follow is never
101   --  to pass a table element as a parameter except for the case of IN
102   --  mode parameters with scalar values.
103
104   type Table_Type is
105     array (Table_Index_Type range <>) of Table_Component_Type;
106   subtype Big_Table_Type is
107     Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
108   --  We work with pointers to a bogus array type that is constrained
109   --  with the maximum possible range bound. This means that the pointer
110   --  is a thin pointer, which is more efficient. Since subscript checks
111   --  in any case must be on the logical, rather than physical bounds,
112   --  safety is not compromised by this approach. These types should never
113   --  be used by the client.
114
115   type Table_Ptr is access all Big_Table_Type;
116   for Table_Ptr'Storage_Size use 0;
117   --  The table is actually represented as a pointer to allow reallocation.
118   --  This type should never be used by the client.
119
120   Table : aliased Table_Ptr := null;
121   --  The table itself. The lower bound is the value of Low_Bound.
122   --  Logically the upper bound is the current value of Last (although
123   --  the actual size of the allocated table may be larger than this).
124   --  The program may only access and modify Table entries in the range
125   --  First .. Last.
126
127   Locked : Boolean := False;
128   --  Table expansion is permitted only if this switch is set to False. A
129   --  client may set Locked to True, in which case any attempt to expand
130   --  the table will cause an assertion failure. Note that while a table
131   --  is locked, its address in memory remains fixed and unchanging.
132
133   procedure Init;
134   --  This procedure allocates a new table of size Initial (freeing any
135   --  previously allocated larger table). It is not necessary to call
136   --  Init when a table is first instantiated (since the instantiation does
137   --  the same initialization steps). However, it is harmless to do so, and
138   --  Init is convenient in reestablishing a table for new use.
139
140   function Last return Table_Index_Type;
141   pragma Inline (Last);
142   --  Returns the current value of the last used entry in the table, which
143   --  can then be used as a subscript for Table. Note that the only way to
144   --  modify Last is to call the Set_Last procedure. Last must always be
145   --  used to determine the logically last entry.
146
147   procedure Release;
148   --  Storage is allocated in chunks according to the values given in the
149   --  Initial and Increment parameters. A call to Release releases all
150   --  storage that is allocated, but is not logically part of the current
151   --  array value. Current array values are not affected by this call.
152
153   procedure Free;
154   --  Free all allocated memory for the table. A call to Init is required
155   --  before any use of this table after calling Free.
156
157   First : constant Table_Index_Type := Table_Low_Bound;
158   --  Export First as synonym for Low_Bound (parallel with use of Last)
159
160   procedure Set_Last (New_Val : Table_Index_Type);
161   pragma Inline (Set_Last);
162   --  This procedure sets Last to the indicated value. If necessary the
163   --  table is reallocated to accommodate the new value (i.e. on return
164   --  the allocated table has an upper bound of at least Last). If Set_Last
165   --  reduces the size of the table, then logically entries are removed
166   --  from the table. If Set_Last increases the size of the table, then
167   --  new entries are logically added to the table.
168
169   procedure Increment_Last;
170   pragma Inline (Increment_Last);
171   --  Adds 1 to Last (same as Set_Last (Last + 1)
172
173   procedure Decrement_Last;
174   pragma Inline (Decrement_Last);
175   --  Subtracts 1 from Last (same as Set_Last (Last - 1)
176
177   procedure Append (New_Val : Table_Component_Type);
178   pragma Inline (Append);
179   --  Equivalent to:
180   --    x.Increment_Last;
181   --    x.Table (x.Last) := New_Val;
182   --  i.e. the table size is increased by one, and the given new item
183   --  stored in the newly created table element.
184
185   procedure Append_All (New_Vals : Table_Type);
186   --  Appends all components of New_Vals
187
188   procedure Set_Item
189     (Index : Table_Index_Type;
190      Item  : Table_Component_Type);
191   pragma Inline (Set_Item);
192   --  Put Item in the table at position Index. The table is expanded if the
193   --  current table length is less than Index and in that case Last is set to
194   --  Index. Item will replace any value already present in the table at this
195   --  position.
196
197   function Allocate (Num : Integer := 1) return Table_Index_Type;
198   pragma Inline (Allocate);
199   --  Adds Num to Last, and returns the old value of Last + 1. Note that
200   --  this function has the possible side effect of reallocating the table.
201   --  This means that a reference X.Table (X.Allocate) is incorrect, since
202   --  the call to X.Allocate may modify the results of calling X.Table.
203
204   generic
205     with procedure Action
206       (Index : Table_Index_Type;
207        Item  : Table_Component_Type;
208        Quit  : in out Boolean) is <>;
209   procedure For_Each;
210   --  Calls procedure Action for each component of the table, or until
211   --  one of these calls set Quit to True.
212
213   generic
214     with function Lt (Comp1, Comp2 : Table_Component_Type) return Boolean;
215   procedure Sort_Table;
216   --  This procedure sorts the components of the table into ascending
217   --  order making calls to Lt to do required comparisons, and using
218   --  assignments to move components around. The Lt function returns True
219   --  if Comp1 is less than Comp2 (in the sense of the desired sort), and
220   --  False if Comp1 is greater than Comp2. For equal objects it does not
221   --  matter if True or False is returned (it is slightly more efficient
222   --  to return False). The sort is not stable (the order of equal items
223   --  in the table is not preserved).
224
225end GNAT.Table;
226