1--  Copyright 1994 Grady Booch
2--  Copyright 1998-2014 Simon Wright <simon@pushface.org>
3
4--  This package is free software; you can redistribute it and/or
5--  modify it under terms of the GNU General Public License as
6--  published by the Free Software Foundation; either version 2, or
7--  (at your option) any later version. This package is distributed in
8--  the hope that it will be useful, but WITHOUT ANY WARRANTY; without
9--  even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10--  PARTICULAR PURPOSE. See the GNU General Public License for more
11--  details. You should have received a copy of the GNU General Public
12--  License distributed with this package; see file COPYING.  If not,
13--  write to the Free Software Foundation, 59 Temple Place - Suite
14--  330, Boston, MA 02111-1307, USA.
15
16--  As a special exception, if other files instantiate generics from
17--  this unit, or you link this unit with other files to produce an
18--  executable, this unit does not by itself cause the resulting
19--  executable to be covered by the GNU General Public License.  This
20--  exception does not however invalidate any other reasons why the
21--  executable file might be covered by the GNU Public License.
22
23with BC.Support.Dynamic;
24with System.Storage_Pools;
25
26generic
27   Storage : in out System.Storage_Pools.Root_Storage_Pool'Class;
28   Initial_Size : Positive := 10;
29package BC.Containers.Collections.Ordered.Dynamic is
30
31   pragma Preelaborate;
32
33   type Collection is new Abstract_Ordered_Collection with private;
34
35   function Null_Container return Collection;
36
37   function "=" (Left, Right : in Collection) return Boolean;
38
39   procedure Clear (C : in out Collection);
40   --  Empty the collection of all items.
41
42   procedure Insert (C : in out Collection; Elem : Item);
43   --  Add the item to the collection, inserting the new item at the
44   --  appropriate position; if an equivalent item is found, the new
45   --  item is inserted before it.
46
47   procedure Insert (C : in out Collection;
48                     Elem : Item;
49                     Before : Positive);
50   --  If the indicated item is equivalent to the new item, the new
51   --  item is inserted before the indicated item; otherwise, the
52   --  behaviour is as above.
53
54   procedure Append (C : in out Collection; Elem : Item);
55   --  Add the item to the collection, inserting the new item at the
56   --  appropriate position; if any equivalent items are found, the
57   --  new item is inserted after all of them.
58
59   procedure Append (C : in out Collection;
60                     Elem : Item;
61                     After : Positive);
62   --  If the indicated item is equivalent to the new item, the new
63   --  item is inserted after the indicated item; otherwise, the
64   --  behaviour is as above.
65
66   procedure Remove (C : in out Collection; At_Index : Positive);
67   --  Remove the item at the given index in the collection.
68
69   procedure Replace (C : in out Collection;
70                      At_Index : Positive;
71                      Elem : Item);
72   --  If the indicated item is equivalent to the new item, it is
73   --  replaced directly.
74   --
75   --  If the new item is "<" the indicated item, the indicated item
76   --  is removed and the new item is Appended, as above. If the
77   --  indicated item is "<" the new item, the indicated item is
78   --  removed and the new item is Inserted, as above. The effect is
79   --  that the new item moves toward the appropriate end of the
80   --  Collection but not beyond any equivalent items.
81
82   function Length (C : Collection) return Natural;
83   --  Return the number of items in the collection.
84
85   function Is_Empty (C : Collection) return Boolean;
86   --  Return True if and only if there are no items in the
87   --  collection.
88
89   function First (C : Collection) return Item;
90   --  Return a copy of the item at the front of the collection.
91
92   function Last (C : Collection) return Item;
93   --  Return a copy of the item at the end of the collection.
94
95   function Item_At (C : Collection; At_Index : Positive) return Item;
96   --  Return a copy of the item at the indicated position in the
97   --  collection.
98
99   function Location (C : Collection; Elem : Item) return Natural;
100   --  Return the first index at which the item is found (0 if the
101   --  item desn't exist in the collecton).
102
103   procedure Preallocate (C : in out Collection; Size : Natural);
104   --  Allocates 'Size' additional storage elements for the Collection
105
106   procedure Set_Chunk_Size
107     (C : in out Collection; Size : Natural);
108   --  Establishes the Size the Collection will grow if the Collection
109   --  exhausts its current size.
110
111   function Chunk_Size (C : Collection) return Natural;
112   --  Returns the Chunk_Size
113
114   function New_Iterator
115     (For_The_Collection : Collection) return Iterator'Class;
116   --  Return a reset Iterator bound to the specific Collection.
117
118private
119
120   function Item_At
121     (C : Collection; Index : Positive) return Item_Ptr;
122
123   package Collection_Nodes
124   is new BC.Support.Dynamic (Item => Item,
125                              Item_Ptr => Item_Ptr,
126                              Storage => Storage,
127                              Initial_Size => Initial_Size);
128
129   type Collection is new Abstract_Ordered_Collection with record
130      Rep : Collection_Nodes.Dyn_Node;
131   end record;
132
133end BC.Containers.Collections.Ordered.Dynamic;
134