1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--                ADA.CONTAINERS.BOUNDED_SYNCHRONIZED_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------------------------------------------------------------------------------
29
30package body Ada.Containers.Bounded_Synchronized_Queues is
31
32   pragma Annotate (CodePeer, Skip_Analysis);
33
34   package body Implementation is
35
36      -------------
37      -- Dequeue --
38      -------------
39
40      procedure Dequeue
41        (List    : in out List_Type;
42         Element : out Queue_Interfaces.Element_Type)
43      is
44         EE : Element_Array renames List.Elements;
45
46      begin
47         Element := EE (List.First);
48         List.Length := List.Length - 1;
49
50         if List.Length = 0 then
51            List.First := 0;
52            List.Last := 0;
53
54         elsif List.First <= List.Last then
55            List.First := List.First + 1;
56
57         else
58            List.First := List.First + 1;
59
60            if List.First > List.Capacity then
61               List.First := 1;
62            end if;
63         end if;
64      end Dequeue;
65
66      -------------
67      -- Enqueue --
68      -------------
69
70      procedure Enqueue
71        (List     : in out List_Type;
72         New_Item : Queue_Interfaces.Element_Type)
73      is
74      begin
75         if List.Length >= List.Capacity then
76            raise Capacity_Error with "No capacity for insertion";
77         end if;
78
79         if List.Length = 0 then
80            List.Elements (1) := New_Item;
81            List.First := 1;
82            List.Last := 1;
83
84         elsif List.First <= List.Last then
85            if List.Last < List.Capacity then
86               List.Elements (List.Last + 1) := New_Item;
87               List.Last := List.Last + 1;
88
89            else
90               List.Elements (1) := New_Item;
91               List.Last := 1;
92            end if;
93
94         else
95            List.Elements (List.Last + 1) := New_Item;
96            List.Last := List.Last + 1;
97         end if;
98
99         List.Length := List.Length + 1;
100
101         if List.Length > List.Max_Length then
102            List.Max_Length := List.Length;
103         end if;
104      end Enqueue;
105
106      ------------
107      -- Length --
108      ------------
109
110      function Length (List : List_Type) return Count_Type is
111      begin
112         return List.Length;
113      end Length;
114
115      ----------------
116      -- Max_Length --
117      ----------------
118
119      function Max_Length (List : List_Type) return Count_Type is
120      begin
121         return List.Max_Length;
122      end Max_Length;
123
124   end Implementation;
125
126   protected body Queue is
127
128      -----------------
129      -- Current_Use --
130      -----------------
131
132      function Current_Use return Count_Type is
133      begin
134         return List.Length;
135      end Current_Use;
136
137      -------------
138      -- Dequeue --
139      -------------
140
141      entry Dequeue (Element : out Queue_Interfaces.Element_Type)
142        when List.Length > 0
143      is
144      begin
145         List.Dequeue (Element);
146      end Dequeue;
147
148      -------------
149      -- Enqueue --
150      -------------
151
152      entry Enqueue (New_Item : Queue_Interfaces.Element_Type)
153        when List.Length < Capacity
154      is
155      begin
156         List.Enqueue (New_Item);
157      end Enqueue;
158
159      --------------
160      -- Peak_Use --
161      --------------
162
163      function Peak_Use return Count_Type is
164      begin
165         return List.Max_Length;
166      end Peak_Use;
167
168   end Queue;
169
170end Ada.Containers.Bounded_Synchronized_Queues;
171