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
with
58 pragma Annotate
(GNATprove
, External_Axiomatization
);
60 subtype Extended_Index
is Index_Type
'Base
61 range Index_Type
'First - 1 ..
62 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
64 No_Index : constant Extended_Index := Extended_Index'First;
66 subtype Capacity_Range is
67 Count_Type range 0 .. Count_Type (Index_Type'Last - Index_Type'First + 1);
69 type Vector (Capacity : Capacity_Range) is limited private with
70 Default_Initial_Condition;
72 function Empty_Vector return Vector;
74 function "=" (Left, Right : Vector) return Boolean with
78 (New_Item : Element_Type;
79 Length : Capacity_Range) return Vector
83 function Capacity (Container : Vector) return Capacity_Range with
86 procedure Reserve_Capacity
87 (Container : in out Vector;
88 Capacity : Capacity_Range)
91 Pre => (if Bounded then Capacity <= Container.Capacity);
93 function Length (Container : Vector) return Capacity_Range with
96 function Is_Empty (Container : Vector) return Boolean with
99 procedure Clear (Container : in out Vector) with
101 -- Note that this reclaims storage in the unbounded case. You need to call
102 -- this before a container goes out of scope in order to avoid storage
105 procedure Assign (Target : in out Vector; Source : Vector) with
107 Pre => (if Bounded then Length (Source) <= Target.Capacity);
111 Capacity : Capacity_Range := 0) return Vector
114 Pre => (if Bounded then Length (Source) <= Capacity);
118 Index : Index_Type) return Element_Type
121 Pre => Index in First_Index (Container) .. Last_Index (Container);
123 procedure Replace_Element
124 (Container : in out Vector;
126 New_Item : Element_Type)
129 Pre => Index in First_Index (Container) .. Last_Index (Container);
132 (Container : in out Vector;
136 Pre => (if Bounded then
137 Length (Container) + Length (New_Item) <= Container.Capacity);
140 (Container : in out Vector;
141 New_Item : Element_Type)
144 Pre => (if Bounded then
145 Length (Container) < Container.Capacity);
147 procedure Delete_Last
148 (Container : in out Vector)
152 procedure Reverse_Elements (Container : in out Vector) with
155 procedure Swap (Container : in out Vector; I, J : Index_Type) with
157 Pre => I in First_Index (Container) .. Last_Index (Container)
158 and then J in First_Index (Container) .. Last_Index (Container);
160 function First_Index (Container : Vector) return Index_Type with
163 function First_Element (Container : Vector) return Element_Type with
165 Pre => not Is_Empty (Container);
167 function Last_Index (Container : Vector) return Extended_Index with
170 function Last_Element (Container : Vector) return Element_Type with
172 Pre => not Is_Empty (Container);
177 Index : Index_Type := Index_Type'First) return Extended_Index
181 function Reverse_Find_Index
184 Index : Index_Type := Index_Type'Last) return Extended_Index
190 Item : Element_Type) return Boolean
195 (Container : Vector; Position : Extended_Index) return Boolean with
199 with function "<" (Left, Right : Element_Type) return Boolean is <>;
200 package Generic_Sorting is
202 function Is_Sorted (Container : Vector) return Boolean with
205 procedure Sort (Container : in out Vector) with
210 function First_To_Previous
212 Current : Index_Type) return Vector
217 function Current_To_Last
219 Current : Index_Type) return Vector
225 pragma SPARK_Mode (Off);
227 pragma Inline (First_Index);
228 pragma Inline (Last_Index);
229 pragma Inline (Element);
230 pragma Inline (First_Element);
231 pragma Inline (Last_Element);
232 pragma Inline (Replace_Element);
233 pragma Inline (Contains);
235 -- The implementation method is to instantiate Bounded_Holders to get a
236 -- definite type for Element_Type, and then use that Holder type to
237 -- instantiate Formal_Vectors. All the operations are just wrappers.
239 package Holders is new Bounded_Holders
240 (Element_Type, Max_Size_In_Storage_Elements, "=");
243 package Def is new Formal_Vectors (Index_Type, Holder, "=", Bounded);
246 -- ????Assert that Def subtypes have the same range.
248 type Vector (Capacity : Capacity_Range) is limited record
249 V : Def.Vector (Capacity);
252 function Empty_Vector return Vector is
253 ((Capacity => 0, V => Def.Empty_Vector));
255 end Ada.Containers.Formal_Indefinite_Vectors;