1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
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 --
9 -- Copyright (C) 2004-2015, 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 with Ada
.Containers
.Helpers
;
37 private with Ada
.Finalization
;
38 private with Ada
.Streams
;
41 type Index_Type
is range <>;
42 type Element_Type
(<>) is private;
44 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
46 package Ada
.Containers
.Indefinite_Vectors
is
47 pragma Annotate
(CodePeer
, Skip_Analysis
);
51 subtype Extended_Index
is Index_Type
'Base
52 range Index_Type
'First - 1 ..
53 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
55 No_Index : constant Extended_Index := Extended_Index'First;
57 type Vector is tagged private
59 Constant_Indexing => Constant_Reference,
60 Variable_Indexing => Reference,
61 Default_Iterator => Iterate,
62 Iterator_Element => Element_Type;
64 pragma Preelaborable_Initialization (Vector);
66 type Cursor is private;
67 pragma Preelaborable_Initialization (Cursor);
69 Empty_Vector : constant Vector;
71 No_Element : constant Cursor;
73 function Has_Element (Position : Cursor) return Boolean;
75 package Vector_Iterator_Interfaces is new
76 Ada.Iterator_Interfaces (Cursor, Has_Element);
78 overriding function "=" (Left, Right : Vector) return Boolean;
80 function To_Vector (Length : Count_Type) return Vector;
83 (New_Item : Element_Type;
84 Length : Count_Type) return Vector;
86 function "&" (Left, Right : Vector) return Vector;
88 function "&" (Left : Vector; Right : Element_Type) return Vector;
90 function "&" (Left : Element_Type; Right : Vector) return Vector;
92 function "&" (Left, Right : Element_Type) return Vector;
94 function Capacity (Container : Vector) return Count_Type;
96 procedure Reserve_Capacity
97 (Container : in out Vector;
98 Capacity : Count_Type);
100 function Length (Container : Vector) return Count_Type;
103 (Container : in out Vector;
104 Length : Count_Type);
106 function Is_Empty (Container : Vector) return Boolean;
108 procedure Clear (Container : in out Vector);
110 type Constant_Reference_Type
111 (Element : not null access constant Element_Type) is private
113 Implicit_Dereference => Element;
115 type Reference_Type (Element : not null access Element_Type) is private
117 Implicit_Dereference => Element;
119 function Constant_Reference
120 (Container : aliased Vector;
121 Position : Cursor) return Constant_Reference_Type;
122 pragma Inline (Constant_Reference);
125 (Container : aliased in out Vector;
126 Position : Cursor) return Reference_Type;
127 pragma Inline (Reference);
129 function Constant_Reference
130 (Container : aliased Vector;
131 Index : Index_Type) return Constant_Reference_Type;
132 pragma Inline (Constant_Reference);
135 (Container : aliased in out Vector;
136 Index : Index_Type) return Reference_Type;
137 pragma Inline (Reference);
141 Index : Extended_Index) return Cursor;
143 function To_Index (Position : Cursor) return Extended_Index;
147 Index : Index_Type) return Element_Type;
149 function Element (Position : Cursor) return Element_Type;
151 procedure Replace_Element
152 (Container : in out Vector;
154 New_Item : Element_Type);
156 procedure Replace_Element
157 (Container : in out Vector;
159 New_Item : Element_Type);
161 procedure Query_Element
164 Process : not null access procedure (Element : Element_Type));
166 procedure Query_Element
168 Process : not null access procedure (Element : Element_Type));
170 procedure Update_Element
171 (Container : in out Vector;
173 Process : not null access procedure (Element : in out Element_Type));
175 procedure Update_Element
176 (Container : in out Vector;
178 Process : not null access procedure (Element : in out Element_Type));
180 procedure Assign (Target : in out Vector; Source : Vector);
182 function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
184 procedure Move (Target : in out Vector; Source : in out Vector);
187 (Container : in out Vector;
188 Before : Extended_Index;
192 (Container : in out Vector;
197 (Container : in out Vector;
200 Position : out Cursor);
203 (Container : in out Vector;
204 Before : Extended_Index;
205 New_Item : Element_Type;
206 Count : Count_Type := 1);
209 (Container : in out Vector;
211 New_Item : Element_Type;
212 Count : Count_Type := 1);
215 (Container : in out Vector;
217 New_Item : Element_Type;
218 Position : out Cursor;
219 Count : Count_Type := 1);
222 (Container : in out Vector;
226 (Container : in out Vector;
227 New_Item : Element_Type;
228 Count : Count_Type := 1);
231 (Container : in out Vector;
235 (Container : in out Vector;
236 New_Item : Element_Type;
237 Count : Count_Type := 1);
239 procedure Insert_Space
240 (Container : in out Vector;
241 Before : Extended_Index;
242 Count : Count_Type := 1);
244 procedure Insert_Space
245 (Container : in out Vector;
247 Position : out Cursor;
248 Count : Count_Type := 1);
251 (Container : in out Vector;
252 Index : Extended_Index;
253 Count : Count_Type := 1);
256 (Container : in out Vector;
257 Position : in out Cursor;
258 Count : Count_Type := 1);
260 procedure Delete_First
261 (Container : in out Vector;
262 Count : Count_Type := 1);
264 procedure Delete_Last
265 (Container : in out Vector;
266 Count : Count_Type := 1);
268 procedure Reverse_Elements (Container : in out Vector);
270 procedure Swap (Container : in out Vector; I, J : Index_Type);
272 procedure Swap (Container : in out Vector; I, J : Cursor);
274 function First_Index (Container : Vector) return Index_Type;
276 function First (Container : Vector) return Cursor;
278 function First_Element (Container : Vector) return Element_Type;
280 function Last_Index (Container : Vector) return Extended_Index;
282 function Last (Container : Vector) return Cursor;
284 function Last_Element (Container : Vector) return Element_Type;
286 function Next (Position : Cursor) return Cursor;
288 procedure Next (Position : in out Cursor);
290 function Previous (Position : Cursor) return Cursor;
292 procedure Previous (Position : in out Cursor);
297 Index : Index_Type := Index_Type'First) return Extended_Index;
302 Position : Cursor := No_Element) return Cursor;
304 function Reverse_Find_Index
307 Index : Index_Type := Index_Type'Last) return Extended_Index;
309 function Reverse_Find
312 Position : Cursor := No_Element) return Cursor;
316 Item : Element_Type) return Boolean;
320 Process : not null access procedure (Position : Cursor));
322 function Iterate (Container : Vector)
323 return Vector_Iterator_Interfaces.Reversible_Iterator'class;
328 return Vector_Iterator_Interfaces.Reversible_Iterator'class;
330 procedure Reverse_Iterate
332 Process : not null access procedure (Position : Cursor));
335 with function "<" (Left, Right : Element_Type) return Boolean is <>;
336 package Generic_Sorting is
338 function Is_Sorted (Container : Vector) return Boolean;
340 procedure Sort (Container : in out Vector);
342 procedure Merge (Target : in out Vector; Source : in out Vector);
348 pragma Inline (Append);
349 pragma Inline (First_Index);
350 pragma Inline (Last_Index);
351 pragma Inline (Element);
352 pragma Inline (First_Element);
353 pragma Inline (Last_Element);
354 pragma Inline (Query_Element);
355 pragma Inline (Update_Element);
356 pragma Inline (Replace_Element);
357 pragma Inline (Is_Empty);
358 pragma Inline (Contains);
359 pragma Inline (Next);
360 pragma Inline (Previous);
362 use Ada.Containers.Helpers;
363 package Implementation is new Generic_Implementation;
366 type Element_Access is access Element_Type;
368 type Elements_Array is array (Index_Type range <>) of Element_Access;
369 function "=" (L, R : Elements_Array) return Boolean is abstract;
371 type Elements_Type (Last : Extended_Index) is limited record
372 EA : Elements_Array (Index_Type'First .. Last);
375 type Elements_Access is access all Elements_Type;
380 type Vector is new Controlled with record
381 Elements : Elements_Access := null;
382 Last : Extended_Index := No_Index;
383 TC : aliased Tamper_Counts;
386 overriding procedure Adjust (Container : in out Vector);
387 overriding procedure Finalize (Container : in out Vector);
390 (Stream : not null access Root_Stream_Type'Class;
393 for Vector'Write use Write;
396 (Stream : not null access Root_Stream_Type'Class;
397 Container : out Vector);
399 for Vector'Read use Read;
401 type Vector_Access is access all Vector;
402 for Vector_Access'Storage_Size use 0;
404 type Cursor is record
405 Container : Vector_Access;
406 Index : Index_Type := Index_Type'First;
410 (Stream : not null access Root_Stream_Type'Class;
411 Position : out Cursor);
413 for Cursor'Read use Read;
416 (Stream : not null access Root_Stream_Type'Class;
419 for Cursor'Write use Write;
421 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
422 -- It is necessary to rename this here, so that the compiler can find it
424 type Constant_Reference_Type
425 (Element : not null access constant Element_Type) is
427 Control : Reference_Control_Type :=
428 raise Program_Error with "uninitialized reference";
429 -- The RM says, "The default initialization of an object of
430 -- type Constant_Reference_Type or Reference_Type propagates
435 (Stream : not null access Root_Stream_Type'Class;
436 Item : Constant_Reference_Type);
438 for Constant_Reference_Type'Write use Write;
441 (Stream : not null access Root_Stream_Type'Class;
442 Item : out Constant_Reference_Type);
444 for Constant_Reference_Type'Read use Read;
447 (Element : not null access Element_Type) is
449 Control : Reference_Control_Type :=
450 raise Program_Error with "uninitialized reference";
451 -- The RM says, "The default initialization of an object of
452 -- type Constant_Reference_Type or Reference_Type propagates
457 (Stream : not null access Root_Stream_Type'Class;
458 Item : Reference_Type);
460 for Reference_Type'Write use Write;
463 (Stream : not null access Root_Stream_Type'Class;
464 Item : out Reference_Type);
466 for Reference_Type'Read use Read;
468 -- Three operations are used to optimize in the expansion of "for ... of"
469 -- loops: the Next(Cursor) procedure in the visible part, and the following
470 -- Pseudo_Reference and Get_Element_Access functions. See Exp_Ch5 for
473 function Pseudo_Reference
474 (Container : aliased Vector'Class) return Reference_Control_Type;
475 pragma Inline (Pseudo_Reference);
476 -- Creates an object of type Reference_Control_Type pointing to the
477 -- container, and increments the Lock. Finalization of this object will
478 -- decrement the Lock.
480 function Get_Element_Access
481 (Position : Cursor) return not null Element_Access;
482 -- Returns a pointer to the element designated by Position.
484 No_Element : constant Cursor := Cursor'(null, Index_Type
'First);
486 Empty_Vector
: constant Vector
:= (Controlled
with others => <>);
488 type Iterator
is new Limited_Controlled
and
489 Vector_Iterator_Interfaces
.Reversible_Iterator
with
491 Container
: Vector_Access
;
492 Index
: Index_Type
'Base;
494 with Disable_Controlled
=> not T_Check
;
496 overriding
procedure Finalize
(Object
: in out Iterator
);
498 overriding
function First
(Object
: Iterator
) return Cursor
;
499 overriding
function Last
(Object
: Iterator
) return Cursor
;
501 overriding
function Next
503 Position
: Cursor
) return Cursor
;
505 overriding
function Previous
507 Position
: Cursor
) return Cursor
;
509 end Ada
.Containers
.Indefinite_Vectors
;