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 System.Address_To_Access_Conversions;
24
25package body BC.Containers.Deques.Bounded is
26
27   procedure Clear (D : in out Deque) is
28   begin
29      Deque_Nodes.Clear (D.Rep);
30   end Clear;
31
32   procedure Append (D : in out Deque;
33                     Elem : Item;
34                     Location : Deque_End := Back) is
35   begin
36      if Location = Back then
37         Deque_Nodes.Append (D.Rep, Elem);
38      else
39         Deque_Nodes.Insert (D.Rep, Elem);
40      end if;
41   end Append;
42
43   procedure Pop (D : in out Deque; Location : Deque_End := Front) is
44   begin
45      if Location = Front then
46         Deque_Nodes.Remove (D.Rep, 1);
47      else
48         Deque_Nodes.Remove (D.Rep,
49                             Deque_Nodes.Length (D.Rep));
50      end if;
51   end Pop;
52
53   procedure Remove (D : in out Deque; From : Positive) is
54   begin
55      Deque_Nodes.Remove (D.Rep, From);
56   end Remove;
57
58   function Available (D : in Deque) return Natural is
59   begin
60      return Deque_Nodes.Available (D.Rep);
61   end Available;
62
63   function Length (D : Deque) return Natural is
64   begin
65      return Deque_Nodes.Length (D.Rep);
66   end Length;
67
68   function Is_Empty (D : Deque) return Boolean is
69   begin
70      return Deque_Nodes.Length (D.Rep) = 0;
71   end Is_Empty;
72
73   function Front (D : Deque) return Item is
74   begin
75      return Deque_Nodes.First (D.Rep);
76   end Front;
77
78   function Back (D : Deque) return Item is
79   begin
80      return Deque_Nodes.Last (D.Rep);
81   end Back;
82
83   function Location (D : Deque; Elem : Item) return Natural is
84   begin
85      return Deque_Nodes.Location (D.Rep, Elem);
86   end Location;
87
88   function "=" (Left, Right : Deque) return Boolean is
89      use Deque_Nodes;
90   begin
91      return Left.Rep = Right.Rep;
92   end "=";
93
94   package Address_Conversions
95   is new System.Address_To_Access_Conversions (Deque);
96
97   function New_Iterator
98     (For_The_Deque : Deque) return Iterator'Class is
99      Result : Deque_Iterator;
100   begin
101      Result.For_The_Container :=
102        Container_Ptr (Address_Conversions.To_Pointer (For_The_Deque'Address));
103      Reset (Result);
104      return Result;
105   end New_Iterator;
106
107   function Item_At (D : Deque; Index : Positive) return Item_Ptr is
108   begin
109      return Deque_Nodes.Item_At (D.Rep, Index);
110   end Item_At;
111
112   function Null_Container return Deque is
113      Empty_Container : Deque;
114      pragma Warnings (Off, Empty_Container);
115   begin
116      return Empty_Container;
117   end Null_Container;
118
119end BC.Containers.Deques.Bounded;
120