1--  Copyright 1994 Grady Booch
2--  Copyright 2003-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.Queues.Ordered.Unmanaged is
26
27   procedure Clear (Q : in out Queue) is
28   begin
29      Queue_Nodes.Clear (Q.Rep);
30   end Clear;
31
32   procedure Append (Q : in out Queue; Elem : Item) is
33   begin
34      for Index in 1 .. Queue_Nodes.Length (Q.Rep)
35      loop
36         if Elem < Queue_Nodes.Item_At (Q.Rep, Index) then
37            Queue_Nodes.Insert (Q.Rep, Elem, Index);
38            return;
39         end if;
40      end loop;
41      Queue_Nodes.Append (Q.Rep, Elem);
42   end Append;
43
44   procedure Pop (Q : in out Queue) is
45   begin
46      Queue_Nodes.Remove (Q.Rep, 1);
47   end Pop;
48
49   procedure Remove (Q : in out Queue; From : Positive) is
50   begin
51      Queue_Nodes.Remove (Q.Rep, From);
52   end Remove;
53
54   function Length (Q : Queue) return Natural is
55   begin
56      return Queue_Nodes.Length (Q.Rep);
57   end Length;
58
59   function Is_Empty (Q : Queue) return Boolean is
60   begin
61      return Queue_Nodes.Length (Q.Rep) = 0;
62   end Is_Empty;
63
64   function Front (Q : Queue) return Item is
65   begin
66      return Queue_Nodes.First (Q.Rep);
67   end Front;
68
69   function Location (Q : Queue; Elem : Item) return Natural is
70   begin
71      return Queue_Nodes.Location (Q.Rep, Elem);
72   end Location;
73
74   function "=" (Left, Right : Queue) return Boolean is
75      use Queue_Nodes;
76   begin
77      return Left.Rep = Right.Rep;
78   end "=";
79
80   package Address_Conversions
81   is new System.Address_To_Access_Conversions (Queue);
82
83   function New_Iterator (For_The_Queue : Queue) return Iterator'Class is
84      Result : Queue_Iterator;
85   begin
86      Result.For_The_Container :=
87        Container_Ptr (Address_Conversions.To_Pointer (For_The_Queue'Address));
88      Reset (Result);
89      return Result;
90   end New_Iterator;
91
92   function Item_At (Q : Queue; Index : Positive) return Item_Ptr is
93   begin
94      return Queue_Nodes.Item_At (Q.Rep, Index);
95   end Item_At;
96
97   function Null_Container return Queue is
98      Empty_Container : Queue;
99      pragma Warnings (Off, Empty_Container);
100   begin
101      return Empty_Container;
102   end Null_Container;
103
104end BC.Containers.Queues.Ordered.Unmanaged;
105