1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT LIBRARY COMPONENTS                          --
4--                                                                          --
5--       A D A . C O N T A I N E R S . B O U N D E D _ V E C T O R S        --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2004-2012, Free Software Foundation, Inc.         --
10--                                                                          --
11-- This specification is derived from the Ada Reference Manual for use with --
12-- GNAT. The copyright notice above, and the license provisions that follow --
13-- apply solely to the  contents of the part following the private keyword. --
14--                                                                          --
15-- GNAT is free software;  you can  redistribute it  and/or modify it under --
16-- terms of the  GNU General Public License as published  by the Free Soft- --
17-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
18-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
21--                                                                          --
22-- As a special exception under Section 7 of GPL version 3, you are granted --
23-- additional permissions described in the GCC Runtime Library Exception,   --
24-- version 3.1, as published by the Free Software Foundation.               --
25--                                                                          --
26-- You should have received a copy of the GNU General Public License and    --
27-- a copy of the GCC Runtime Library Exception along with this program;     --
28-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
29-- <http://www.gnu.org/licenses/>.                                          --
30--                                                                          --
31-- This unit was originally developed by Matthew J Heaney.                  --
32------------------------------------------------------------------------------
33
34with Ada.Iterator_Interfaces;
35
36private with Ada.Streams;
37
38generic
39   type Index_Type is range <>;
40   type Element_Type is private;
41
42   with function "=" (Left, Right : Element_Type) return Boolean is <>;
43
44package Ada.Containers.Bounded_Vectors is
45   pragma Pure;
46   pragma Remote_Types;
47
48   subtype Extended_Index is Index_Type'Base
49     range Index_Type'First - 1 ..
50           Index_Type'Min (Index_Type'Base'Last - 1, Index_Type'Last) + 1;
51
52   No_Index : constant Extended_Index := Extended_Index'First;
53
54   type Vector (Capacity : Count_Type) is tagged private with
55      Constant_Indexing => Constant_Reference,
56      Variable_Indexing => Reference,
57      Default_Iterator  => Iterate,
58      Iterator_Element  => Element_Type;
59
60   pragma Preelaborable_Initialization (Vector);
61
62   type Cursor is private;
63   pragma Preelaborable_Initialization (Cursor);
64
65   Empty_Vector : constant Vector;
66
67   No_Element : constant Cursor;
68
69   function Has_Element (Position : Cursor) return Boolean;
70
71   package Vector_Iterator_Interfaces is new
72      Ada.Iterator_Interfaces (Cursor, Has_Element);
73
74   overriding function "=" (Left, Right : Vector) return Boolean;
75
76   function To_Vector (Length : Count_Type) return Vector;
77
78   function To_Vector
79     (New_Item : Element_Type;
80      Length   : Count_Type) return Vector;
81
82   function "&" (Left, Right : Vector) return Vector;
83
84   function "&" (Left : Vector; Right : Element_Type) return Vector;
85
86   function "&" (Left : Element_Type; Right : Vector) return Vector;
87
88   function "&" (Left, Right : Element_Type) return Vector;
89
90   function Capacity (Container : Vector) return Count_Type;
91
92   procedure Reserve_Capacity
93     (Container : in out Vector;
94      Capacity  : Count_Type);
95
96   function Length (Container : Vector) return Count_Type;
97
98   procedure Set_Length
99     (Container : in out Vector;
100      Length    : Count_Type);
101
102   function Is_Empty (Container : Vector) return Boolean;
103
104   procedure Clear (Container : in out Vector);
105
106   function To_Cursor
107     (Container : Vector;
108      Index     : Extended_Index) return Cursor;
109
110   function To_Index (Position : Cursor) return Extended_Index;
111
112   function Element
113     (Container : Vector;
114      Index     : Index_Type) return Element_Type;
115
116   function Element (Position : Cursor) return Element_Type;
117
118   procedure Replace_Element
119     (Container : in out Vector;
120      Index     : Index_Type;
121      New_Item  : Element_Type);
122
123   procedure Replace_Element
124     (Container : in out Vector;
125      Position  : Cursor;
126      New_Item  : Element_Type);
127
128   procedure Query_Element
129     (Container : Vector;
130      Index     : Index_Type;
131      Process   : not null access procedure (Element : Element_Type));
132
133   procedure Query_Element
134     (Position : Cursor;
135      Process  : not null access procedure (Element : Element_Type));
136
137   procedure Update_Element
138     (Container : in out Vector;
139      Index     : Index_Type;
140      Process   : not null access procedure (Element : in out Element_Type));
141
142   procedure Update_Element
143     (Container : in out Vector;
144      Position  : Cursor;
145      Process   : not null access procedure (Element : in out Element_Type));
146
147   type Constant_Reference_Type
148      (Element : not null access constant Element_Type) is
149   private
150   with
151      Implicit_Dereference => Element;
152
153   type Reference_Type (Element : not null access Element_Type) is private
154   with
155      Implicit_Dereference => Element;
156
157   function Constant_Reference
158     (Container : aliased Vector;
159      Position  : Cursor) return Constant_Reference_Type;
160
161   function Reference
162     (Container : aliased in out Vector;
163      Position  : Cursor) return Reference_Type;
164
165   function Constant_Reference
166     (Container : aliased Vector;
167      Index     : Index_Type) return Constant_Reference_Type;
168
169   function Reference
170     (Container : aliased in out Vector;
171      Index     : Index_Type) return Reference_Type;
172
173   procedure Assign (Target : in out Vector; Source : Vector);
174
175   function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
176
177   procedure Move (Target : in out Vector; Source : in out Vector);
178
179   procedure Insert
180     (Container : in out Vector;
181      Before    : Extended_Index;
182      New_Item  : Vector);
183
184   procedure Insert
185     (Container : in out Vector;
186      Before    : Cursor;
187      New_Item  : Vector);
188
189   procedure Insert
190     (Container : in out Vector;
191      Before    : Cursor;
192      New_Item  : Vector;
193      Position  : out Cursor);
194
195   procedure Insert
196     (Container : in out Vector;
197      Before    : Extended_Index;
198      New_Item  : Element_Type;
199      Count     : Count_Type := 1);
200
201   procedure Insert
202     (Container : in out Vector;
203      Before    : Cursor;
204      New_Item  : Element_Type;
205      Count     : Count_Type := 1);
206
207   procedure Insert
208     (Container : in out Vector;
209      Before    : Cursor;
210      New_Item  : Element_Type;
211      Position  : out Cursor;
212      Count     : Count_Type := 1);
213
214   procedure Insert
215     (Container : in out Vector;
216      Before    : Extended_Index;
217      Count     : Count_Type := 1);
218
219   procedure Insert
220     (Container : in out Vector;
221      Before    : Cursor;
222      Position  : out Cursor;
223      Count     : Count_Type := 1);
224
225   procedure Prepend
226     (Container : in out Vector;
227      New_Item  : Vector);
228
229   procedure Prepend
230     (Container : in out Vector;
231      New_Item  : Element_Type;
232      Count     : Count_Type := 1);
233
234   procedure Append
235     (Container : in out Vector;
236      New_Item  : Vector);
237
238   procedure Append
239     (Container : in out Vector;
240      New_Item  : Element_Type;
241      Count     : Count_Type := 1);
242
243   procedure Insert_Space
244     (Container : in out Vector;
245      Before    : Extended_Index;
246      Count     : Count_Type := 1);
247
248   procedure Insert_Space
249     (Container : in out Vector;
250      Before    : Cursor;
251      Position  : out Cursor;
252      Count     : Count_Type := 1);
253
254   procedure Delete
255     (Container : in out Vector;
256      Index     : Extended_Index;
257      Count     : Count_Type := 1);
258
259   procedure Delete
260     (Container : in out Vector;
261      Position  : in out Cursor;
262      Count     : Count_Type := 1);
263
264   procedure Delete_First
265     (Container : in out Vector;
266      Count     : Count_Type := 1);
267
268   procedure Delete_Last
269     (Container : in out Vector;
270      Count     : Count_Type := 1);
271
272   procedure Reverse_Elements (Container : in out Vector);
273
274   procedure Swap (Container : in out Vector; I, J : Index_Type);
275
276   procedure Swap (Container : in out Vector; I, J : Cursor);
277
278   function First_Index (Container : Vector) return Index_Type;
279
280   function First (Container : Vector) return Cursor;
281
282   function First_Element (Container : Vector) return Element_Type;
283
284   function Last_Index (Container : Vector) return Extended_Index;
285
286   function Last (Container : Vector) return Cursor;
287
288   function Last_Element (Container : Vector) return Element_Type;
289
290   function Next (Position : Cursor) return Cursor;
291
292   procedure Next (Position : in out Cursor);
293
294   function Previous (Position : Cursor) return Cursor;
295
296   procedure Previous (Position : in out Cursor);
297
298   function Find_Index
299     (Container : Vector;
300      Item      : Element_Type;
301      Index     : Index_Type := Index_Type'First) return Extended_Index;
302
303   function Find
304     (Container : Vector;
305      Item      : Element_Type;
306      Position  : Cursor := No_Element) return Cursor;
307
308   function Reverse_Find_Index
309     (Container : Vector;
310      Item      : Element_Type;
311      Index     : Index_Type := Index_Type'Last) return Extended_Index;
312
313   function Reverse_Find
314     (Container : Vector;
315      Item      : Element_Type;
316      Position  : Cursor := No_Element) return Cursor;
317
318   function Contains
319     (Container : Vector;
320      Item      : Element_Type) return Boolean;
321
322   procedure Iterate
323     (Container : Vector;
324      Process   : not null access procedure (Position : Cursor));
325
326   procedure Reverse_Iterate
327     (Container : Vector;
328      Process   : not null access procedure (Position : Cursor));
329
330   function Iterate
331     (Container : Vector)
332      return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
333
334   function Iterate
335     (Container : Vector;
336      Start     : Cursor)
337      return Vector_Iterator_Interfaces.Reversible_Iterator'class;
338
339   generic
340      with function "<" (Left, Right : Element_Type) return Boolean is <>;
341   package Generic_Sorting is
342
343      function Is_Sorted (Container : Vector) return Boolean;
344
345      procedure Sort (Container : in out Vector);
346
347      procedure Merge (Target : in out Vector; Source : in out Vector);
348
349   end Generic_Sorting;
350
351private
352
353   pragma Inline (First_Index);
354   pragma Inline (Last_Index);
355   pragma Inline (Element);
356   pragma Inline (First_Element);
357   pragma Inline (Last_Element);
358   pragma Inline (Query_Element);
359   pragma Inline (Update_Element);
360   pragma Inline (Replace_Element);
361   pragma Inline (Is_Empty);
362   pragma Inline (Contains);
363   pragma Inline (Next);
364   pragma Inline (Previous);
365
366   use Ada.Streams;
367
368   type Elements_Array is array (Count_Type range <>) of aliased Element_Type;
369   function "=" (L, R : Elements_Array) return Boolean is abstract;
370
371   type Vector (Capacity : Count_Type) is tagged record
372      Elements : Elements_Array (1 .. Capacity) := (others => <>);
373      Last     : Extended_Index := No_Index;
374      Busy     : Natural := 0;
375      Lock     : Natural := 0;
376   end record;
377
378   procedure Write
379     (Stream    : not null access Root_Stream_Type'Class;
380      Container : Vector);
381
382   for Vector'Write use Write;
383
384   procedure Read
385     (Stream    : not null access Root_Stream_Type'Class;
386      Container : out Vector);
387
388   for Vector'Read use Read;
389
390   type Vector_Access is access all Vector;
391   for Vector_Access'Storage_Size use 0;
392
393   type Cursor is record
394      Container : Vector_Access;
395      Index     : Index_Type := Index_Type'First;
396   end record;
397
398   procedure Write
399     (Stream   : not null access Root_Stream_Type'Class;
400      Position : Cursor);
401
402   for Cursor'Write use Write;
403
404   procedure Read
405     (Stream   : not null access Root_Stream_Type'Class;
406      Position : out Cursor);
407
408   for Cursor'Read use Read;
409
410   type Constant_Reference_Type
411      (Element : not null access constant Element_Type) is null record;
412
413   procedure Read
414     (Stream : not null access Root_Stream_Type'Class;
415      Item   : out Constant_Reference_Type);
416
417   for Constant_Reference_Type'Read use Read;
418
419   procedure Write
420     (Stream : not null access Root_Stream_Type'Class;
421      Item   : Constant_Reference_Type);
422
423   for Constant_Reference_Type'Write use Write;
424
425   type Reference_Type
426      (Element : not null access Element_Type) is null record;
427
428   procedure Read
429     (Stream : not null access Root_Stream_Type'Class;
430      Item   : out Reference_Type);
431
432   for Reference_Type'Read use Read;
433
434   procedure Write
435     (Stream : not null access Root_Stream_Type'Class;
436      Item   : Reference_Type);
437
438   for Reference_Type'Write use Write;
439
440   Empty_Vector : constant Vector := (Capacity => 0, others => <>);
441
442   No_Element : constant Cursor := Cursor'(null, Index_Type'First);
443
444end Ada.Containers.Bounded_Vectors;
445