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 Move (Target : in out Vector; Source : in out Vector);
207 (Container : in out Vector;
208 Before : Extended_Index;
212 (Container : in out Vector;
217 (Container : in out Vector;
220 Position : out Cursor);
223 (Container : in out Vector;
224 Before : Extended_Index;
225 New_Item : Element_Type;
226 Count : Count_Type := 1);
229 (Container : in out Vector;
231 New_Item : Element_Type;
232 Count : Count_Type := 1);
235 (Container : in out Vector;
237 New_Item : Element_Type;
238 Position : out Cursor;
239 Count : Count_Type := 1);
242 (Container : in out Vector;
243 Before : Extended_Index;
244 Count : Count_Type := 1);
247 (Container : in out Vector;
249 Position : out Cursor;
250 Count : Count_Type := 1);
253 (Container : in out Vector;
257 (Container : in out Vector;
258 New_Item : Element_Type;
259 Count : Count_Type := 1);
262 (Container : in out Vector;
266 (Container : in out Vector;
267 New_Item : Element_Type;
268 Count : Count_Type := 1);
270 procedure Insert_Space
271 (Container : in out Vector;
272 Before : Extended_Index;
273 Count : Count_Type := 1);
275 procedure Insert_Space
276 (Container : in out Vector;
278 Position : out Cursor;
279 Count : Count_Type := 1);
282 (Container : in out Vector;
283 Index : Extended_Index;
284 Count : Count_Type := 1);
287 (Container : in out Vector;
288 Position : in out Cursor;
289 Count : Count_Type := 1);
291 procedure Delete_First
292 (Container : in out Vector;
293 Count : Count_Type := 1);
295 procedure Delete_Last
296 (Container : in out Vector;
297 Count : Count_Type := 1);
299 procedure Reverse_Elements (Container : in out Vector);
301 procedure Swap (Container : in out Vector; I, J : Index_Type);
303 procedure Swap (Container : in out Vector; I, J : Cursor);
305 function First_Index (Container : Vector) return Index_Type;
307 function First (Container : Vector) return Cursor;
309 function First_Element (Container : Vector) return Element_Type;
311 function Last_Index (Container : Vector) return Extended_Index;
313 function Last (Container : Vector) return Cursor;
315 function Last_Element (Container : Vector) return Element_Type;
317 function Next (Position : Cursor) return Cursor;
319 procedure Next (Position : in out Cursor);
321 function Previous (Position : Cursor) return Cursor;
323 procedure Previous (Position : in out Cursor);
328 Index : Index_Type := Index_Type'First) return Extended_Index;
333 Position : Cursor := No_Element) return Cursor;
335 function Reverse_Find_Index
338 Index : Index_Type := Index_Type'Last) return Extended_Index;
340 function Reverse_Find
343 Position : Cursor := No_Element) return Cursor;
347 Item : Element_Type) return Boolean;
351 Process : not null access procedure (Position : Cursor));
353 procedure Reverse_Iterate
355 Process : not null access procedure (Position : Cursor));
357 function Iterate (Container : Vector)
358 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
360 function Iterate (Container : Vector; Start : Cursor)
361 return Vector_Iterator_Interfaces.Forward_Iterator'Class;
364 with function "<" (Left, Right : Element_Type) return Boolean is <>;
365 package Generic_Sorting is
367 function Is_Sorted (Container : Vector) return Boolean;
369 procedure Sort (Container : in out Vector);
371 procedure Merge (Target : in out Vector; Source : in out Vector);
377 pragma Inline (First_Index);
378 pragma Inline (Last_Index);
379 pragma Inline (Element);
380 pragma Inline (First_Element);
381 pragma Inline (Last_Element);
382 pragma Inline (Query_Element);
383 pragma Inline (Update_Element);
384 pragma Inline (Replace_Element);
385 pragma Inline (Is_Empty);
386 pragma Inline (Contains);
387 pragma Inline (Next);
388 pragma Inline (Previous);
390 type Elements_Array is array (Index_Type range <>) of aliased Element_Type;
391 function "=" (L, R : Elements_Array) return Boolean is abstract;
393 type Elements_Type (Last : Index_Type) is limited record
394 EA : Elements_Array (Index_Type'First .. Last);
397 type Elements_Access is access Elements_Type;
399 use Ada.Finalization;
401 type Vector is new Controlled with record
402 Elements : Elements_Access;
403 Last : Extended_Index := No_Index;
408 type Vector_Access is access constant Vector;
409 for Vector_Access'Storage_Size use 0;
411 type Cursor is record
412 Container : Vector_Access;
413 Index : Index_Type := Index_Type'First;
417 (Stream : not null access Root_Stream_Type'Class;
420 for Vector'Write use Write;
423 (Stream : not null access Root_Stream_Type'Class;
424 Container : out Vector);
426 for Vector'Read use Read;
428 type Constant_Reference_Type
429 (Element : not null access constant Element_Type) is null record;
432 (Element : not null access Element_Type) is null record;
434 overriding procedure Adjust (Container : in out Vector);
436 overriding procedure Finalize (Container : in out Vector);
438 No_Element : constant Cursor := Cursor'(null, Index_Type'First);
439 Empty_Vector : constant Vector := (Controlled with null, No_Index, 0, 0);
441 end Ada.Containers.Vectors;