1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S
6 -- . F O R M A L _ I N D E F I N I T E _ V E C T O R S --
10 -- Copyright (C) 2014, Free Software Foundation, Inc. --
12 -- This specification is derived from the Ada Reference Manual for use with --
13 -- GNAT. The copyright notice above, and the license provisions that follow --
14 -- apply solely to the contents of the part following the private keyword. --
16 -- GNAT is free software; you can redistribute it and/or modify it under --
17 -- terms of the GNU General Public License as published by the Free Soft- --
18 -- ware Foundation; either version 3, or (at your option) any later ver- --
19 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
20 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
21 -- or FITNESS FOR A PARTICULAR PURPOSE. --
23 -- As a special exception under Section 7 of GPL version 3, you are granted --
24 -- additional permissions described in the GCC Runtime Library Exception, --
25 -- version 3.1, as published by the Free Software Foundation. --
27 -- You should have received a copy of the GNU General Public License and --
28 -- a copy of the GCC Runtime Library Exception along with this program; --
29 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
30 -- <http://www.gnu.org/licenses/>. --
31 ------------------------------------------------------------------------------
33 -- Similar to Ada.Containers.Formal_Vectors. The main difference is that
34 -- Element_Type may be indefinite (but not an unconstrained array). In
35 -- addition, this is simplified by removing less-used functionality.
37 with Ada
.Containers
.Bounded_Holders
;
38 with Ada
.Containers
.Formal_Vectors
;
41 type Index_Type
is range <>;
42 type Element_Type
(<>) is private;
43 Max_Size_In_Storage_Elements
: Natural :=
44 Element_Type
'Max_Size_In_Storage_Elements;
45 -- This has the same meaning as in Ada.Containers.Bounded_Holders, with the
48 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
50 Bounded
: Boolean := True;
51 -- If True, the containers are bounded; the initial capacity is the maximum
52 -- size, and heap allocation will be avoided. If False, the containers can
53 -- grow via heap allocation.
55 package Ada
.Containers
.Formal_Indefinite_Vectors
is
56 pragma Annotate
(GNATprove
, External_Axiomatization
);
58 subtype Extended_Index
is Index_Type
'Base
59 range Index_Type
'First - 1 ..
60 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
62 No_Index : constant Extended_Index := Extended_Index'First;
64 subtype Capacity_Range is
65 Count_Type range 0 .. Count_Type (Index_Type'Last - Index_Type'First + 1);
67 type Vector (Capacity : Capacity_Range) is limited private with
68 Default_Initial_Condition;
70 function Empty_Vector return Vector;
72 function "=" (Left, Right : Vector) return Boolean with
76 (New_Item : Element_Type;
77 Length : Capacity_Range) return Vector
81 function Capacity (Container : Vector) return Capacity_Range with
84 procedure Reserve_Capacity
85 (Container : in out Vector;
86 Capacity : Capacity_Range)
89 Pre => (if Bounded then Capacity <= Container.Capacity);
91 function Length (Container : Vector) return Capacity_Range with
94 function Is_Empty (Container : Vector) return Boolean with
97 procedure Clear (Container : in out Vector) with
99 -- Note that this reclaims storage in the unbounded case. You need to call
100 -- this before a container goes out of scope in order to avoid storage
103 procedure Assign (Target : in out Vector; Source : Vector) with
105 Pre => (if Bounded then Length (Source) <= Target.Capacity);
109 Capacity : Capacity_Range := 0) return Vector
112 Pre => (if Bounded then Length (Source) <= Capacity);
116 Index : Index_Type) return Element_Type
119 Pre => Index in First_Index (Container) .. Last_Index (Container);
121 procedure Replace_Element
122 (Container : in out Vector;
124 New_Item : Element_Type)
127 Pre => Index in First_Index (Container) .. Last_Index (Container);
130 (Container : in out Vector;
134 Pre => (if Bounded then
135 Length (Container) + Length (New_Item) <= Container.Capacity);
138 (Container : in out Vector;
139 New_Item : Element_Type)
142 Pre => (if Bounded then
143 Length (Container) < Container.Capacity);
145 procedure Delete_Last
146 (Container : in out Vector)
150 procedure Reverse_Elements (Container : in out Vector) with
153 procedure Swap (Container : in out Vector; I, J : Index_Type) with
155 Pre => I in First_Index (Container) .. Last_Index (Container)
156 and then J in First_Index (Container) .. Last_Index (Container);
158 function First_Index (Container : Vector) return Index_Type with
161 function First_Element (Container : Vector) return Element_Type with
163 Pre => not Is_Empty (Container);
165 function Last_Index (Container : Vector) return Extended_Index with
168 function Last_Element (Container : Vector) return Element_Type with
170 Pre => not Is_Empty (Container);
175 Index : Index_Type := Index_Type'First) return Extended_Index
179 function Reverse_Find_Index
182 Index : Index_Type := Index_Type'Last) return Extended_Index
188 Item : Element_Type) return Boolean
193 (Container : Vector; Position : Extended_Index) return Boolean with
197 with function "<" (Left, Right : Element_Type) return Boolean is <>;
198 package Generic_Sorting is
200 function Is_Sorted (Container : Vector) return Boolean with
203 procedure Sort (Container : in out Vector) with
208 function First_To_Previous
210 Current : Index_Type) return Vector
213 function Current_To_Last
215 Current : Index_Type) return Vector
221 pragma Inline (First_Index);
222 pragma Inline (Last_Index);
223 pragma Inline (Element);
224 pragma Inline (First_Element);
225 pragma Inline (Last_Element);
226 pragma Inline (Replace_Element);
227 pragma Inline (Contains);
229 -- The implementation method is to instantiate Bounded_Holders to get a
230 -- definite type for Element_Type, and then use that Holder type to
231 -- instantiate Formal_Vectors. All the operations are just wrappers.
233 package Holders is new Bounded_Holders
234 (Element_Type, Max_Size_In_Storage_Elements, "=");
237 package Def is new Formal_Vectors (Index_Type, Holder, "=", Bounded);
240 -- ????Assert that Def subtypes have the same range.
242 type Vector (Capacity : Capacity_Range) is limited record
243 V : Def.Vector (Capacity);
246 function Empty_Vector return Vector is
247 ((Capacity => 0, V => Def.Empty_Vector));
249 end Ada.Containers.Formal_Indefinite_Vectors;