1--  Copyright 1994 Grady Booch
2--  Copyright 1994-1997 David Weller
3--  Copyright 1998-2014 Simon Wright <simon@pushface.org>
4
5--  This package is free software; you can redistribute it and/or
6--  modify it under terms of the GNU General Public License as
7--  published by the Free Software Foundation; either version 2, or
8--  (at your option) any later version. This package is distributed in
9--  the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10--  even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11--  PARTICULAR PURPOSE. See the GNU General Public License for more
12--  details. You should have received a copy of the GNU General Public
13--  License distributed with this package; see file COPYING.  If not,
14--  write to the Free Software Foundation, 59 Temple Place - Suite
15--  330, Boston, MA 02111-1307, USA.
16
17--  As a special exception, if other files instantiate generics from
18--  this unit, or you link this unit with other files to produce an
19--  executable, this unit does not by itself cause the resulting
20--  executable to be covered by the GNU General Public License.  This
21--  exception does not however invalidate any other reasons why the
22--  executable file might be covered by the GNU Public License.
23
24with Ada.Finalization;
25with Ada.Streams;
26with System.Storage_Pools;
27
28generic
29   type Item is private;
30   with function "=" (L, R : Item) return Boolean is <>;
31   type Item_Ptr is access all Item;
32   Storage : in out System.Storage_Pools.Root_Storage_Pool'Class;
33   Initial_Size : Positive := 10;
34package BC.Support.Dynamic is
35
36   pragma Preelaborate;
37
38   type Dyn_Node is private;
39   --  An optimally-packed dynamic container whose items are stored on
40   --  the heap
41
42   function "=" (Left, Right : Dyn_Node) return Boolean;
43
44   procedure Clear (Obj : in out Dyn_Node);
45   --  Empty the container of all Items
46
47   procedure Insert (Obj : in out Dyn_Node; Elem : Item);
48   --  Add an item to the front of the container
49
50   procedure Insert (Obj : in out Dyn_Node; Elem : Item; Before : Positive);
51   --  Add an item to the container, before the given index
52
53   procedure Append (Obj : in out Dyn_Node; Elem : Item);
54   --  Add an item to the end of the container
55
56   procedure Append (Obj : in out Dyn_Node; Elem : Item; After : Positive);
57   --  Add an item to the end of the container, after the given index
58
59   procedure Remove (Obj : in out Dyn_Node; From : Positive);
60   --  Remove the item at a given index
61
62   procedure Replace (Obj : in out Dyn_Node; Index : Positive; Elem : Item);
63   --  Replace the Item at Index with the new Elem
64
65   function Length (Obj : Dyn_Node) return Natural;
66   --  Returns the number of items in the container
67
68   function First (Obj : Dyn_Node) return Item;
69   --  Returns the Item at the front of the container
70
71   function Last (Obj : Dyn_Node) return Item;
72   --  Returns the item at the end of the container
73
74   function Item_At (Obj : Dyn_Node; Index : Positive) return Item;
75   function Item_At (Obj : Dyn_Node; Index : Positive) return Item_Ptr;
76   --  Returns the item at the given index
77
78   function Location (Obj : Dyn_Node; Elem : Item; Start : Positive := 1)
79                     return Natural;
80   --  Returns the first index in which the given item is
81   --  found. Returns 0 if unsuccessful.
82
83   procedure Preallocate (Obj : in out Dyn_Node;
84                          New_Length : Natural := Initial_Size);
85   --  Preallocate New_Length number of unused items for the container
86
87   procedure Set_Chunk_Size (Obj : in out Dyn_Node; Size : Natural);
88   --  Set the Chunk_Size for the container
89
90   function Chunk_Size (Obj : in Dyn_Node) return Natural;
91   --  Returns the current Chunk_Size
92
93private
94
95   type Dyn_Arr is array (Positive range <>) of Item;
96
97   type Dyn_Arr_Ref is access all Dyn_Arr;
98   for Dyn_Arr_Ref'Storage_Pool use Storage;
99
100   type Dyn_Node is new Ada.Finalization.Controlled with record
101      Ref        : Dyn_Arr_Ref;
102      Size       : Natural := 0;
103      Chunk_Size : Natural := Initial_Size;
104   end record;
105
106   procedure Initialize (D : in out Dyn_Node);
107   procedure Adjust (D : in out Dyn_Node);
108   procedure Finalize (D : in out Dyn_Node);
109
110   procedure Extend (Obj : in out Dyn_Node);
111
112   procedure Write_Dyn_Node
113     (Stream : access Ada.Streams.Root_Stream_Type'Class;
114      Obj : Dyn_Node);
115
116   procedure Read_Dyn_Node
117     (Stream : access Ada.Streams.Root_Stream_Type'Class;
118      Obj : out Dyn_Node);
119
120   for Dyn_Node'Write use Write_Dyn_Node;
121   for Dyn_Node'Read use Read_Dyn_Node;
122
123end BC.Support.Dynamic;
124