1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- ADA.CONTAINERS.FORMAL_INDEFINITE_VECTORS --
9 -- Copyright (C) 2014, 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 -- This has the same meaning as in Ada.Containers.Bounded_Holders, with the
47 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
49 Bounded
: Boolean := True;
50 -- If True, the containers are bounded; the initial capacity is the maximum
51 -- size, and heap allocation will be avoided. If False, the containers can
52 -- grow via heap allocation.
54 package Ada
.Containers
.Formal_Indefinite_Vectors
with
57 pragma Annotate
(GNATprove
, External_Axiomatization
);
59 subtype Extended_Index
is Index_Type
'Base
60 range Index_Type
'First - 1 ..
61 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
63 No_Index : constant Extended_Index := Extended_Index'First;
65 subtype Capacity_Range is
66 Count_Type range 0 .. Count_Type (Index_Type'Last - Index_Type'First + 1);
68 type Vector (Capacity : Capacity_Range) is limited private with
69 Default_Initial_Condition;
71 function Empty_Vector return Vector;
73 function "=" (Left, Right : Vector) return Boolean with
77 (New_Item : Element_Type;
78 Length : Capacity_Range) return Vector
82 function Capacity (Container : Vector) return Capacity_Range with
84 Post => Capacity'Result >= Container.Capacity;
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 (Capacity = 0 or 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;
137 then Length (Container) + Length (New_Item) <=
141 (Container : in out Vector;
142 New_Item : Element_Type)
146 then Length (Container) < Container.Capacity);
148 procedure Delete_Last
149 (Container : in out Vector)
153 procedure Reverse_Elements (Container : in out Vector) with
156 procedure Swap (Container : in out Vector; I, J : Index_Type) with
158 Pre => I in First_Index (Container) .. Last_Index (Container)
159 and then J in First_Index (Container) .. Last_Index (Container);
161 function First_Index (Container : Vector) return Index_Type with
164 function First_Element (Container : Vector) return Element_Type with
166 Pre => not Is_Empty (Container);
168 function Last_Index (Container : Vector) return Extended_Index with
171 function Last_Element (Container : Vector) return Element_Type with
173 Pre => not Is_Empty (Container);
178 Index : Index_Type := Index_Type'First) return Extended_Index
182 function Reverse_Find_Index
185 Index : Index_Type := Index_Type'Last) return Extended_Index
191 Item : Element_Type) return Boolean
196 (Container : Vector; Position : Extended_Index) return Boolean with
200 with function "<" (Left, Right : Element_Type) return Boolean is <>;
201 package Generic_Sorting is
203 function Is_Sorted (Container : Vector) return Boolean with
206 procedure Sort (Container : in out Vector) with
211 function First_To_Previous
213 Current : Index_Type) return Vector
218 function Current_To_Last
220 Current : Index_Type) return Vector
226 pragma SPARK_Mode (Off);
228 pragma Inline (First_Index);
229 pragma Inline (Last_Index);
230 pragma Inline (Element);
231 pragma Inline (First_Element);
232 pragma Inline (Last_Element);
233 pragma Inline (Replace_Element);
234 pragma Inline (Contains);
236 -- The implementation method is to instantiate Bounded_Holders to get a
237 -- definite type for Element_Type, and then use that Holder type to
238 -- instantiate Formal_Vectors. All the operations are just wrappers.
240 package Holders is new Bounded_Holders
241 (Element_Type, Max_Size_In_Storage_Elements, "=");
244 package Def is new Formal_Vectors (Index_Type, Holder, "=", Bounded);
247 -- ????Assert that Def subtypes have the same range
249 type Vector (Capacity : Capacity_Range) is limited record
250 V : Def.Vector (Capacity);
253 function Empty_Vector return Vector is
254 ((Capacity => 0, V => Def.Empty_Vector));
256 end Ada.Containers.Formal_Indefinite_Vectors;