DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Ada.Containers.Bounded_Vectors

Verada predefined library: Ada.Containers.Bounded_Vectors (RM A.18.19), vectors of at most Capacity elements indexed by Index_Type from its first value, in the design of Verada.Containers.Formal.Vectors: the elements are inside the object, each operation's contract states what it does to every element, and the ghost Model is the elements in order. Where Ada raises Constraint_Error or Capacity_Error a precondition requires what avoids it. Elements are reached by index; there are no cursors (a cursor would name its vector by access, which the proof does not follow). Contracts compare elements with Models.Equal_Elements, the element type's own equality; the generic "=" is the one Find_Index, Contains and the vectors' "=" use, as in Ada.

generic
   type Index_Type is range <>;
   type Element_Type is private;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;

Extended_Index

subtype Extended_Index is Index_Type'Base range Index_Type'First - 1 .. Index_Type'Last;

No_Index

No_Index : constant Extended_Index := Extended_Index'First;

Index_Count

Index_Count : constant Long_Long_Long_Integer :=
  Long_Long_Long_Integer'Max (0, Long_Long_Long_Integer (Index_Type'Last) - Long_Long_Long_Integer (Index_Type'First) + 1);

how many indices Index_Type has: the most elements a vector holds, whatever its capacity

Vector

type Vector (Capacity : Count_Type) is private
  with Default_Initial_Condition => Length (Vector) = 0,
       Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);

Length

function Length (Container : Vector) return Count_Type
  with Global => null,
       Post => Length'Result <= Container.Capacity and then Long_Long_Long_Integer (Length'Result) <= Index_Count;

Empty

function Empty (Capacity : Count_Type := 10) return Vector
  with Global => null,
       Post => Empty'Result.Capacity = Capacity and then Length (Empty'Result) = 0;

the vector of no elements, with room for Capacity

Is_Empty

function Is_Empty (Container : Vector) return Boolean is (Length (Container) = 0);

First_Index

function First_Index (Container : Vector) return Index_Type is (Index_Type'First);

Last_Index

function Last_Index (Container : Vector) return Extended_Index
  with Global => null,
       Post => Long_Long_Long_Integer (Last_Index'Result)
               = Long_Long_Long_Integer (Index_Type'First) - 1 + Long_Long_Long_Integer (Length (Container));

the index of the last element, No_Index when there is none

Element

function Element (Container : Vector; Index : Index_Type) return Element_Type
  with Global => null,
       Pre => Index <= Last_Index (Container);

Position

function Position (Index : Index_Type) return Long_Long_Long_Integer is
  (Long_Long_Long_Integer (Index) - Long_Long_Long_Integer (Index_Type'First) + 1);

the place of an index's element in the model, from 1

Models

package Models is new Verada.Containers.Functional.Sequences (Element_Type);

the functional model: the elements in order, as a sequence

Model

function Model (Container : Vector) return Models.Sequence
  with Ghost,
       Global => null,
       Post => Models.Length (Model'Result) = Natural (Length (Container))
               and then (for all I in Index_Type'First .. Last_Index (Container) =>
                           Models.Equal_Elements (Models.Get (Model'Result, Positive (Position (I))), Element (Container, I)));

Same_Elements

function Same_Elements (Left, Right : Vector) return Boolean is
  (Length (Left) = Length (Right)
   and then (for all I in Index_Type'First .. Last_Index (Left) => Models.Equal_Elements (Element (Left, I), Element (Right, I))))
  with Ghost;

Left and Right hold the same elements in the same order (their capacities may differ)

First_Element

function First_Element (Container : Vector) return Element_Type
  with Global => null,
       Pre => not Is_Empty (Container),
       Post => Models.Equal_Elements (First_Element'Result, Element (Container, Index_Type'First));

Last_Element

function Last_Element (Container : Vector) return Element_Type
  with Global => null,
       Pre => not Is_Empty (Container),
       Post => Models.Equal_Elements (Last_Element'Result, Element (Container, Last_Index (Container)));

Clear

procedure Clear (Container : in out Vector)
  with Global => null,
       Post => Length (Container) = 0;

Replace_Element

procedure Replace_Element (Container : in out Vector; Index : Index_Type; New_Item : Element_Type)
  with Global => null,
       Pre => Index <= Last_Index (Container),
       Post => Length (Container) = Length (Container)'Old
               and then Models.Equal_Elements (Element (Container, Index), New_Item)
               and then (for all I in Index_Type'First .. Last_Index (Container) =>
                           (if I /= Index then Models.Equal_Elements (Element (Container, I), Element (Container'Old, I))));

Append

procedure Append (Container : in out Vector; New_Item : Element_Type)
  with Global => null,
       Pre => Length (Container) < Container.Capacity and then Last_Index (Container) < Index_Type'Last,
       Post => Length (Container) = Length (Container)'Old + 1
               and then Models.Equal_Elements (Element (Container, Last_Index (Container)), New_Item)
               and then (for all I in Index_Type'First .. Last_Index (Container'Old) =>
                           Models.Equal_Elements (Element (Container, I), Element (Container'Old, I)));

Insert

procedure Insert (Container : in out Vector; Before : Extended_Index; New_Item : Element_Type)
  with Global => null,
       Pre => Length (Container) < Container.Capacity and then Last_Index (Container) < Index_Type'Last
              and then Before >= Index_Type'First and then Before <= Last_Index (Container) + 1,
       Post => Length (Container) = Length (Container)'Old + 1
               and then Models.Equal_Elements (Element (Container, Before), New_Item)
               and then (for all I in Index_Type'First .. Last_Index (Container'Old) =>
                           (if I < Before then Models.Equal_Elements (Element (Container, I), Element (Container'Old, I))
                            else Models.Equal_Elements (Element (Container, I + 1), Element (Container'Old, I))));

New_Item before the element at Before (at the end when Before is Last_Index + 1)

Prepend

procedure Prepend (Container : in out Vector; New_Item : Element_Type)
  with Global => null,
       Pre => Length (Container) < Container.Capacity and then Last_Index (Container) < Index_Type'Last,
       Post => Length (Container) = Length (Container)'Old + 1
               and then Models.Equal_Elements (Element (Container, Index_Type'First), New_Item)
               and then (for all I in Index_Type'First .. Last_Index (Container'Old) =>
                           Models.Equal_Elements (Element (Container, I + 1), Element (Container'Old, I)));

Delete

procedure Delete (Container : in out Vector; Index : Extended_Index)
  with Global => null,
       Pre => Index >= Index_Type'First and then Index <= Last_Index (Container),
       Post => Length (Container) = Length (Container)'Old - 1
               and then (for all I in Index_Type'First .. Last_Index (Container) =>
                           (if I < Index then Models.Equal_Elements (Element (Container, I), Element (Container'Old, I))
                            else Models.Equal_Elements (Element (Container, I), Element (Container'Old, I + 1))));

the element at Index goes; those after it move down one

Delete_First

procedure Delete_First (Container : in out Vector)
  with Global => null,
       Pre => not Is_Empty (Container),
       Post => Length (Container) = Length (Container)'Old - 1
               and then (for all I in Index_Type'First .. Last_Index (Container) =>
                           Models.Equal_Elements (Element (Container, I), Element (Container'Old, I + 1)));

Delete_Last

procedure Delete_Last (Container : in out Vector)
  with Global => null,
       Pre => not Is_Empty (Container),
       Post => Length (Container) = Length (Container)'Old - 1
               and then (for all I in Index_Type'First .. Last_Index (Container) =>
                           Models.Equal_Elements (Element (Container, I), Element (Container'Old, I)));

Swap

procedure Swap (Container : in out Vector; I, J : Index_Type)
  with Global => null,
       Pre => I <= Last_Index (Container) and then J <= Last_Index (Container),
       Post => Length (Container) = Length (Container)'Old
               and then Models.Equal_Elements (Element (Container, I), Element (Container'Old, J))
               and then Models.Equal_Elements (Element (Container, J), Element (Container'Old, I))
               and then (for all K in Index_Type'First .. Last_Index (Container) =>
                           (if K /= I and then K /= J then Models.Equal_Elements (Element (Container, K), Element (Container'Old, K))));

the elements at I and J change places

Reverse_Elements

procedure Reverse_Elements (Container : in out Vector)
  with Global => null,
       Post => Length (Container) = Length (Container)'Old
               and then (for all I in Index_Type'First .. Last_Index (Container) =>
                           Models.Equal_Elements
                             (Element (Container, I),
                              Element (Container'Old, Index_Type'First + (Last_Index (Container) - I))));

the elements in the opposite order

Find_Index

function Find_Index (Container : Vector; Item : Element_Type; Index : Index_Type := Index_Type'First) return Extended_Index
  with Global => null,
       Post => (if Find_Index'Result = No_Index
                then (for all I in Index .. Last_Index (Container) => not (Element (Container, I) = Item))
                else Find_Index'Result >= Index and then Find_Index'Result <= Last_Index (Container)
                     and then Element (Container, Find_Index'Result) = Item
                     and then (for all I in Index .. Find_Index'Result - 1 => not (Element (Container, I) = Item)));

the first index from Index on whose element is Item ("="), or No_Index

Reverse_Find_Index

function Reverse_Find_Index (Container : Vector; Item : Element_Type; Index : Index_Type := Index_Type'Last) return Extended_Index
  with Global => null,
       Post => (if Reverse_Find_Index'Result = No_Index
                then (for all I in Index_Type'First .. Extended_Index'Min (Index, Last_Index (Container)) => not (Element (Container, I) = Item))
                else Reverse_Find_Index'Result <= Index and then Reverse_Find_Index'Result <= Last_Index (Container)
                     and then Element (Container, Reverse_Find_Index'Result) = Item
                     and then (for all I in Index_Type'First .. Extended_Index'Min (Index, Last_Index (Container)) =>
                                 (if I > Reverse_Find_Index'Result then not (Element (Container, I) = Item))));

the last index up to Index whose element is Item ("="), or No_Index

Contains

function Contains (Container : Vector; Item : Element_Type) return Boolean is
  (Find_Index (Container, Item) /= No_Index);
function "=" (Left, Right : Vector) return Boolean
  with Global => null,
       Post => "="'Result = (Length (Left) = Length (Right)
                             and then (for all I in Index_Type'First .. Last_Index (Left) => Element (Left, I) = Element (Right, I)));

the same length and elements equal ("=") index by index

Assign

procedure Assign (Target : in out Vector; Source : Vector)
  with Global => null,
       Pre => Length (Source) <= Target.Capacity,
       Post => Same_Elements (Target, Source);

Target holds Source's elements

Copy

function Copy (Source : Vector; Capacity : Count_Type := 0) return Vector
  with Global => null,
       Pre => Capacity = 0 or else Capacity >= Length (Source),
       Post => Copy'Result.Capacity = (if Capacity = 0 then Length (Source) else Capacity)
               and then Same_Elements (Copy'Result, Source);

a vector of Source's elements with room for Capacity, or for Source's length when Capacity is 0

To_Vector

function To_Vector (New_Item : Element_Type; Length : Count_Type) return Vector
  with Global => null,
       Pre => Long_Long_Long_Integer (Length) <= Index_Count,
       Post => To_Vector'Result.Capacity = Length
               and then Bounded_Vectors.Length (To_Vector'Result) = Length
               and then (for all I in Index_Type'First .. Last_Index (To_Vector'Result) =>
                           Models.Equal_Elements (Element (To_Vector'Result, I), New_Item));

Length copies of New_Item

Iter_First

function Iter_First (Container : Vector) return Count_Type is (1);

for E of V and for all E of V =>...: positions 1 .. Length (V)

Iter_Next

function Iter_Next (Container : Vector; Position : Count_Type) return Count_Type is
  (if Position < Count_Type'Last then Position + 1 else Position);

Iter_Has_Element

function Iter_Has_Element (Container : Vector; Position : Count_Type) return Boolean is
  (Position in 1 .. Length (Container));

Iter_Element

function Iter_Element (Container : Vector; Position : Count_Type) return Element_Type
  with Global => null,
       Pre => Iter_Has_Element (Container, Position);