1 ------------------------------------------------------------------------------
3 -- GNAT LIBRARY COMPONENTS --
5 -- A D A . C O N T A I N E R S . F O R M A L _ V E C T O R S --
9 -- Copyright (C) 2004-2013, 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 -- This spec is derived from package Ada.Containers.Bounded_Vectors in the Ada
33 -- 2012 RM. The modifications are meant to facilitate formal proofs by making
34 -- it easier to express properties, and by making the specification of this
35 -- unit compatible with SPARK 2014. Note that the API of this unit may be
36 -- subject to incompatible changes as SPARK 2014 evolves.
38 -- The modifications are:
40 -- A parameter for the container is added to every function reading the
41 -- content of a container: Element, Next, Query_Element, Previous, Iterate,
42 -- Has_Element, Reverse_Iterate. This change is motivated by the need
43 -- to have cursors which are valid on different containers (typically a
44 -- container C and its previous version C'Old) for expressing properties,
45 -- which is not possible if cursors encapsulate an access to the underlying
48 -- There are three new functions:
50 -- function Strict_Equal (Left, Right : Vector) return Boolean;
51 -- function First_To_Previous (Container : Vector; Current : Cursor)
53 -- function Current_To_Last (Container : Vector; Current : Cursor)
56 -- See detailed specifications for these subprograms
62 type Index_Type
is range <>;
63 type Element_Type
is private;
65 with function "=" (Left
, Right
: Element_Type
) return Boolean is <>;
67 package Ada
.Containers
.Formal_Vectors
is
68 pragma Annotate
(GNATprove
, External_Axiomatization
);
71 subtype Extended_Index
is Index_Type
'Base
72 range Index_Type
'First - 1 ..
73 Index_Type
'Min (Index_Type
'Base'Last - 1, Index_Type'Last) + 1;
75 No_Index : constant Extended_Index := Extended_Index'First;
77 subtype Capacity_Range is
78 Count_Type range 0 .. Count_Type (Index_Type'Last - Index_Type'First + 1);
80 type Vector (Capacity : Capacity_Range) is private with
81 Iterable => (First => First,
83 Has_Element => Has_Element,
86 type Cursor is private;
87 pragma Preelaborable_Initialization (Cursor);
89 Empty_Vector : constant Vector;
91 No_Element : constant Cursor;
93 function "=" (Left, Right : Vector) return Boolean with
97 (New_Item : Element_Type;
98 Length : Count_Type) return Vector
102 function "&" (Left, Right : Vector) return Vector with
104 Pre => Capacity_Range'Last - Length (Left) >= Length (Right);
106 function "&" (Left : Vector; Right : Element_Type) return Vector with
108 Pre => Length (Left) < Capacity_Range'Last;
110 function "&" (Left : Element_Type; Right : Vector) return Vector with
112 Pre => Length (Right) < Capacity_Range'Last;
114 function "&" (Left, Right : Element_Type) return Vector with
116 Pre => Capacity_Range'Last >= 2;
118 function Capacity (Container : Vector) return Count_Type with
121 procedure Reserve_Capacity
122 (Container : in out Vector;
123 Capacity : Count_Type)
126 Pre => Capacity <= Container.Capacity;
128 function Length (Container : Vector) return Count_Type with
132 (Container : in out Vector;
133 New_Length : Count_Type)
136 Pre => New_Length <= Length (Container);
138 function Is_Empty (Container : Vector) return Boolean with
141 procedure Clear (Container : in out Vector) with
144 procedure Assign (Target : in out Vector; Source : Vector) with
146 Pre => Length (Source) <= Target.Capacity;
150 Capacity : Count_Type := 0) return Vector
153 Pre => Length (Source) <= Capacity and then Capacity in Capacity_Range;
157 Index : Extended_Index) return Cursor
161 function To_Index (Position : Cursor) return Extended_Index with
166 Index : Index_Type) return Element_Type
169 Pre => First_Index (Container) <= Index
170 and then Index <= Last_Index (Container);
174 Position : Cursor) return Element_Type
177 Pre => Has_Element (Container, Position);
179 procedure Replace_Element
180 (Container : in out Vector;
182 New_Item : Element_Type)
185 Pre => First_Index (Container) <= Index
186 and then Index <= Last_Index (Container);
188 procedure Replace_Element
189 (Container : in out Vector;
191 New_Item : Element_Type)
194 Pre => Has_Element (Container, Position);
196 procedure Move (Target : in out Vector; Source : in out Vector) with
198 Pre => Length (Source) <= Target.Capacity;
201 (Container : in out Vector;
202 Before : Extended_Index;
206 Pre => First_Index (Container) <= Before
207 and then Before <= Last_Index (Container) + 1
208 and then Length (Container) < Container.Capacity;
211 (Container : in out Vector;
216 Pre => Length (Container) < Container.Capacity
217 and then (Has_Element (Container, Before)
218 or else Before = No_Element);
221 (Container : in out Vector;
224 Position : out Cursor)
227 Pre => Length (Container) < Container.Capacity
228 and then (Has_Element (Container, Before)
229 or else Before = No_Element);
232 (Container : in out Vector;
233 Before : Extended_Index;
234 New_Item : Element_Type;
235 Count : Count_Type := 1)
238 Pre => First_Index (Container) <= Before
239 and then Before <= Last_Index (Container) + 1
240 and then Length (Container) + Count <= Container.Capacity;
243 (Container : in out Vector;
245 New_Item : Element_Type;
246 Count : Count_Type := 1)
249 Pre => Length (Container) + Count <= Container.Capacity
250 and then (Has_Element (Container, Before)
251 or else Before = No_Element);
254 (Container : in out Vector;
256 New_Item : Element_Type;
257 Position : out Cursor;
258 Count : Count_Type := 1)
261 Pre => Length (Container) + Count <= Container.Capacity
262 and then (Has_Element (Container, Before)
263 or else Before = No_Element);
266 (Container : in out Vector;
270 Pre => Length (Container) < Container.Capacity;
273 (Container : in out Vector;
274 New_Item : Element_Type;
275 Count : Count_Type := 1)
278 Pre => Length (Container) + Count <= Container.Capacity;
281 (Container : in out Vector;
285 Pre => Length (Container) < Container.Capacity;
288 (Container : in out Vector;
289 New_Item : Element_Type;
290 Count : Count_Type := 1)
293 Pre => Length (Container) + Count <= Container.Capacity;
296 (Container : in out Vector;
297 Index : Extended_Index;
298 Count : Count_Type := 1)
301 Pre => First_Index (Container) <= Index
302 and then Index <= Last_Index (Container) + 1;
305 (Container : in out Vector;
306 Position : in out Cursor;
307 Count : Count_Type := 1)
310 Pre => Has_Element (Container, Position);
312 procedure Delete_First
313 (Container : in out Vector;
314 Count : Count_Type := 1)
318 procedure Delete_Last
319 (Container : in out Vector;
320 Count : Count_Type := 1)
324 procedure Reverse_Elements (Container : in out Vector) with
327 procedure Swap (Container : in out Vector; I, J : Index_Type) with
329 Pre => First_Index (Container) <= I
330 and then I <= Last_Index (Container)
331 and then First_Index (Container) <= J
332 and then J <= Last_Index (Container);
334 procedure Swap (Container : in out Vector; I, J : Cursor) with
336 Pre => Has_Element (Container, I) and then Has_Element (Container, J);
338 function First_Index (Container : Vector) return Index_Type with
341 function First (Container : Vector) return Cursor with
344 function First_Element (Container : Vector) return Element_Type with
346 Pre => not Is_Empty (Container);
348 function Last_Index (Container : Vector) return Extended_Index with
351 function Last (Container : Vector) return Cursor with
354 function Last_Element (Container : Vector) return Element_Type with
356 Pre => not Is_Empty (Container);
358 function Next (Container : Vector; Position : Cursor) return Cursor with
360 Pre => Has_Element (Container, Position) or else Position = No_Element;
362 procedure Next (Container : Vector; Position : in out Cursor) with
364 Pre => Has_Element (Container, Position) or else Position = No_Element;
366 function Previous (Container : Vector; Position : Cursor) return Cursor with
368 Pre => Has_Element (Container, Position) or else Position = No_Element;
370 procedure Previous (Container : Vector; Position : in out Cursor) with
372 Pre => Has_Element (Container, Position) or else Position = No_Element;
377 Index : Index_Type := Index_Type'First) return Extended_Index
384 Position : Cursor := No_Element) return Cursor
387 Pre => Has_Element (Container, Position) or else Position = No_Element;
389 function Reverse_Find_Index
392 Index : Index_Type := Index_Type'Last) return Extended_Index
396 function Reverse_Find
399 Position : Cursor := No_Element) return Cursor
402 Pre => Has_Element (Container, Position) or else Position = No_Element;
406 Item : Element_Type) return Boolean
410 function Has_Element (Container : Vector; Position : Cursor) return Boolean
415 with function "<" (Left, Right : Element_Type) return Boolean is <>;
416 package Generic_Sorting is
418 function Is_Sorted (Container : Vector) return Boolean with
421 procedure Sort (Container : in out Vector) with
424 procedure Merge (Target : in out Vector; Source : in out Vector) with
429 function Strict_Equal (Left, Right : Vector) return Boolean with
431 -- Strict_Equal returns True if the containers are physically equal, i.e.
432 -- they are structurally equal (function "=" returns True) and that they
433 -- have the same set of cursors.
435 function First_To_Previous
437 Current : Cursor) return Vector
440 Pre => Has_Element (Container, Current) or else Current = No_Element;
441 function Current_To_Last
443 Current : Cursor) return Vector
446 Pre => Has_Element (Container, Current) or else Current = No_Element;
447 -- First_To_Previous returns a container containing all elements preceding
448 -- Current (excluded) in Container. Current_To_Last returns a container
449 -- containing all elements following Current (included) in Container.
450 -- These two new functions can be used to express invariant properties in
451 -- loops which iterate over containers. First_To_Previous returns the part
452 -- of the container already scanned and Current_To_Last the part not
457 pragma Inline (First_Index);
458 pragma Inline (Last_Index);
459 pragma Inline (Element);
460 pragma Inline (First_Element);
461 pragma Inline (Last_Element);
462 pragma Inline (Replace_Element);
463 pragma Inline (Contains);
464 pragma Inline (Next);
465 pragma Inline (Previous);
467 type Elements_Array is array (Count_Type range <>) of Element_Type;
468 function "=" (L, R : Elements_Array) return Boolean is abstract;
470 type Vector (Capacity : Capacity_Range) is record
471 Elements : Elements_Array (1 .. Capacity);
472 Last : Extended_Index := No_Index;
475 type Cursor is record
476 Valid : Boolean := True;
477 Index : Index_Type := Index_Type'First;
480 Empty_Vector : constant Vector := (Capacity => 0, others => <>);
482 No_Element : constant Cursor := (Valid => False, Index => Index_Type'First);
484 end Ada.Containers.Formal_Vectors;