1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . V E C T O R S --
9 -- Copyright (C) 2004-2011, Free Software Foundation, Inc. --
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. --
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. --
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. --
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/>. --
31 -- This unit was originally developed by Matthew J Heaney. --
32 ------------------------------------------------------------------------------
34 private with Ada.Finalization;
36 with Ada.Iterator_Interfaces;
38 type Index_Type is range <>;
39 type Element_Type is private;
41 with function "=" (Left, Right : Element_Type) return Boolean is <>;
43 package Ada.Containers.Vectors is
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;
52 No_Index : constant Extended_Index := Extended_Index'First;
54 type Vector is tagged private
56 Constant_Indexing => Constant_Reference,
57 Variable_Indexing => Reference,
58 Default_Iterator => Iterate,
59 Iterator_Element => Element_Type;
60 pragma Preelaborable_Initialization (Vector);
62 type Cursor is private;
63 pragma Preelaborable_Initialization (Cursor);
64 No_Element : constant Cursor;
66 function Has_Element (Position : Cursor) return Boolean;
69 (Stream : not null access Root_Stream_Type'Class;
70 Position : out Cursor);
72 for Cursor'Read use Read;
75 (Stream : not null access Root_Stream_Type'Class;
77 for Cursor'Write use Write;
79 package Vector_Iterator_Interfaces is new
80 Ada.Iterator_Interfaces (Cursor, Has_Element);
82 Empty_Vector : constant Vector;
84 overriding function "=" (Left, Right : Vector) return Boolean;
86 function To_Vector (Length : Count_Type) return Vector;
89 (New_Item : Element_Type;
90 Length : Count_Type) return Vector;
92 function "&" (Left, Right : Vector) return Vector;
94 function "&" (Left : Vector; Right : Element_Type) return Vector;
96 function "&" (Left : Element_Type; Right : Vector) return Vector;
98 function "&" (Left, Right : Element_Type) return Vector;
100 function Capacity (Container : Vector) return Count_Type;
102 procedure Reserve_Capacity
103 (Container : in out Vector;
104 Capacity : Count_Type);
106 function Length (Container : Vector) return Count_Type;
109 (Container : in out Vector;
110 Length : Count_Type);
112 function Is_Empty (Container : Vector) return Boolean;
114 procedure Clear (Container : in out Vector);
118 Index : Extended_Index) return Cursor;
120 function To_Index (Position : Cursor) return Extended_Index;
124 Index : Index_Type) return Element_Type;
126 function Element (Position : Cursor) return Element_Type;
128 procedure Replace_Element
129 (Container : in out Vector;
131 New_Item : Element_Type);
133 procedure Replace_Element
134 (Container : in out Vector;
136 New_Item : Element_Type);
138 procedure Query_Element
141 Process : not null access procedure (Element : Element_Type));
143 procedure Query_Element
145 Process : not null access procedure (Element : Element_Type));
147 procedure Update_Element
148 (Container : in out Vector;
150 Process : not null access procedure (Element : in out Element_Type));
152 procedure Update_Element
153 (Container : in out Vector;
155 Process : not null access procedure (Element : in out Element_Type));
157 type Constant_Reference_Type
158 (Element : not null access constant Element_Type) is
161 Implicit_Dereference => Element;
164 (Stream : not null access Root_Stream_Type'Class;
165 Item : Constant_Reference_Type);
167 for Constant_Reference_Type'Write use Write;
170 (Stream : not null access Root_Stream_Type'Class;
171 Item : out Constant_Reference_Type);
173 for Constant_Reference_Type'Read use Read;
175 type Reference_Type (Element : not null access Element_Type) is private
177 Implicit_Dereference => Element;
180 (Stream : not null access Root_Stream_Type'Class;
181 Item : Reference_Type);
183 for Reference_Type'Write use Write;
186 (Stream : not null access Root_Stream_Type'Class;
187 Item : out Reference_Type);
189 for Reference_Type'Read use Read;
191 function Constant_Reference
192 (Container : Vector; Position : Cursor) -- SHOULD BE ALIASED
193 return Constant_Reference_Type;
195 function Constant_Reference
196 (Container : Vector; Position : Index_Type)
197 return Constant_Reference_Type;
199 function Reference (Container : Vector; Position : Cursor)
200 return Reference_Type;
202 function Reference (Container : Vector; Position : Index_Type)
203 return Reference_Type;
205 procedure Assign (Target : in out Vector; Source : Vector);
207 function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
209 procedure Move (Target : in out Vector; Source : in out Vector);
212 (Container : in out Vector;
213 Before : Extended_Index;
217 (Container : in out Vector;
222 (Container : in out Vector;
225 Position : out Cursor);
228 (Container : in out Vector;
229 Before : Extended_Index;
230 New_Item : Element_Type;
231 Count : Count_Type := 1);
234 (Container : in out Vector;
236 New_Item : Element_Type;
237 Count : Count_Type := 1);
240 (Container : in out Vector;
242 New_Item : Element_Type;
243 Position : out Cursor;
244 Count : Count_Type := 1);
247 (Container : in out Vector;
248 Before : Extended_Index;
249 Count : Count_Type := 1);
252 (Container : in out Vector;
254 Position : out Cursor;
255 Count : Count_Type := 1);
258 (Container : in out Vector;
262 (Container : in out Vector;
263 New_Item : Element_Type;
264 Count : Count_Type := 1);
267 (Container : in out Vector;
271 (Container : in out Vector;
272 New_Item : Element_Type;
273 Count : Count_Type := 1);
275 procedure Insert_Space
276 (Container : in out Vector;
277 Before : Extended_Index;
278 Count : Count_Type := 1);
280 procedure Insert_Space
281 (Container : in out Vector;
283 Position : out Cursor;
284 Count : Count_Type := 1);
287 (Container : in out Vector;
288 Index : Extended_Index;
289 Count : Count_Type := 1);
292 (Container : in out Vector;
293 Position : in out Cursor;
294 Count : Count_Type := 1);
296 procedure Delete_First
297 (Container : in out Vector;
298 Count : Count_Type := 1);
300 procedure Delete_Last
301 (Container : in out Vector;
302 Count : Count_Type := 1);
304 procedure Reverse_Elements (Container : in out Vector);
306 procedure Swap (Container : in out Vector; I, J : Index_Type);
308 procedure Swap (Container : in out Vector; I, J : Cursor);
310 function First_Index (Container : Vector) return Index_Type;
312 function First (Container : Vector) return Cursor;
314 function First_Element (Container : Vector) return Element_Type;
316 function Last_Index (Container : Vector) return Extended_Index;
318 function Last (Container : Vector) return Cursor;
320 function Last_Element (Container : Vector) return Element_Type;
322 function Next (Position : Cursor) return Cursor;
324 procedure Next (Position : in out Cursor);
326 function Previous (Position : Cursor) return Cursor;
328 procedure Previous (Position : in out Cursor);
333 Index : Index_Type := Index_Type'First) return Extended_Index;
338 Position : Cursor := No_Element) return Cursor;
340 function Reverse_Find_Index
343 Index : Index_Type := Index_Type'Last) return Extended_Index;
345 function Reverse_Find
348 Position : Cursor := No_Element) return Cursor;
352 Item : Element_Type) return Boolean;
356 Process : not null access procedure (Position : Cursor));
358 procedure Reverse_Iterate
360 Process : not null access procedure (Position : Cursor));
362 function Iterate (Container : Vector)
363 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
365 function Iterate (Container : Vector; Start : Cursor)
366 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
369 with function "<" (Left, Right : Element_Type) return Boolean is <>;
370 package Generic_Sorting is
372 function Is_Sorted (Container : Vector) return Boolean;
374 procedure Sort (Container : in out Vector);
376 procedure Merge (Target : in out Vector; Source : in out Vector);
382 pragma Inline (First_Index);
383 pragma Inline (Last_Index);
384 pragma Inline (Element);
385 pragma Inline (First_Element);
386 pragma Inline (Last_Element);
387 pragma Inline (Query_Element);
388 pragma Inline (Update_Element);
389 pragma Inline (Replace_Element);
390 pragma Inline (Is_Empty);
391 pragma Inline (Contains);
392 pragma Inline (Next);
393 pragma Inline (Previous);
395 type Elements_Array is array (Index_Type range <>) of aliased Element_Type;
396 function "=" (L, R : Elements_Array) return Boolean is abstract;
398 type Elements_Type (Last : Index_Type) is limited record
399 EA : Elements_Array (Index_Type'First .. Last);
402 type Elements_Access is access Elements_Type;
404 use Ada.Finalization;
406 type Vector is new Controlled with record
407 Elements : Elements_Access;
408 Last : Extended_Index := No_Index;
413 type Vector_Access is access constant Vector;
414 for Vector_Access'Storage_Size use 0;
416 type Cursor is record
417 Container : Vector_Access;
418 Index : Index_Type := Index_Type'First;
422 (Stream : not null access Root_Stream_Type'Class;
425 for Vector'Write use Write;
428 (Stream : not null access Root_Stream_Type'Class;
429 Container : out Vector);
431 for Vector'Read use Read;
433 type Constant_Reference_Type
434 (Element : not null access constant Element_Type) is null record;
437 (Element : not null access Element_Type) is null record;
439 overriding procedure Adjust (Container : in out Vector);
441 overriding procedure Finalize (Container : in out Vector);
443 No_Element : constant Cursor := Cursor'(null, Index_Type'First);
444 Empty_Vector : constant Vector := (Controlled with null, No_Index, 0, 0);
446 end Ada.Containers.Vectors;