--  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.
with Verada.Containers.Functional.Sequences;

generic
   type Index_Type is range <>;
   type Element_Type is private;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ada.Containers.Bounded_Vectors is
   subtype Extended_Index is Index_Type'Base range Index_Type'First - 1 .. Index_Type'Last;

   No_Index : constant Extended_Index := Extended_Index'First;

   --  how many indices Index_Type has: the most elements a vector holds, whatever its capacity
   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);

   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);

   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;

   --  the vector of no elements, with room for Capacity
   function Empty (Capacity : Count_Type := 10) return Vector
     with Global => null,
          Post => Empty'Result.Capacity = Capacity and then Length (Empty'Result) = 0;

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

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

   --  the index of the last element, No_Index when there is none
   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));

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

   --  the place of an index's element in the model, from 1
   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 functional model: the elements in order, as a sequence
   package Models is new Verada.Containers.Functional.Sequences (Element_Type);

   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)));

   --  Left and Right hold the same elements in the same order (their capacities may differ)
   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;

   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));

   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)));

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

   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))));

   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)));

   --  New_Item before the element at Before (at the end when Before is Last_Index + 1)
   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))));

   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)));

   --  the element at Index goes; those after it move down one
   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))));

   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)));

   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)));

   --  the elements at I and J change places
   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 in the opposite order
   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 first index from Index on whose element is Item ("="), or No_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 last index up to Index whose element is Item ("="), or No_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))));

   function Contains (Container : Vector; Item : Element_Type) return Boolean is
     (Find_Index (Container, Item) /= No_Index);

   --  the same length and elements equal ("=") index by 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)));

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

   --  a vector of Source's elements with room for Capacity, or for Source's length when
   --  Capacity is 0
   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);

   --  Length copies of New_Item
   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));

   --  `for E of V` and `for all E of V =>...`: positions 1 .. Length (V)
   function Iter_First (Container : Vector) return Count_Type is (1);

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

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

   function Iter_Element (Container : Vector; Position : Count_Type) return Element_Type
     with Global => null,
          Pre => Iter_Has_Element (Container, Position);
private
   type Element_Array is array (Count_Type range <>) of Element_Type;

   type Vector (Capacity : Count_Type) is record
      Last : Count_Type := 0;
      Items : Element_Array (1 .. Capacity);
   end record
     with Type_Invariant => Vector.Last <= Vector.Capacity and then Long_Long_Long_Integer (Vector.Last) <= Index_Count;
   --  the items past Last start unset: the preconditions keep every read below Last
   pragma Annotate (Verada, Intentional, "not fully initialized", "items past Last are never read");

   function Length (Container : Vector) return Count_Type is (Container.Last);

   function Element (Container : Vector; Index : Index_Type) return Element_Type is
     (Container.Items (Count_Type (Position (Index))));

   function Iter_Element (Container : Vector; Position : Count_Type) return Element_Type is (Container.Items (Position));
end Ada.Containers.Bounded_Vectors;
