OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-dyntab.ads
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 --                                                                          --
10 --            Copyright (C) 2000-2001 Ada Core Technologies, Inc.           --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNAT is maintained by Ada Core Technologies Inc (http://www.gnat.com).   --
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.Table, except
42 --  that this package declares a type that can be used to define dynamic
43 --  instances of the table, while an instantiation of GNAT.Table creates a
44 --  single instance of the table type.
45
46 --  Note that this interface should remain synchronized with those in
47 --  GNAT.Table and the GNAT compiler source unit Table to keep as much
48 --  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.Dynamic_Tables is
59
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:
63
64    --    Table : array (Table_Low_Bound .. <>) of Table_Component_Type;
65
66    --  Table_Component_Type may be any Ada type, except that controlled
67    --  types are not supported. Note however that default initialization
68    --  will NOT occur for array components.
69
70    --  The Table_Initial values controls the allocation of the table when
71    --  it is first allocated, either by default, or by an explicit Init
72    --  call.
73
74    --  The Table_Increment value controls the amount of increase, if the
75    --  table has to be increased in size. The value given is a percentage
76    --  value (e.g. 100 = increase table size by 100%, i.e. double it).
77
78    --  The Last and Set_Last subprograms provide control over the current
79    --  logical allocation. They are quite efficient, so they can be used
80    --  freely (expensive reallocation occurs only at major granularity
81    --  chunks controlled by the allocation parameters).
82
83    --  Note: we do not make the table components aliased, since this would
84    --  restrict the use of table for discriminated types. If it is necessary
85    --  to take the access of a table element, use Unrestricted_Access.
86
87    type Table_Type is
88      array (Table_Index_Type range <>) of Table_Component_Type;
89
90    subtype Big_Table_Type is
91      Table_Type (Table_Low_Bound .. Table_Index_Type'Last);
92    --  We work with pointers to a bogus array type that is constrained
93    --  with the maximum possible range bound. This means that the pointer
94    --  is a thin pointer, which is more efficient. Since subscript checks
95    --  in any case must be on the logical, rather than physical bounds,
96    --  safety is not compromised by this approach.
97
98    type Table_Ptr is access all Big_Table_Type;
99    --  The table is actually represented as a pointer to allow
100    --  reallocation.
101
102    type Table_Private is private;
103    --  table private data that is not exported in Instance.
104
105    type Instance is record
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
111    --  range First .. Last.
112
113       P : Table_Private;
114    end record;
115
116    procedure Init (T : in out Instance);
117    --  This procedure allocates a new table of size Initial (freeing any
118    --  previously allocated larger table). Init must be called before using
119    --  the table. Init is convenient in reestablishing a table for new use.
120
121    function Last (T : in Instance) return Table_Index_Type;
122    pragma Inline (Last);
123    --  Returns the current value of the last used entry in the table,
124    --  which can then be used as a subscript for Table. Note that the
125    --  only way to modify Last is to call the Set_Last procedure. Last
126    --  must always be used to determine the logically last entry.
127
128    procedure Release (T : in out Instance);
129    --  Storage is allocated in chunks according to the values given in the
130    --  Initial and Increment parameters. A call to Release releases all
131    --  storage that is allocated, but is not logically part of the current
132    --  array value. Current array values are not affected by this call.
133
134    procedure Free (T : in out Instance);
135    --  Free all allocated memory for the table. A call to init is required
136    --  before any use of this table after calling Free.
137
138    First : constant Table_Index_Type := Table_Low_Bound;
139    --  Export First as synonym for Low_Bound (parallel with use of Last)
140
141    procedure Set_Last (T : in out Instance; New_Val : Table_Index_Type);
142    pragma Inline (Set_Last);
143    --  This procedure sets Last to the indicated value. If necessary the
144    --  table is reallocated to accommodate the new value (i.e. on return
145    --  the allocated table has an upper bound of at least Last). If
146    --  Set_Last reduces the size of the table, then logically entries are
147    --  removed from the table. If Set_Last increases the size of the
148    --  table, then new entries are logically added to the table.
149
150    procedure Increment_Last (T : in out Instance);
151    pragma Inline (Increment_Last);
152    --  Adds 1 to Last (same as Set_Last (Last + 1).
153
154    procedure Decrement_Last (T : in out Instance);
155    pragma Inline (Decrement_Last);
156    --  Subtracts 1 from Last (same as Set_Last (Last - 1).
157
158    procedure Append (T : in out Instance; New_Val : Table_Component_Type);
159    pragma Inline (Append);
160    --  Equivalent to:
161    --    Increment_Last (T);
162    --    T.Table (T.Last) := New_Val;
163    --  i.e. the table size is increased by one, and the given new item
164    --  stored in the newly created table element.
165
166    procedure Set_Item
167      (T     : in out Instance;
168       Index : Table_Index_Type;
169       Item  : Table_Component_Type);
170    pragma Inline (Set_Item);
171    --  Put Item in the table at position Index. The table is expanded if
172    --  current table length is less than Index and in that case Last is set to
173    --  Index. Item will replace any value already present in the table at this
174    --  position.
175
176    procedure Allocate (T : in out Instance; Num : Integer := 1);
177    pragma Inline (Allocate);
178    --  Adds Num to Last.
179
180 private
181
182    type Table_Private is record
183       Max : Integer;
184       --  Subscript of the maximum entry in the currently allocated table
185
186       Length : Integer := 0;
187       --  Number of entries in currently allocated table. The value of zero
188       --  ensures that we initially allocate the table.
189
190       Last_Val : Integer;
191       --  Current value of Last.
192    end record;
193
194 end GNAT.Dynamic_Tables;