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-2012, 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 with Ada.Iterator_Interfaces;
36 private with Ada.Finalization;
37 private with Ada.Streams;
40 type Index_Type is range <>;
41 type Element_Type is private;
43 with function "=" (Left, Right : Element_Type) return Boolean is <>;
45 package Ada.Containers.Vectors is
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;
53 No_Index : constant Extended_Index := Extended_Index'First;
55 type Vector is tagged private
57 Constant_Indexing => Constant_Reference,
58 Variable_Indexing => Reference,
59 Default_Iterator => Iterate,
60 Iterator_Element => Element_Type;
61 pragma Preelaborable_Initialization (Vector);
63 type Cursor is private;
64 pragma Preelaborable_Initialization (Cursor);
66 No_Element : constant Cursor;
68 function Has_Element (Position : Cursor) return Boolean;
70 package Vector_Iterator_Interfaces is new
71 Ada.Iterator_Interfaces (Cursor, Has_Element);
73 Empty_Vector : constant Vector;
75 overriding function "=" (Left, Right : Vector) return Boolean;
77 function To_Vector (Length : Count_Type) return Vector;
80 (New_Item : Element_Type;
81 Length : Count_Type) return Vector;
83 function "&" (Left, Right : Vector) return Vector;
85 function "&" (Left : Vector; Right : Element_Type) return Vector;
87 function "&" (Left : Element_Type; Right : Vector) return Vector;
89 function "&" (Left, Right : Element_Type) return Vector;
91 function Capacity (Container : Vector) return Count_Type;
93 procedure Reserve_Capacity
94 (Container : in out Vector;
95 Capacity : Count_Type);
97 function Length (Container : Vector) return Count_Type;
100 (Container : in out Vector;
101 Length : Count_Type);
103 function Is_Empty (Container : Vector) return Boolean;
105 procedure Clear (Container : in out Vector);
109 Index : Extended_Index) return Cursor;
111 function To_Index (Position : Cursor) return Extended_Index;
115 Index : Index_Type) return Element_Type;
117 function Element (Position : Cursor) return Element_Type;
119 procedure Replace_Element
120 (Container : in out Vector;
122 New_Item : Element_Type);
124 procedure Replace_Element
125 (Container : in out Vector;
127 New_Item : Element_Type);
129 procedure Query_Element
132 Process : not null access procedure (Element : Element_Type));
134 procedure Query_Element
136 Process : not null access procedure (Element : Element_Type));
138 procedure Update_Element
139 (Container : in out Vector;
141 Process : not null access procedure (Element : in out Element_Type));
143 procedure Update_Element
144 (Container : in out Vector;
146 Process : not null access procedure (Element : in out Element_Type));
148 type Constant_Reference_Type
149 (Element : not null access constant Element_Type) is
152 Implicit_Dereference => Element;
154 type Reference_Type (Element : not null access Element_Type) is private
156 Implicit_Dereference => Element;
158 function Constant_Reference
159 (Container : aliased Vector;
160 Position : Cursor) return Constant_Reference_Type;
163 (Container : aliased in out Vector;
164 Position : Cursor) return Reference_Type;
166 function Constant_Reference
167 (Container : aliased Vector;
168 Index : Index_Type) return Constant_Reference_Type;
171 (Container : aliased in out Vector;
172 Index : Index_Type) return Reference_Type;
174 procedure Assign (Target : in out Vector; Source : Vector);
176 function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
178 procedure Move (Target : in out Vector; Source : in out Vector);
181 (Container : in out Vector;
182 Before : Extended_Index;
186 (Container : in out Vector;
191 (Container : in out Vector;
194 Position : out Cursor);
197 (Container : in out Vector;
198 Before : Extended_Index;
199 New_Item : Element_Type;
200 Count : Count_Type := 1);
203 (Container : in out Vector;
205 New_Item : Element_Type;
206 Count : Count_Type := 1);
209 (Container : in out Vector;
211 New_Item : Element_Type;
212 Position : out Cursor;
213 Count : Count_Type := 1);
216 (Container : in out Vector;
217 Before : Extended_Index;
218 Count : Count_Type := 1);
221 (Container : in out Vector;
223 Position : out Cursor;
224 Count : Count_Type := 1);
227 (Container : in out Vector;
231 (Container : in out Vector;
232 New_Item : Element_Type;
233 Count : Count_Type := 1);
236 (Container : in out Vector;
240 (Container : in out Vector;
241 New_Item : Element_Type;
242 Count : Count_Type := 1);
244 procedure Insert_Space
245 (Container : in out Vector;
246 Before : Extended_Index;
247 Count : Count_Type := 1);
249 procedure Insert_Space
250 (Container : in out Vector;
252 Position : out Cursor;
253 Count : Count_Type := 1);
256 (Container : in out Vector;
257 Index : Extended_Index;
258 Count : Count_Type := 1);
261 (Container : in out Vector;
262 Position : in out Cursor;
263 Count : Count_Type := 1);
265 procedure Delete_First
266 (Container : in out Vector;
267 Count : Count_Type := 1);
269 procedure Delete_Last
270 (Container : in out Vector;
271 Count : Count_Type := 1);
273 procedure Reverse_Elements (Container : in out Vector);
275 procedure Swap (Container : in out Vector; I, J : Index_Type);
277 procedure Swap (Container : in out Vector; I, J : Cursor);
279 function First_Index (Container : Vector) return Index_Type;
281 function First (Container : Vector) return Cursor;
283 function First_Element (Container : Vector) return Element_Type;
285 function Last_Index (Container : Vector) return Extended_Index;
287 function Last (Container : Vector) return Cursor;
289 function Last_Element (Container : Vector) return Element_Type;
291 function Next (Position : Cursor) return Cursor;
293 procedure Next (Position : in out Cursor);
295 function Previous (Position : Cursor) return Cursor;
297 procedure Previous (Position : in out Cursor);
302 Index : Index_Type := Index_Type'First) return Extended_Index;
307 Position : Cursor := No_Element) return Cursor;
309 function Reverse_Find_Index
312 Index : Index_Type := Index_Type'Last) return Extended_Index;
314 function Reverse_Find
317 Position : Cursor := No_Element) return Cursor;
321 Item : Element_Type) return Boolean;
325 Process : not null access procedure (Position : Cursor));
327 procedure Reverse_Iterate
329 Process : not null access procedure (Position : Cursor));
331 function Iterate (Container : Vector)
332 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
334 function Iterate (Container : Vector; Start : Cursor)
335 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
338 with function "<" (Left, Right : Element_Type) return Boolean is <>;
339 package Generic_Sorting is
341 function Is_Sorted (Container : Vector) return Boolean;
343 procedure Sort (Container : in out Vector);
345 procedure Merge (Target : in out Vector; Source : in out Vector);
351 pragma Inline (First_Index);
352 pragma Inline (Last_Index);
353 pragma Inline (Element);
354 pragma Inline (First_Element);
355 pragma Inline (Last_Element);
356 pragma Inline (Query_Element);
357 pragma Inline (Update_Element);
358 pragma Inline (Replace_Element);
359 pragma Inline (Is_Empty);
360 pragma Inline (Contains);
361 pragma Inline (Next);
362 pragma Inline (Previous);
364 type Elements_Array is array (Index_Type range <>) of aliased Element_Type;
365 function "=" (L, R : Elements_Array) return Boolean is abstract;
367 type Elements_Type (Last : Index_Type) is limited record
368 EA : Elements_Array (Index_Type'First .. Last);
371 type Elements_Access is access Elements_Type;
373 use Ada.Finalization;
376 type Vector is new Controlled with record
377 Elements : Elements_Access;
378 Last : Extended_Index := No_Index;
383 overriding procedure Adjust (Container : in out Vector);
385 overriding procedure Finalize (Container : in out Vector);
388 (Stream : not null access Root_Stream_Type'Class;
391 for Vector'Write use Write;
394 (Stream : not null access Root_Stream_Type'Class;
395 Container : out Vector);
397 for Vector'Read use Read;
399 type Vector_Access is access all Vector;
400 for Vector_Access'Storage_Size use 0;
402 type Cursor is record
403 Container : Vector_Access;
404 Index : Index_Type := Index_Type'First;
408 (Stream : not null access Root_Stream_Type'Class;
409 Position : out Cursor);
411 for Cursor'Read use Read;
414 (Stream : not null access Root_Stream_Type'Class;
417 for Cursor'Write use Write;
419 type Constant_Reference_Type
420 (Element : not null access constant Element_Type) is null record;
423 (Stream : not null access Root_Stream_Type'Class;
424 Item : Constant_Reference_Type);
426 for Constant_Reference_Type'Write use Write;
429 (Stream : not null access Root_Stream_Type'Class;
430 Item : out Constant_Reference_Type);
432 for Constant_Reference_Type'Read use Read;
435 (Element : not null access Element_Type) is null record;
438 (Stream : not null access Root_Stream_Type'Class;
439 Item : Reference_Type);
441 for Reference_Type'Write use Write;
444 (Stream : not null access Root_Stream_Type'Class;
445 Item : out Reference_Type);
447 for Reference_Type'Read use Read;
449 No_Element : constant Cursor := Cursor'(null, Index_Type'First);
451 Empty_Vector : constant Vector := (Controlled with null, No_Index, 0, 0);
453 end Ada.Containers.Vectors;