2015-09-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / ada / a-cuprqu.adb
blobdfb78687b4b8a9fe7908303d7d09925b33822608
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT LIBRARY COMPONENTS --
4 -- --
5 -- ADA.CONTAINERS.UNBOUNDED_PRIORITY_QUEUES --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2011-2014, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
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 pragma Annotate (CodePeer, Skip_Analysis);
36 package body Implementation is
38 -----------------------
39 -- Local Subprograms --
40 -----------------------
42 procedure Free is
43 new Ada.Unchecked_Deallocation (Node_Type, Node_Access);
45 -------------
46 -- Dequeue --
47 -------------
49 procedure Dequeue
50 (List : in out List_Type;
51 Element : out Queue_Interfaces.Element_Type)
53 X : Node_Access;
55 begin
56 Element := List.First.Element;
58 X := List.First;
59 List.First := List.First.Next;
61 if List.First = null then
62 List.Last := null;
63 end if;
65 List.Length := List.Length - 1;
67 Free (X);
68 end Dequeue;
70 procedure Dequeue
71 (List : in out List_Type;
72 At_Least : Queue_Priority;
73 Element : in out Queue_Interfaces.Element_Type;
74 Success : out Boolean)
76 begin
77 -- This operation dequeues a high priority item if it exists in the
78 -- queue. By "high priority" we mean an item whose priority is equal
79 -- or greater than the value At_Least. The generic formal operation
80 -- Before has the meaning "has higher priority than". To dequeue an
81 -- item (meaning that we return True as our Success value), we need
82 -- as our predicate the equivalent of "has equal or higher priority
83 -- than", but we cannot say that directly, so we require some logical
84 -- gymnastics to make it so.
86 -- If E is the element at the head of the queue, and symbol ">"
87 -- refers to the "is higher priority than" function Before, then we
88 -- derive our predicate as follows:
89 -- original: P(E) >= At_Least
90 -- same as: not (P(E) < At_Least)
91 -- same as: not (At_Least > P(E))
92 -- same as: not Before (At_Least, P(E))
94 -- But that predicate needs to be true in order to successfully
95 -- dequeue an item. If it's false, it means no item is dequeued, and
96 -- we return False as the Success value.
98 if List.Length = 0
99 or else Before (At_Least, Get_Priority (List.First.Element))
100 then
101 Success := False;
102 return;
103 end if;
105 List.Dequeue (Element);
106 Success := True;
107 end Dequeue;
109 -------------
110 -- Enqueue --
111 -------------
113 procedure Enqueue
114 (List : in out List_Type;
115 New_Item : Queue_Interfaces.Element_Type)
117 P : constant Queue_Priority := Get_Priority (New_Item);
119 Node : Node_Access;
120 Prev : Node_Access;
122 begin
123 Node := new Node_Type'(New_Item, null);
125 if List.First = null then
126 List.First := Node;
127 List.Last := List.First;
129 else
130 Prev := List.First;
132 if Before (P, Get_Priority (Prev.Element)) then
133 Node.Next := List.First;
134 List.First := Node;
136 else
137 while Prev.Next /= null loop
138 if Before (P, Get_Priority (Prev.Next.Element)) then
139 Node.Next := Prev.Next;
140 Prev.Next := Node;
142 exit;
143 end if;
145 Prev := Prev.Next;
146 end loop;
148 if Prev.Next = null then
149 List.Last.Next := Node;
150 List.Last := Node;
151 end if;
152 end if;
153 end if;
155 List.Length := List.Length + 1;
157 if List.Length > List.Max_Length then
158 List.Max_Length := List.Length;
159 end if;
160 end Enqueue;
162 --------------
163 -- Finalize --
164 --------------
166 procedure Finalize (List : in out List_Type) is
167 X : Node_Access;
168 begin
169 while List.First /= null loop
170 X := List.First;
171 List.First := List.First.Next;
172 Free (X);
173 end loop;
174 end Finalize;
176 ------------
177 -- Length --
178 ------------
180 function Length (List : List_Type) return Count_Type is
181 begin
182 return List.Length;
183 end Length;
185 ----------------
186 -- Max_Length --
187 ----------------
189 function Max_Length (List : List_Type) return Count_Type is
190 begin
191 return List.Max_Length;
192 end Max_Length;
194 end Implementation;
196 protected body Queue is
198 -----------------
199 -- Current_Use --
200 -----------------
202 function Current_Use return Count_Type is
203 begin
204 return List.Length;
205 end Current_Use;
207 -------------
208 -- Dequeue --
209 -------------
211 entry Dequeue (Element : out Queue_Interfaces.Element_Type)
212 when List.Length > 0
214 begin
215 List.Dequeue (Element);
216 end Dequeue;
218 --------------------------------
219 -- Dequeue_Only_High_Priority --
220 --------------------------------
222 procedure Dequeue_Only_High_Priority
223 (At_Least : Queue_Priority;
224 Element : in out Queue_Interfaces.Element_Type;
225 Success : out Boolean)
227 begin
228 List.Dequeue (At_Least, Element, Success);
229 end Dequeue_Only_High_Priority;
231 -------------
232 -- Enqueue --
233 -------------
235 entry Enqueue (New_Item : Queue_Interfaces.Element_Type) when True is
236 begin
237 List.Enqueue (New_Item);
238 end Enqueue;
240 --------------
241 -- Peak_Use --
242 --------------
244 function Peak_Use return Count_Type is
245 begin
246 return List.Max_Length;
247 end Peak_Use;
249 end Queue;
251 end Ada.Containers.Unbounded_Priority_Queues;