--  Verada predefined library: bounded vectors of at most Capacity elements, indexed from 1,
--  with contracts that say what each operation does to every element.
with Verada.Containers.Functional.Sequences;

generic
   type Element_Type is private;
   Capacity : Positive;
package Verada.Containers.Formal.Vectors is
   subtype Extended_Index is Natural range 0 .. Capacity;
   subtype Index_Type is Positive range 1 .. Capacity;

   type Vector is private
     with Default_Initial_Condition => Length (Vector) = 0,
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element),
          Aggregate => (Empty => Empty_Vector, Add_Unnamed => Append);

   function Length (Container : Vector) return Extended_Index
     with Global => null;

   --  the vector of no elements, from which `[A, B, C]` is built by Append
   function Empty_Vector return Vector
     with Global => null,
          Post => Length (Empty_Vector'Result) = 0;

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

   function Is_Full (Container : Vector) return Boolean is (Length (Container) = Capacity);

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

   --  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) = Length (Container)
                  and then (for all I in 1 .. Length (Container) => Models.Get (Model'Result, I) = Element (Container, I));

   function First_Element (Container : Vector) return Element_Type
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => First_Element'Result = Element (Container, 1);

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

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

   procedure Append (Container : in out Vector; New_Item : Element_Type)
     with Global => null,
          Pre => not Is_Full (Container),
          Post => Length (Container) = Length (Container)'Old + 1
                  and then Element (Container, Length (Container)) = New_Item
                  and then (for all I in 1 .. Length (Container'Old) => Element (Container, I) = Element (Container'Old, I));

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

   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 1 .. Length (Container) => Element (Container, I) = Element (Container'Old, I));

   --  the first index of Item, or 0 when no element is Item
   function Find_Index (Container : Vector; Item : Element_Type) return Extended_Index
     with Global => null,
          Post => (if Find_Index'Result = 0
                   then (for all I in 1 .. Length (Container) => Element (Container, I) /= Item)
                   else Find_Index'Result <= Length (Container)
                        and then Element (Container, Find_Index'Result) = Item
                        and then (for all I in 1 .. Find_Index'Result - 1 => Element (Container, I) /= Item));

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

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

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

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

   function Iter_Element (Container : Vector; Position : Natural) return Element_Type is
     (Element (Container, Position))
     with Pre => Iter_Has_Element (Container, Position);
private
   type Element_Array is array (Index_Type) of Element_Type;

   type Vector is record
      Last : Extended_Index := 0;
      Items : Element_Array;
   end record;
   --  the items past Last start unset: Element's precondition keeps every read below Last
   pragma Annotate (Verada, Intentional, "not fully initialized", "items past Last are never read");

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

   function Element (Container : Vector; Index : Index_Type) return Element_Type is (Container.Items (Index));
end Verada.Containers.Formal.Vectors;
