1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FORMAL_INDEFINITE_VECTORS --
9 -- Copyright (C) 2014-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/>. --
30 ------------------------------------------------------------------------------
32 -- Similar to Ada.Containers.Formal_Vectors. The main difference is that
33 -- Element_Type may be indefinite (but not an unconstrained array). In
34 -- addition, this is simplified by removing less-used functionality.
36 with Ada
.Containers
.Bounded_Holders
;
37 with Ada
.Containers
.Formal_Vectors
;
40 type Index_Type
is range <>;
41 type Element_Type
(<>) is private;
42 Max_Size_In_Storage_Elements
: Natural :=
43 Element_Type
'Max_Size_In_Storage_Elements;
44 -- Maximum size of Vector elements in bytes. This has the same meaning as
45 -- in Ada.Containers.Bounded_Holders, with the same restrictions. Note that
46 -- setting this too small can lead to erroneous execution; see comments in
47 -- Ada.Containers.Bounded_Holders. If Element_Type is class-wide, it is the
48 -- responsibility of clients to calculate the maximum size of all types in
51 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
53 Bounded
: Boolean := True;
54 -- If True, the containers are bounded; the initial capacity is the maximum
55 -- size, and heap allocation will be avoided. If False, the containers can
56 -- grow via heap allocation.
58 package Ada
.Containers
.Formal_Indefinite_Vectors
with
61 pragma Annotate
(GNATprove
, External_Axiomatization
);
62 pragma Annotate
(CodePeer
, Skip_Analysis
);
64 subtype Extended_Index
is Index_Type
'Base
65 range Index_Type
'First - 1 ..
66 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
68 No_Index : constant Extended_Index := Extended_Index'First;
70 subtype Capacity_Range is
71 Count_Type range 0 .. Count_Type (Index_Type'Last - Index_Type'First + 1);
73 type Vector (Capacity : Capacity_Range) is limited private with
74 Default_Initial_Condition;
76 function Empty_Vector return Vector;
78 function "=" (Left, Right : Vector) return Boolean with
82 (New_Item : Element_Type;
83 Length : Capacity_Range) return Vector
87 function Capacity (Container : Vector) return Capacity_Range with
89 Post => Capacity'Result >= Container.Capacity;
91 procedure Reserve_Capacity
92 (Container : in out Vector;
93 Capacity : Capacity_Range)
96 Pre => (if Bounded then Capacity <= Container.Capacity);
98 function Length (Container : Vector) return Capacity_Range with
101 function Is_Empty (Container : Vector) return Boolean with
104 procedure Clear (Container : in out Vector) with
106 -- Note that this reclaims storage in the unbounded case. You need to call
107 -- this before a container goes out of scope in order to avoid storage
110 procedure Assign (Target : in out Vector; Source : Vector) with
112 Pre => (if Bounded then Length (Source) <= Target.Capacity);
116 Capacity : Capacity_Range := 0) return Vector
119 Pre => (if Bounded then (Capacity = 0 or Length (Source) <= Capacity));
123 Index : Index_Type) return Element_Type
126 Pre => Index in First_Index (Container) .. Last_Index (Container);
128 procedure Replace_Element
129 (Container : in out Vector;
131 New_Item : Element_Type)
134 Pre => Index in First_Index (Container) .. Last_Index (Container);
137 (Container : in out Vector;
142 then Length (Container) + Length (New_Item) <=
146 (Container : in out Vector;
147 New_Item : Element_Type)
151 then Length (Container) < Container.Capacity);
153 procedure Delete_Last
154 (Container : in out Vector)
158 procedure Reverse_Elements (Container : in out Vector) with
161 procedure Swap (Container : in out Vector; I, J : Index_Type) with
163 Pre => I in First_Index (Container) .. Last_Index (Container)
164 and then J in First_Index (Container) .. Last_Index (Container);
166 function First_Index (Container : Vector) return Index_Type with
169 function First_Element (Container : Vector) return Element_Type with
171 Pre => not Is_Empty (Container);
173 function Last_Index (Container : Vector) return Extended_Index with
176 function Last_Element (Container : Vector) return Element_Type with
178 Pre => not Is_Empty (Container);
183 Index : Index_Type := Index_Type'First) return Extended_Index
187 function Reverse_Find_Index
190 Index : Index_Type := Index_Type'Last) return Extended_Index
196 Item : Element_Type) return Boolean
201 (Container : Vector; Position : Extended_Index) return Boolean with
205 with function "<" (Left, Right : Element_Type) return Boolean is <>;
206 package Generic_Sorting with SPARK_Mode is
208 function Is_Sorted (Container : Vector) return Boolean with
211 procedure Sort (Container : in out Vector) with
216 function First_To_Previous
218 Current : Index_Type) return Vector
223 function Current_To_Last
225 Current : Index_Type) return Vector
231 pragma SPARK_Mode (Off);
233 pragma Inline (First_Index);
234 pragma Inline (Last_Index);
235 pragma Inline (Element);
236 pragma Inline (First_Element);
237 pragma Inline (Last_Element);
238 pragma Inline (Replace_Element);
239 pragma Inline (Contains);
241 -- The implementation method is to instantiate Bounded_Holders to get a
242 -- definite type for Element_Type, and then use that Holder type to
243 -- instantiate Formal_Vectors. All the operations are just wrappers.
245 package Holders is new Bounded_Holders
246 (Element_Type, Max_Size_In_Storage_Elements, "=");
249 package Def is new Formal_Vectors (Index_Type, Holder, "=", Bounded);
252 -- ????Assert that Def subtypes have the same range
254 type Vector (Capacity : Capacity_Range) is limited record
255 V : Def.Vector (Capacity);
258 function Empty_Vector return Vector is
259 ((Capacity => 0, V => Def.Empty_Vector));
261 end Ada.Containers.Formal_Indefinite_Vectors;