1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.UNBOUNDED_PRIORITY_QUEUES --
9 -- Copyright (C) 2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- This unit was originally developed by Matthew J Heaney. --
28 ------------------------------------------------------------------------------
30 with Ada
.Unchecked_Deallocation
;
32 package body Ada
.Containers
.Unbounded_Priority_Queues
is
34 package body Implementation
is
36 -----------------------
37 -- Local Subprograms --
38 -----------------------
41 new Ada
.Unchecked_Deallocation
(Node_Type
, Node_Access
);
48 (List
: in out List_Type
;
49 Element
: out Queue_Interfaces
.Element_Type
)
54 Element
:= List
.First
.Element
;
57 List
.First
:= List
.First
.Next
;
59 if List
.First
= null then
63 List
.Length
:= List
.Length
- 1;
69 (List
: in out List_Type
;
70 At_Least
: Queue_Priority
;
71 Element
: in out Queue_Interfaces
.Element_Type
;
72 Success
: out Boolean)
75 -- This operation dequeues a high priority item if it exists in the
76 -- queue. By "high priority" we mean an item whose priority is equal
77 -- or greater than the value At_Least. The generic formal operation
78 -- Before has the meaning "has higher priority than". To dequeue an
79 -- item (meaning that we return True as our Success value), we need
80 -- as our predicate the equivalent of "has equal or higher priority
81 -- than", but we cannot say that directly, so we require some logical
82 -- gymnastics to make it so.
84 -- If E is the element at the head of the queue, and symbol ">"
85 -- refers to the "is higher priority than" function Before, then we
86 -- derive our predicate as follows:
87 -- original: P(E) >= At_Least
88 -- same as: not (P(E) < At_Least)
89 -- same as: not (At_Least > P(E))
90 -- same as: not Before (At_Least, P(E))
92 -- But that predicate needs to be true in order to successfully
93 -- dequeue an item. If it's false, it means no item is dequeued, and
94 -- we return False as the Success value.
97 or else Before
(At_Least
, Get_Priority
(List
.First
.Element
))
103 List
.Dequeue
(Element
);
112 (List
: in out List_Type
;
113 New_Item
: Queue_Interfaces
.Element_Type
)
115 P
: constant Queue_Priority
:= Get_Priority
(New_Item
);
121 Node
:= new Node_Type
'(New_Item, null);
123 if List.First = null then
125 List.Last := List.First;
130 if Before (P, Get_Priority (Prev.Element)) then
131 Node.Next := List.First;
135 while Prev.Next /= null loop
136 if Before (P, Get_Priority (Prev.Next.Element)) then
137 Node.Next := Prev.Next;
146 if Prev.Next = null then
147 List.Last.Next := Node;
153 List.Length := List.Length + 1;
155 if List.Length > List.Max_Length then
156 List.Max_Length := List.Length;
164 procedure Finalize (List : in out List_Type) is
167 while List.First /= null loop
169 List.First := List.First.Next;
178 function Length (List : List_Type) return Count_Type is
187 function Max_Length (List : List_Type) return Count_Type is
189 return List.Max_Length;
194 protected body Queue is
200 function Current_Use return Count_Type is
209 entry Dequeue (Element : out Queue_Interfaces.Element_Type)
213 List.Dequeue (Element);
216 --------------------------------
217 -- Dequeue_Only_High_Priority --
218 --------------------------------
220 procedure Dequeue_Only_High_Priority
221 (At_Least : Queue_Priority;
222 Element : in out Queue_Interfaces.Element_Type;
223 Success : out Boolean)
226 List.Dequeue (At_Least, Element, Success);
227 end Dequeue_Only_High_Priority;
233 entry Enqueue (New_Item : Queue_Interfaces.Element_Type) when True is
235 List.Enqueue (New_Item);
242 function Peak_Use return Count_Type is
244 return List.Max_Length;
249 end Ada.Containers.Unbounded_Priority_Queues;