1--  Efficient expandable one dimensional array.
2--  Copyright (C) 2015 Tristan Gingold
3--
4--  This program is free software: you can redistribute it and/or modify
5--  it under the terms of the GNU General Public License as published by
6--  the Free Software Foundation, either version 2 of the License, or
7--  (at your option) any later version.
8--
9--  This program is distributed in the hope that it will be useful,
10--  but WITHOUT ANY WARRANTY; without even the implied warranty of
11--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12--  GNU General Public License for more details.
13--
14--  You should have received a copy of the GNU General Public License
15--  along with this program.  If not, see <gnu.org/licenses>.
16
17--  This package mimics GNAT.Table, but:
18--  - the index type can be any discrete type (in particular a modular type)
19--  - the increment is not used
20--  - the interface is simplified.
21with Dyn_Tables;
22
23generic
24   --  This package creates:
25   --    array (Table_Index_Type range Table_Low_Bound .. <>)
26   --      of Table_Component_Type;
27   type Table_Component_Type is private;
28   type Table_Index_Type is (<>);
29
30   --  The lowest bound of the array.  Note that Table_Low_Bound shouldn't be
31   --  Table_Index_Type'First, as otherwise Last may raise constraint error
32   --  when the table is empty.
33   Table_Low_Bound : Table_Index_Type;
34
35   --  Initial number of elements.
36   Table_Initial   : Positive;
37package Tables is
38   package Dyn_Table is new Dyn_Tables (Table_Component_Type,
39                                        Table_Index_Type,
40                                        Table_Low_Bound);
41
42   T : Dyn_Table.Instance;
43
44   subtype Table_Type is Dyn_Table.Table_Type;
45
46   --  Pointer to the table.  Note that the use of a thin pointer to the
47   --  largest array, this implementation bypasses Ada index checks.
48   Table : Dyn_Table.Table_Thin_Ptr renames T.Table;
49
50   --  Initialize the table.  This is done automatically at elaboration.
51   procedure Init;
52
53   --  Logical bounds of the array.
54   First : constant Table_Index_Type := Table_Low_Bound;
55   function Last return Table_Index_Type;
56   pragma Inline (Last);
57
58   --  Deallocate all the memory.  Makes the array unusable until the next
59   --  call to Init.
60   procedure Free;
61
62   --  Increase by 1 the length of the array.  This may allocate memory.
63   procedure Increment_Last;
64   pragma Inline (Increment_Last);
65
66   --  Decrease by 1 the length of the array.
67   procedure Decrement_Last;
68   pragma Inline (Decrement_Last);
69
70   --  Increase or decrease the length of the array by specifying the upper
71   --  bound.
72   procedure Set_Last (Index : Table_Index_Type);
73
74   --  Append VAL to the array.  This always increase the length of the array.
75   procedure Append (Val : Table_Component_Type);
76   pragma Inline (Append);
77
78   --  Increase by NUM the length of the array, and returns the old value
79   --  of Last + 1.
80   function Allocate (Num : Natural := 1) return Table_Index_Type;
81   pragma Inline (Allocate);
82end Tables;
83