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