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-2013, 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;
37private with Ada.Finalization;
38
39generic
40   type Index_Type is range <>;
41   type Element_Type is private;
42
43   with function "=" (Left, Right : Element_Type) return Boolean is <>;
44
45package Ada.Containers.Bounded_Vectors is
46   pragma Pure;
47   pragma Remote_Types;
48
49   subtype Extended_Index is Index_Type'Base
50     range Index_Type'First - 1 ..
51           Index_Type'Min (Index_Type'Base'Last - 1, Index_Type'Last) + 1;
52
53   No_Index : constant Extended_Index := Extended_Index'First;
54
55   type Vector (Capacity : Count_Type) is tagged private with
56      Constant_Indexing => Constant_Reference,
57      Variable_Indexing => Reference,
58      Default_Iterator  => Iterate,
59      Iterator_Element  => Element_Type;
60
61   pragma Preelaborable_Initialization (Vector);
62
63   type Cursor is private;
64   pragma Preelaborable_Initialization (Cursor);
65
66   Empty_Vector : constant Vector;
67
68   No_Element : constant Cursor;
69
70   function Has_Element (Position : Cursor) return Boolean;
71
72   package Vector_Iterator_Interfaces is new
73      Ada.Iterator_Interfaces (Cursor, Has_Element);
74
75   overriding function "=" (Left, Right : Vector) return Boolean;
76
77   function To_Vector (Length : Count_Type) return Vector;
78
79   function To_Vector
80     (New_Item : Element_Type;
81      Length   : Count_Type) return Vector;
82
83   function "&" (Left, Right : Vector) return Vector;
84
85   function "&" (Left : Vector; Right : Element_Type) return Vector;
86
87   function "&" (Left : Element_Type; Right : Vector) return Vector;
88
89   function "&" (Left, Right : Element_Type) return Vector;
90
91   function Capacity (Container : Vector) return Count_Type;
92
93   procedure Reserve_Capacity
94     (Container : in out Vector;
95      Capacity  : Count_Type);
96
97   function Length (Container : Vector) return Count_Type;
98
99   procedure Set_Length
100     (Container : in out Vector;
101      Length    : Count_Type);
102
103   function Is_Empty (Container : Vector) return Boolean;
104
105   procedure Clear (Container : in out Vector);
106
107   function To_Cursor
108     (Container : Vector;
109      Index     : Extended_Index) return Cursor;
110
111   function To_Index (Position : Cursor) return Extended_Index;
112
113   function Element
114     (Container : Vector;
115      Index     : Index_Type) return Element_Type;
116
117   function Element (Position : Cursor) return Element_Type;
118
119   procedure Replace_Element
120     (Container : in out Vector;
121      Index     : Index_Type;
122      New_Item  : Element_Type);
123
124   procedure Replace_Element
125     (Container : in out Vector;
126      Position  : Cursor;
127      New_Item  : Element_Type);
128
129   procedure Query_Element
130     (Container : Vector;
131      Index     : Index_Type;
132      Process   : not null access procedure (Element : Element_Type));
133
134   procedure Query_Element
135     (Position : Cursor;
136      Process  : not null access procedure (Element : Element_Type));
137
138   procedure Update_Element
139     (Container : in out Vector;
140      Index     : Index_Type;
141      Process   : not null access procedure (Element : in out Element_Type));
142
143   procedure Update_Element
144     (Container : in out Vector;
145      Position  : Cursor;
146      Process   : not null access procedure (Element : in out Element_Type));
147
148   type Constant_Reference_Type
149      (Element : not null access constant Element_Type) is
150   private
151   with
152      Implicit_Dereference => Element;
153
154   type Reference_Type (Element : not null access Element_Type) is private
155   with
156      Implicit_Dereference => Element;
157
158   function Constant_Reference
159     (Container : aliased Vector;
160      Position  : Cursor) return Constant_Reference_Type;
161
162   function Reference
163     (Container : aliased in out Vector;
164      Position  : Cursor) return Reference_Type;
165
166   function Constant_Reference
167     (Container : aliased Vector;
168      Index     : Index_Type) return Constant_Reference_Type;
169
170   function Reference
171     (Container : aliased in out Vector;
172      Index     : Index_Type) return Reference_Type;
173
174   procedure Assign (Target : in out Vector; Source : Vector);
175
176   function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
177
178   procedure Move (Target : in out Vector; Source : in out Vector);
179
180   procedure Insert
181     (Container : in out Vector;
182      Before    : Extended_Index;
183      New_Item  : Vector);
184
185   procedure Insert
186     (Container : in out Vector;
187      Before    : Cursor;
188      New_Item  : Vector);
189
190   procedure Insert
191     (Container : in out Vector;
192      Before    : Cursor;
193      New_Item  : Vector;
194      Position  : out Cursor);
195
196   procedure Insert
197     (Container : in out Vector;
198      Before    : Extended_Index;
199      New_Item  : Element_Type;
200      Count     : Count_Type := 1);
201
202   procedure Insert
203     (Container : in out Vector;
204      Before    : Cursor;
205      New_Item  : Element_Type;
206      Count     : Count_Type := 1);
207
208   procedure Insert
209     (Container : in out Vector;
210      Before    : Cursor;
211      New_Item  : Element_Type;
212      Position  : out Cursor;
213      Count     : Count_Type := 1);
214
215   procedure Insert
216     (Container : in out Vector;
217      Before    : Extended_Index;
218      Count     : Count_Type := 1);
219
220   procedure Insert
221     (Container : in out Vector;
222      Before    : Cursor;
223      Position  : out Cursor;
224      Count     : Count_Type := 1);
225
226   procedure Prepend
227     (Container : in out Vector;
228      New_Item  : Vector);
229
230   procedure Prepend
231     (Container : in out Vector;
232      New_Item  : Element_Type;
233      Count     : Count_Type := 1);
234
235   procedure Append
236     (Container : in out Vector;
237      New_Item  : Vector);
238
239   procedure Append
240     (Container : in out Vector;
241      New_Item  : Element_Type;
242      Count     : Count_Type := 1);
243
244   procedure Insert_Space
245     (Container : in out Vector;
246      Before    : Extended_Index;
247      Count     : Count_Type := 1);
248
249   procedure Insert_Space
250     (Container : in out Vector;
251      Before    : Cursor;
252      Position  : out Cursor;
253      Count     : Count_Type := 1);
254
255   procedure Delete
256     (Container : in out Vector;
257      Index     : Extended_Index;
258      Count     : Count_Type := 1);
259
260   procedure Delete
261     (Container : in out Vector;
262      Position  : in out Cursor;
263      Count     : Count_Type := 1);
264
265   procedure Delete_First
266     (Container : in out Vector;
267      Count     : Count_Type := 1);
268
269   procedure Delete_Last
270     (Container : in out Vector;
271      Count     : Count_Type := 1);
272
273   procedure Reverse_Elements (Container : in out Vector);
274
275   procedure Swap (Container : in out Vector; I, J : Index_Type);
276
277   procedure Swap (Container : in out Vector; I, J : Cursor);
278
279   function First_Index (Container : Vector) return Index_Type;
280
281   function First (Container : Vector) return Cursor;
282
283   function First_Element (Container : Vector) return Element_Type;
284
285   function Last_Index (Container : Vector) return Extended_Index;
286
287   function Last (Container : Vector) return Cursor;
288
289   function Last_Element (Container : Vector) return Element_Type;
290
291   function Next (Position : Cursor) return Cursor;
292
293   procedure Next (Position : in out Cursor);
294
295   function Previous (Position : Cursor) return Cursor;
296
297   procedure Previous (Position : in out Cursor);
298
299   function Find_Index
300     (Container : Vector;
301      Item      : Element_Type;
302      Index     : Index_Type := Index_Type'First) return Extended_Index;
303
304   function Find
305     (Container : Vector;
306      Item      : Element_Type;
307      Position  : Cursor := No_Element) return Cursor;
308
309   function Reverse_Find_Index
310     (Container : Vector;
311      Item      : Element_Type;
312      Index     : Index_Type := Index_Type'Last) return Extended_Index;
313
314   function Reverse_Find
315     (Container : Vector;
316      Item      : Element_Type;
317      Position  : Cursor := No_Element) return Cursor;
318
319   function Contains
320     (Container : Vector;
321      Item      : Element_Type) return Boolean;
322
323   procedure Iterate
324     (Container : Vector;
325      Process   : not null access procedure (Position : Cursor));
326
327   procedure Reverse_Iterate
328     (Container : Vector;
329      Process   : not null access procedure (Position : Cursor));
330
331   function Iterate
332     (Container : Vector)
333      return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
334
335   function Iterate
336     (Container : Vector;
337      Start     : Cursor)
338      return Vector_Iterator_Interfaces.Reversible_Iterator'class;
339
340   generic
341      with function "<" (Left, Right : Element_Type) return Boolean is <>;
342   package Generic_Sorting is
343
344      function Is_Sorted (Container : Vector) return Boolean;
345
346      procedure Sort (Container : in out Vector);
347
348      procedure Merge (Target : in out Vector; Source : in out Vector);
349
350   end Generic_Sorting;
351
352private
353
354   pragma Inline (First_Index);
355   pragma Inline (Last_Index);
356   pragma Inline (Element);
357   pragma Inline (First_Element);
358   pragma Inline (Last_Element);
359   pragma Inline (Query_Element);
360   pragma Inline (Update_Element);
361   pragma Inline (Replace_Element);
362   pragma Inline (Is_Empty);
363   pragma Inline (Contains);
364   pragma Inline (Next);
365   pragma Inline (Previous);
366
367   use Ada.Streams;
368   use Ada.Finalization;
369
370   type Elements_Array is array (Count_Type range <>) of aliased Element_Type;
371   function "=" (L, R : Elements_Array) return Boolean is abstract;
372
373   type Vector (Capacity : Count_Type) is tagged record
374      Elements : Elements_Array (1 .. Capacity) := (others => <>);
375      Last     : Extended_Index := No_Index;
376      Busy     : Natural := 0;
377      Lock     : Natural := 0;
378   end record;
379
380   procedure Write
381     (Stream    : not null access Root_Stream_Type'Class;
382      Container : Vector);
383
384   for Vector'Write use Write;
385
386   procedure Read
387     (Stream    : not null access Root_Stream_Type'Class;
388      Container : out Vector);
389
390   for Vector'Read use Read;
391
392   type Vector_Access is access all Vector;
393   for Vector_Access'Storage_Size use 0;
394
395   type Cursor is record
396      Container : Vector_Access;
397      Index     : Index_Type := Index_Type'First;
398   end record;
399
400   procedure Write
401     (Stream   : not null access Root_Stream_Type'Class;
402      Position : Cursor);
403
404   for Cursor'Write use Write;
405
406   procedure Read
407     (Stream   : not null access Root_Stream_Type'Class;
408      Position : out Cursor);
409
410   for Cursor'Read use Read;
411
412   type Constant_Reference_Type
413      (Element : not null access constant Element_Type) is null record;
414
415   procedure Read
416     (Stream : not null access Root_Stream_Type'Class;
417      Item   : out Constant_Reference_Type);
418
419   for Constant_Reference_Type'Read use Read;
420
421   procedure Write
422     (Stream : not null access Root_Stream_Type'Class;
423      Item   : Constant_Reference_Type);
424
425   for Constant_Reference_Type'Write use Write;
426
427   type Reference_Type
428      (Element : not null access Element_Type) is null record;
429
430   procedure Read
431     (Stream : not null access Root_Stream_Type'Class;
432      Item   : out Reference_Type);
433
434   for Reference_Type'Read use Read;
435
436   procedure Write
437     (Stream : not null access Root_Stream_Type'Class;
438      Item   : Reference_Type);
439
440   for Reference_Type'Write use Write;
441
442   Empty_Vector : constant Vector := (Capacity => 0, others => <>);
443
444   No_Element : constant Cursor := Cursor'(null, Index_Type'First);
445
446   type Iterator is new Limited_Controlled and
447     Vector_Iterator_Interfaces.Reversible_Iterator with
448   record
449      Container : Vector_Access;
450      Index     : Index_Type'Base;
451   end record;
452
453   overriding procedure Finalize (Object : in out Iterator);
454
455   overriding function First (Object : Iterator) return Cursor;
456   overriding function Last  (Object : Iterator) return Cursor;
457
458   overriding function Next
459     (Object   : Iterator;
460      Position : Cursor) return Cursor;
461
462   overriding function Previous
463     (Object   : Iterator;
464      Position : Cursor) return Cursor;
465
466end Ada.Containers.Bounded_Vectors;
467