with Verada.Containers.Formal.Doubly_Linked_Lists;
with Ada.Text_IO; use Ada.Text_IO;

--  A bounded doubly linked list: cursors stay valid while other elements come and go (D100)
procedure Main is
   package Lists is new Verada.Containers.Formal.Doubly_Linked_Lists (Integer, 8);
   use Lists;

   procedure Show (L : List) is
      P : Cursor := First (L);
   begin
      while P /= No_Element loop
         pragma Loop_Invariant (Has_Element (L, P));
         Put (Integer'Image (Element (L, P)));
         P := Next (L, P);
      end loop;
      New_Line;
   end Show;

   procedure Show_Backwards (L : List) is
      P : Cursor := Last (L);
   begin
      while P /= No_Element loop
         pragma Loop_Invariant (Has_Element (L, P));
         Put (Integer'Image (Element (L, P)));
         P := Previous (L, P);
      end loop;
      New_Line;
   end Show_Backwards;

   L : List;
   C, D : Cursor;
begin
   for I in 1 .. 4 loop
      pragma Loop_Invariant (Length (L) = I - 1);
      Append (L, I * 10);
   end loop;
   Prepend (L, 5);
   C := Find (L, 30);
   if C = No_Element then
      return;
   end if;
   Insert (L, C, 25, D);
   Show (L);
   --  D designates 25 still, after C goes
   Delete (L, C);
   pragma Assert (Has_Element (L, D));
   Show (L);
   Replace_Element (L, D, 26);
   Delete_First (L);
   Delete_Last (L);
   Show (L);
   Append (L, 99);
   Insert (L, No_Element, 100, C);
   Show (L);
   Show_Backwards (L);
   Put_Line (Boolean'Image (Contains (L, 26)) & Integer'Image (Length (L)));
end Main;
