1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . B O U N D E D _ V E C T O R S --
9 -- Copyright (C) 2004-2023, 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
.Streams
;
38 private with Ada
.Finalization
;
39 private with Ada
.Strings
.Text_Buffers
;
42 type Index_Type
is range <>;
43 type Element_Type
is private;
45 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
47 package Ada
.Containers
.Bounded_Vectors
is
48 pragma Annotate
(CodePeer
, Skip_Analysis
);
52 subtype Extended_Index
is Index_Type
'Base
53 range Index_Type
'First - 1 ..
54 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
56 No_Index : constant Extended_Index := Extended_Index'First;
58 type Vector (Capacity : Count_Type) is tagged private with
59 Constant_Indexing => Constant_Reference,
60 Variable_Indexing => Reference,
61 Default_Iterator => Iterate,
62 Iterator_Element => Element_Type,
63 Aggregate => (Empty => Empty,
64 Add_Unnamed => Append,
65 New_Indexed => New_Vector,
66 Assign_Indexed => Replace_Element),
67 Preelaborable_Initialization
68 => Element_Type'Preelaborable_Initialization;
70 type Cursor is private with Preelaborable_Initialization;
72 Empty_Vector : constant Vector;
74 No_Element : constant Cursor;
76 function Has_Element (Position : Cursor) return Boolean;
78 package Vector_Iterator_Interfaces is new
79 Ada.Iterator_Interfaces (Cursor, Has_Element);
81 function Empty (Capacity : Count_Type := 10) return Vector;
82 pragma Ada_2022 (Empty);
84 overriding function "=" (Left, Right : Vector) return Boolean;
86 function New_Vector (First, Last : Index_Type) return Vector
87 with Pre => First = Index_Type'First;
88 -- Ada 2022 aggregate operation.
89 pragma Ada_2022 (New_Vector);
91 function To_Vector (Length : Count_Type) return Vector;
94 (New_Item : Element_Type;
95 Length : Count_Type) return Vector;
97 function "&" (Left, Right : Vector) return Vector;
99 function "&" (Left : Vector; Right : Element_Type) return Vector;
101 function "&" (Left : Element_Type; Right : Vector) return Vector;
103 function "&" (Left, Right : Element_Type) return Vector;
105 function Capacity (Container : Vector) return Count_Type;
107 procedure Reserve_Capacity
108 (Container : in out Vector;
109 Capacity : Count_Type);
111 function Length (Container : Vector) return Count_Type;
114 (Container : in out Vector;
115 Length : Count_Type);
117 function Is_Empty (Container : Vector) return Boolean;
119 procedure Clear (Container : in out Vector);
123 Index : Extended_Index) return Cursor;
125 function To_Index (Position : Cursor) return Extended_Index;
129 Index : Index_Type) return Element_Type;
131 function Element (Position : Cursor) return Element_Type;
133 procedure Replace_Element
134 (Container : in out Vector;
136 New_Item : Element_Type);
138 procedure Replace_Element
139 (Container : in out Vector;
141 New_Item : Element_Type);
143 procedure Query_Element
146 Process : not null access procedure (Element : Element_Type));
148 procedure Query_Element
150 Process : not null access procedure (Element : Element_Type));
152 procedure Update_Element
153 (Container : in out Vector;
155 Process : not null access procedure (Element : in out Element_Type));
157 procedure Update_Element
158 (Container : in out Vector;
160 Process : not null access procedure (Element : in out Element_Type));
162 type Constant_Reference_Type
163 (Element : not null access constant Element_Type) is
166 Implicit_Dereference => Element;
168 type Reference_Type (Element : not null access Element_Type) is private
170 Implicit_Dereference => Element;
172 function Constant_Reference
173 (Container : aliased Vector;
174 Position : Cursor) return Constant_Reference_Type;
177 (Container : aliased in out Vector;
178 Position : Cursor) return Reference_Type;
180 function Constant_Reference
181 (Container : aliased Vector;
182 Index : Index_Type) return Constant_Reference_Type;
185 (Container : aliased in out Vector;
186 Index : Index_Type) return Reference_Type;
188 procedure Assign (Target : in out Vector; Source : Vector);
190 function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector;
192 procedure Move (Target : in out Vector; Source : in out Vector);
194 procedure Insert_Vector
195 (Container : in out Vector;
196 Before : Extended_Index;
198 pragma Ada_2022 (Insert_Vector);
201 (Container : in out Vector;
202 Before : Extended_Index;
203 New_Item : Vector) renames Insert_Vector;
204 -- Retained for now for compatibility; AI12-0400 will remove this.
206 procedure Insert_Vector
207 (Container : in out Vector;
210 pragma Ada_2022 (Insert_Vector);
213 (Container : in out Vector;
215 New_Item : Vector) renames Insert_Vector;
216 -- Retained for now for compatibility; AI12-0400 will remove this.
218 procedure Insert_Vector
219 (Container : in out Vector;
222 Position : out Cursor);
223 pragma Ada_2022 (Insert_Vector);
226 (Container : in out Vector;
229 Position : out Cursor) renames Insert_Vector;
230 -- Retained for now for compatibility; AI12-0400 will remove this.
233 (Container : in out Vector;
234 Before : Extended_Index;
235 New_Item : Element_Type;
236 Count : Count_Type := 1);
239 (Container : in out Vector;
241 New_Item : Element_Type;
242 Count : Count_Type := 1);
245 (Container : in out Vector;
247 New_Item : Element_Type;
248 Position : out Cursor;
249 Count : Count_Type := 1);
252 (Container : in out Vector;
253 Before : Extended_Index;
254 Count : Count_Type := 1);
257 (Container : in out Vector;
259 Position : out Cursor;
260 Count : Count_Type := 1);
262 procedure Prepend_Vector
263 (Container : in out Vector;
265 pragma Ada_2022 (Prepend_Vector);
268 (Container : in out Vector;
269 New_Item : Vector) renames Prepend_Vector;
270 -- Retained for now for compatibility; AI12-0400 will remove this.
273 (Container : in out Vector;
274 New_Item : Element_Type;
275 Count : Count_Type := 1);
277 procedure Append_Vector
278 (Container : in out Vector;
280 pragma Ada_2022 (Append_Vector);
283 (Container : in out Vector;
284 New_Item : Vector) renames Append_Vector;
285 -- Retained for now for compatibility; AI12-0400 will remove this.
288 (Container : in out Vector;
289 New_Item : Element_Type;
292 procedure Append (Container : in out Vector;
293 New_Item : Element_Type);
295 procedure Insert_Space
296 (Container : in out Vector;
297 Before : Extended_Index;
298 Count : Count_Type := 1);
300 procedure Insert_Space
301 (Container : in out Vector;
303 Position : out Cursor;
304 Count : Count_Type := 1);
307 (Container : in out Vector;
308 Index : Extended_Index;
309 Count : Count_Type := 1);
312 (Container : in out Vector;
313 Position : in out Cursor;
314 Count : Count_Type := 1);
316 procedure Delete_First
317 (Container : in out Vector;
318 Count : Count_Type := 1);
320 procedure Delete_Last
321 (Container : in out Vector;
322 Count : Count_Type := 1);
324 procedure Reverse_Elements (Container : in out Vector);
326 procedure Swap (Container : in out Vector; I, J : Index_Type);
328 procedure Swap (Container : in out Vector; I, J : Cursor);
330 function First_Index (Container : Vector) return Index_Type;
332 function First (Container : Vector) return Cursor;
334 function First_Element (Container : Vector) return Element_Type;
336 function Last_Index (Container : Vector) return Extended_Index;
338 function Last (Container : Vector) return Cursor;
340 function Last_Element (Container : Vector) return Element_Type;
342 function Next (Position : Cursor) return Cursor;
344 procedure Next (Position : in out Cursor);
346 function Previous (Position : Cursor) return Cursor;
348 procedure Previous (Position : in out Cursor);
353 Index : Index_Type := Index_Type'First) return Extended_Index;
358 Position : Cursor := No_Element) return Cursor;
360 function Reverse_Find_Index
363 Index : Index_Type := Index_Type'Last) return Extended_Index;
365 function Reverse_Find
368 Position : Cursor := No_Element) return Cursor;
372 Item : Element_Type) return Boolean;
376 Process : not null access procedure (Position : Cursor));
378 procedure Reverse_Iterate
380 Process : not null access procedure (Position : Cursor));
384 return Vector_Iterator_Interfaces.Reversible_Iterator'Class;
389 return Vector_Iterator_Interfaces.Reversible_Iterator'class;
392 with function "<" (Left, Right : Element_Type) return Boolean is <>;
393 package Generic_Sorting is
395 function Is_Sorted (Container : Vector) return Boolean;
397 procedure Sort (Container : in out Vector);
399 procedure Merge (Target : in out Vector; Source : in out Vector);
405 pragma Inline (First_Index);
406 pragma Inline (Last_Index);
407 pragma Inline (Element);
408 pragma Inline (First_Element);
409 pragma Inline (Last_Element);
410 pragma Inline (Query_Element);
411 pragma Inline (Update_Element);
412 pragma Inline (Replace_Element);
413 pragma Inline (Is_Empty);
414 pragma Inline (Contains);
415 pragma Inline (Next);
416 pragma Inline (Previous);
418 use Ada.Containers.Helpers;
419 package Implementation is new Generic_Implementation;
423 use Ada.Finalization;
425 type Elements_Array is array (Count_Type range <>) of aliased Element_Type;
426 function "=" (L, R : Elements_Array) return Boolean is abstract;
428 type Vector (Capacity : Count_Type) is tagged record
429 Elements : Elements_Array (1 .. Capacity);
430 Last : Extended_Index := No_Index;
431 TC : aliased Tamper_Counts;
432 end record with Put_Image => Put_Image;
435 (S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Vector);
438 (Stream : not null access Root_Stream_Type'Class;
441 for Vector'Write use Write;
444 (Stream : not null access Root_Stream_Type'Class;
445 Container : out Vector);
447 for Vector'Read use Read;
449 type Vector_Access is access all Vector;
450 for Vector_Access'Storage_Size use 0;
452 type Cursor is record
453 Container : Vector_Access;
454 Index : Index_Type := Index_Type'First;
458 (Stream : not null access Root_Stream_Type'Class;
461 for Cursor'Write use Write;
464 (Stream : not null access Root_Stream_Type'Class;
465 Position : out Cursor);
467 for Cursor'Read use Read;
469 subtype Reference_Control_Type is Implementation.Reference_Control_Type;
470 -- It is necessary to rename this here, so that the compiler can find it
472 type Constant_Reference_Type
473 (Element : not null access constant Element_Type) is
475 Control : Reference_Control_Type :=
476 raise Program_Error with "uninitialized reference";
477 -- The RM says, "The default initialization of an object of
478 -- type Constant_Reference_Type or Reference_Type propagates
483 (Stream : not null access Root_Stream_Type'Class;
484 Item : out Constant_Reference_Type);
486 for Constant_Reference_Type'Read use Read;
489 (Stream : not null access Root_Stream_Type'Class;
490 Item : Constant_Reference_Type);
492 for Constant_Reference_Type'Write use Write;
494 type Reference_Type (Element : not null access Element_Type) is record
495 Control : Reference_Control_Type :=
496 raise Program_Error with "uninitialized reference";
497 -- The RM says, "The default initialization of an object of
498 -- type Constant_Reference_Type or Reference_Type propagates
503 (Stream : not null access Root_Stream_Type'Class;
504 Item : out Reference_Type);
506 for Reference_Type'Read use Read;
509 (Stream : not null access Root_Stream_Type'Class;
510 Item : Reference_Type);
512 for Reference_Type'Write use Write;
514 -- See Ada.Containers.Vectors for documentation on the following
516 procedure _Next (Position : in out Cursor) renames Next;
517 procedure _Previous (Position : in out Cursor) renames Previous;
519 function Pseudo_Reference
520 (Container : aliased Vector'Class) return Reference_Control_Type;
521 pragma Inline (Pseudo_Reference);
522 -- Creates an object of type Reference_Control_Type pointing to the
523 -- container, and increments the Lock. Finalization of this object will
524 -- decrement the Lock.
526 type Element_Access is access all Element_Type with
529 function Get_Element_Access
530 (Position : Cursor) return not null Element_Access;
531 -- Returns a pointer to the element designated by Position.
533 Empty_Vector : constant Vector := (Capacity => 0, others => <>);
535 No_Element : constant Cursor := Cursor'(null, Index_Type
'First);
537 type Iterator
is new Limited_Controlled
and
538 Vector_Iterator_Interfaces
.Reversible_Iterator
with
540 Container
: Vector_Access
;
541 Index
: Index_Type
'Base;
543 with Disable_Controlled
=> not T_Check
;
545 overriding
procedure Finalize
(Object
: in out Iterator
);
547 overriding
function First
(Object
: Iterator
) return Cursor
;
548 overriding
function Last
(Object
: Iterator
) return Cursor
;
550 overriding
function Next
552 Position
: Cursor
) return Cursor
;
554 overriding
function Previous
556 Position
: Cursor
) return Cursor
;
558 end Ada
.Containers
.Bounded_Vectors
;