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 BC.Support.Dynamic;
25with System.Storage_Pools;
26
27generic
28   Storage : in out System.Storage_Pools.Root_Storage_Pool'Class;
29   Initial_Size : Positive := 10;
30package BC.Containers.Stacks.Dynamic is
31
32   pragma Preelaborate;
33
34   type Stack is new Abstract_Stack with private;
35   --  A dynamic Stack exhibits similar performance to a
36   --  Bounded_Stack, except its size is limited only to available
37   --  memory.  It dynamically grows in a linear fashion (based on
38   --  Chunk_Size).  There is currently no support for linear
39   --  collapsing of the Stack.
40
41   function Null_Container return Stack;
42
43   function "=" (Left, Right : Stack) return Boolean;
44   --  Return True if and only if both stacks have the same depth and
45   --  the same items in the same order; return False otherwise.
46
47   procedure Clear (S : in out Stack);
48   --  Empty the Stack of all items.
49
50   procedure Push (S : in out Stack; Elem : Item);
51   --  Add a copy of the item to the top of the Stack.
52
53   procedure Pop (S : in out Stack);
54   --  Remove the item from the top of the Stack.
55
56   function Depth (S : in Stack) return Natural;
57   --  Returns the number of items in the Stack
58
59   function Is_Empty (S : in Stack) return Boolean;
60   --  Returns True if and only if no items are in the stack.
61
62   function Top (S : in Stack) return Item;
63   --  Return a copy of the item at the top of the Stack.
64
65   procedure Preallocate (S : in out Stack; Size : Natural);
66   --  Allocates 'Size' additional storage elements for the Stack.
67
68   procedure Set_Chunk_Size (S : in out Stack; Size : Natural);
69   --  Establishes the Size the Stack will grow if the Stack exhausts
70   --  its current size.
71
72   function Chunk_Size (S : Stack) return Natural;
73   --  Returns the Chunk_Size
74
75   function New_Iterator (For_The_Stack : Stack) return Iterator'Class;
76   --  Return a reset Iterator bound to the specific Stack.
77
78private
79
80   function Item_At (S : Stack; Index : Positive) return Item_Ptr;
81
82   procedure Add (S : in out Stack; Elem : Item);
83   procedure Remove (S : in out Stack; From : Positive);
84
85   package Stack_Nodes
86   is new BC.Support.Dynamic (Item => Item,
87                              Item_Ptr => Item_Ptr,
88                              Storage => Storage,
89                              Initial_Size => Initial_Size);
90
91   type Stack is new Abstract_Stack with record
92      Rep : Stack_Nodes.Dyn_Node;
93   end record;
94
95end BC.Containers.Stacks.Dynamic;
96