--  Verada predefined library: Ada.Containers.Bounded_Doubly_Linked_Lists (RM A.18.20), doubly
--  linked lists of at most Capacity elements, in the design of
--  Verada.Containers.Formal.Doubly_Linked_Lists: no storage allocated after the list is made,
--  cursors that stay valid while other elements come and go, and contracts over the list's
--  model (its elements in order, a functional sequence) and the place of every cursor in it.
--  A cursor is a node's number; the operations that read through one take the list too
--  (Ada's take the cursor alone, which names its list by access, a second name the proof
--  does not follow). Where Ada raises Constraint_Error, Program_Error or Capacity_Error a
--  precondition requires what avoids it. Contracts compare elements with
--  Models.Equal_Elements, the element type's own equality; the generic "=" is the one Find,
--  Contains and the lists' "=" use, as in Ada.
with Verada.Containers.Functional.Sequences;

generic
   type Element_Type is private;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ada.Containers.Bounded_Doubly_Linked_Lists is
   --  a place in a list, from 1
   subtype Positive_Count is Count_Type range 1 .. Count_Type'Last;

   type List (Capacity : Count_Type) is private
     with Default_Initial_Condition => Length (List) = 0;

   --  a node of a list; No_Element designates none
   type Cursor is record
      Node : Count_Type := 0;
   end record;

   No_Element : constant Cursor := (Node => 0);

   function Length (Container : List) return Count_Type
     with Global => null;

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

   --  the functional model: the elements in order, as a sequence
   package Models is new Verada.Containers.Functional.Sequences (Element_Type);

   --  (proof only) a model's length and its element at a place, counted as the list is
   function Model_Length (S : Models.Sequence) return Count_Type is (Count_Type (Models.Length (S)))
     with Ghost;

   function Model_Get (S : Models.Sequence; I : Count_Type) return Element_Type is (Models.Get (S, Natural (I)))
     with Ghost,
          Pre => I in 1 .. Model_Length (S);

   function Model (Container : List) return Models.Sequence
     with Ghost,
          Global => null,
          Post => Model_Length (Model'Result) = Length (Container);

   --  the node of the list's element at a place, from 1 (proof only)
   function Node_At (Container : List; Index : Positive_Count) return Count_Type
     with Ghost,
          Global => null,
          Pre => Index <= Length (Container);

   --  the place of a node's element in the list, from 1, or 0 when the list does not use it
   function Node_Index (Container : List; Node : Count_Type) return Count_Type
     with Global => null,
          Post => Node_Index'Result <= Length (Container)
                  and then (if Node = 0 then Node_Index'Result = 0)
                  and then (if Node_Index'Result /= 0 then Node_At (Container, Node_Index'Result) = Node)
                  and then (for all I in 1 .. Length (Container) => (Node_At (Container, I) = Node) = (I = Node_Index'Result));

   --  the place of a cursor's element in the list, from 1, or 0 when it designates none
   function Index_Of (Container : List; Position : Cursor) return Count_Type is
     (Node_Index (Container, Position.Node));

   function Has_Element (Container : List; Position : Cursor) return Boolean is
     (Index_Of (Container, Position) /= 0);

   function Element (Container : List; Position : Cursor) return Element_Type
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => Models.Equal_Elements (Element'Result, Model_Get (Model (Container), Index_Of (Container, Position)));

   --  every cursor keeps its place
   function Same_Places (Left, Right : List) return Boolean is
     (for all N in 1 .. Left.Capacity => Node_Index (Left, N) = Node_Index (Right, N))
     with Ghost;

   --  the cursors of Old keep their place in New, those from At on one further
   function Places_Shifted_Up (Old, New_List : List; At_Index : Positive_Count) return Boolean is
     (for all N in 1 .. Old.Capacity =>
        (if Node_Index (Old, N) /= 0 then
           Node_Index (New_List, N) - (if Node_Index (Old, N) < At_Index then 0 else 1) = Node_Index (Old, N)))
     with Ghost;

   --  the cursors of Old other than the one at At_Index keep their place in New, those after
   --  it one nearer
   function Places_Shifted_Down (Old, New_List : List; At_Index : Positive_Count) return Boolean is
     (for all N in 1 .. Old.Capacity =>
        (if Node_Index (Old, N) /= 0 and then Node_Index (Old, N) /= At_Index then
           Node_Index (New_List, N)
           = (if Node_Index (Old, N) < At_Index then Node_Index (Old, N) else Node_Index (Old, N) - 1)))
     with Ghost;

   --  New is Old with Item at At_Index
   function Inserted (Old, New_Model : Models.Sequence; At_Index : Positive_Count; Item : Element_Type) return Boolean is
     (Model_Length (Old) < Count_Type'Last
      and then At_Index <= Model_Length (Old) + 1
      and then Model_Length (New_Model) = Model_Length (Old) + 1
      and then Models.Equal_Elements (Model_Get (New_Model, At_Index), Item)
      and then (for all I in 1 .. At_Index - 1 => Models.Equal_Elements (Model_Get (New_Model, I), Model_Get (Old, I)))
      and then (for all I in At_Index .. Model_Length (Old) => Models.Equal_Elements (Model_Get (New_Model, I + 1), Model_Get (Old, I))))
     with Ghost;

   --  New is Old without the element at At_Index
   function Removed (Old, New_Model : Models.Sequence; At_Index : Positive_Count) return Boolean is
     (At_Index <= Model_Length (Old)
      and then Model_Length (New_Model) = Model_Length (Old) - 1
      and then (for all I in 1 .. At_Index - 1 => Models.Equal_Elements (Model_Get (New_Model, I), Model_Get (Old, I)))
      and then (for all I in At_Index .. Model_Length (Old) => (if I > At_Index then Models.Equal_Elements (Model_Get (New_Model, I - 1), Model_Get (Old, I)))))
     with Ghost;

   function First (Container : List) return Cursor
     with Global => null,
          Post => (if Is_Empty (Container) then First'Result = No_Element else Index_Of (Container, First'Result) = 1);

   function Last (Container : List) return Cursor
     with Global => null,
          Post => (if Is_Empty (Container) then Last'Result = No_Element else Index_Of (Container, Last'Result) = Length (Container));

   function Next (Container : List; Position : Cursor) return Cursor
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => (if Index_Of (Container, Position) = Length (Container) then Next'Result = No_Element
                   else Index_Of (Container, Next'Result) = Index_Of (Container, Position) + 1);

   function Previous (Container : List; Position : Cursor) return Cursor
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => (if Index_Of (Container, Position) = 1 then Previous'Result = No_Element
                   else Index_Of (Container, Previous'Result) = Index_Of (Container, Position) - 1);

   function First_Element (Container : List) return Element_Type
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Models.Equal_Elements (First_Element'Result, Model_Get (Model (Container), 1));

   function Last_Element (Container : List) return Element_Type
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Models.Equal_Elements (Last_Element'Result, Model_Get (Model (Container), Length (Container)));

   --  the first cursor of Item, or No_Element
   function Find (Container : List; Item : Element_Type) return Cursor
     with Global => null,
          Post => (if Find'Result = No_Element then
                     (for all I in 1 .. Length (Container) => not (Model_Get (Model (Container), I) = Item))
                   else Has_Element (Container, Find'Result) and then Element (Container, Find'Result) = Item);

   function Contains (Container : List; Item : Element_Type) return Boolean is
     (Find (Container, Item) /= No_Element);

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

   --  New_Item before Before (at the end when Before is No_Element); Position designates it
   procedure Insert (Container : in out List; Before : Cursor; New_Item : Element_Type; Position : out Cursor)
     with Global => null,
          Pre => Length (Container) < Container.Capacity and then (Before = No_Element or else Has_Element (Container, Before)),
          Post => Inserted (Model (Container'Old), Model (Container),
                            (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)), New_Item)
                  and then Index_Of (Container, Position) = (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before))
                  and then Places_Shifted_Up (Container'Old, Container, Index_Of (Container, Position));

   --  New_Item before Before (at the end when Before is No_Element)
   procedure Insert (Container : in out List; Before : Cursor; New_Item : Element_Type)
     with Global => null,
          Pre => Length (Container) < Container.Capacity and then (Before = No_Element or else Has_Element (Container, Before)),
          Post => Inserted (Model (Container'Old), Model (Container),
                            (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)), New_Item)
                  and then Places_Shifted_Up (Container'Old, Container,
                                              (if Before = No_Element then Length (Container'Old) + 1 else Index_Of (Container'Old, Before)));

   procedure Append (Container : in out List; New_Item : Element_Type)
     with Global => null,
          Pre => Length (Container) < Container.Capacity,
          Post => Inserted (Model (Container'Old), Model (Container), Length (Container), New_Item)
                  and then Places_Shifted_Up (Container'Old, Container, Length (Container));

   procedure Prepend (Container : in out List; New_Item : Element_Type)
     with Global => null,
          Pre => Length (Container) < Container.Capacity,
          Post => Inserted (Model (Container'Old), Model (Container), 1, New_Item)
                  and then Places_Shifted_Up (Container'Old, Container, 1);

   procedure Replace_Element (Container : in out List; Position : Cursor; New_Item : Element_Type)
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => Length (Container) = Length (Container'Old)
                  and then Models.Equal_Elements (Element (Container, Position), New_Item)
                  and then Same_Places (Container'Old, Container)
                  and then (for all I in 1 .. Length (Container) =>
                              (if I /= Index_Of (Container, Position) then Models.Equal_Elements (Model_Get (Model (Container), I), Model_Get (Model (Container'Old), I))));

   --  the element Position designates is gone, and so is Position
   procedure Delete (Container : in out List; Position : in out Cursor)
     with Global => null,
          Pre => Has_Element (Container, Position),
          Post => Removed (Model (Container'Old), Model (Container), Index_Of (Container'Old, Position'Old))
                  and then Position = No_Element
                  and then not Has_Element (Container, Position'Old)
                  and then Places_Shifted_Down (Container'Old, Container, Index_Of (Container'Old, Position'Old));

   procedure Delete_First (Container : in out List)
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Removed (Model (Container'Old), Model (Container), 1)
                  and then Places_Shifted_Down (Container'Old, Container, 1);

   procedure Delete_Last (Container : in out List)
     with Global => null,
          Pre => not Is_Empty (Container),
          Post => Removed (Model (Container'Old), Model (Container), Length (Container'Old))
                  and then Places_Shifted_Down (Container'Old, Container, Length (Container'Old));
private
   type Node_Type is record
      Element : Element_Type;
      Next, Previous : Count_Type := 0;
      In_Use : Boolean := False;
   end record;

   type Node_Array is array (Count_Type range <>) of Node_Type;

   --  sequences of nodes (proof only)
   package Node_Sequences is new Verada.Containers.Functional.Sequences (Count_Type);

   function Node_Length (S : Node_Sequences.Sequence) return Count_Type is (Count_Type (Node_Sequences.Length (S)));

   function Node_Get (S : Node_Sequences.Sequence; I : Count_Type) return Count_Type is (Node_Sequences.Get (S, Natural (I)))
     with Pre => I in 1 .. Node_Length (S);

   --  every node, 1 to Capacity
   function All_Nodes (Capacity : Count_Type) return Node_Sequences.Sequence
     with Ghost,
          Global => null,
          Post => Node_Length (All_Nodes'Result) = Capacity
                  and then (for all I in 1 .. Capacity => Node_Get (All_Nodes'Result, I) = I);

   --  the elements in order from Head by Next (back from Tail by Previous); Order lists their
   --  nodes and Values their elements (the model), Spare the nodes not in use; a new element
   --  takes the first node not in use
   type List (Capacity : Count_Type) is record
      Head, Tail : Count_Type := 0;
      Count : Count_Type := 0;
      Nodes : Node_Array (1 .. Capacity);
      Order : Node_Sequences.Sequence := Node_Sequences.Empty_Sequence;
      Spare : Node_Sequences.Sequence := All_Nodes (Capacity);
      Values : Models.Sequence := Models.Empty_Sequence;
   end record
     with Type_Invariant => Well_Formed (List);
   --  an unused node's element is unset: Element's precondition keeps every read to used ones
   pragma Annotate (Verada, Intentional, "not fully initialized", "unused nodes are never read");

   --  Order's nodes in use, linked each way, all distinct; Spare's not in use, all distinct;
   --  every node in one of them
   function Well_Formed (Container : List) return Boolean is
     (Node_Length (Container.Order) = Container.Count
      and then Count_Type (Models.Length (Container.Values)) = Container.Count
      and then Node_Length (Container.Spare) = Container.Capacity - Container.Count
      and then (if Container.Count = 0 then Container.Head = 0 and then Container.Tail = 0
                else Container.Head = Node_Get (Container.Order, 1)
                     and then Container.Tail = Node_Get (Container.Order, Container.Count))
      and then (for all I in 1 .. Container.Count =>
                  Node_Get (Container.Order, I) in 1 .. Container.Capacity
                  and then Container.Nodes (Node_Get (Container.Order, I)).In_Use
                  and then Models.Equal_Elements (Container.Nodes (Node_Get (Container.Order, I)).Element, Models.Get (Container.Values, Natural (I)))
                  and then Container.Nodes (Node_Get (Container.Order, I)).Next
                           = (if I = Container.Count then 0 else Node_Get (Container.Order, I + 1))
                  and then Container.Nodes (Node_Get (Container.Order, I)).Previous
                           = (if I = 1 then 0 else Node_Get (Container.Order, I - 1)))
      and then (for all I in 1 .. Container.Count =>
                  (for all J in 1 .. I - 1 => Node_Get (Container.Order, J) /= Node_Get (Container.Order, I)))
      and then (for all I in 1 .. Node_Length (Container.Spare) =>
                  Node_Get (Container.Spare, I) in 1 .. Container.Capacity
                  and then not Container.Nodes (Node_Get (Container.Spare, I)).In_Use)
      and then (for all I in 1 .. Node_Length (Container.Spare) =>
                  (for all J in 1 .. I - 1 => Node_Get (Container.Spare, J) /= Node_Get (Container.Spare, I)))
      and then (for all N in 1 .. Container.Capacity =>
                  (if Container.Nodes (N).In_Use then (for some I in 1 .. Container.Count => Node_Get (Container.Order, I) = N)
                   else (for some I in 1 .. Node_Length (Container.Spare) => Node_Get (Container.Spare, I) = N))))
     with Ghost;

   function Length (Container : List) return Count_Type is (Container.Count);
   function Model (Container : List) return Models.Sequence is (Container.Values);
   function Node_At (Container : List; Index : Positive_Count) return Count_Type is (Node_Get (Container.Order, Index));
end Ada.Containers.Bounded_Doubly_Linked_Lists;
