--  Verada predefined library: Ada.Containers.Bounded_Ordered_Sets (RM A.18.24), sets of at most
--  Capacity elements, in the design of Verada.Containers.Formal.Ordered_Sets: the elements are
--  inside the object at places 1 .. Length, in increasing order (a new element goes before the
--  first greater one, by "<", which must be a strict order); two elements are equivalent when
--  neither is less than the other; an element is looked up one after another; each
--  operation's contract states what it does at every place. Where Ada raises Constraint_Error
--  or Capacity_Error a precondition requires what avoids it. Elements are reached by value or
--  by place; there are no cursors (a cursor would name its set by access, which the proof does
--  not follow). Contracts compare elements with Models.Equal_Elements, the element type's own
--  equality; "<" and the generic "=" are the ones lookup and the sets' "=" use, as in Ada.
with Verada.Containers.Functional.Sequences;

generic
   type Element_Type is private;
   with function "<" (Left, Right : Element_Type) return Boolean is <>;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ada.Containers.Bounded_Ordered_Sets is
   type Set (Capacity : Count_Type) is private
     with Default_Initial_Condition => Length (Set) = 0,
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);

   --  neither element is less than the other
   function Equivalent_Elements (Left, Right : Element_Type) return Boolean is (not (Left < Right) and then not (Right < Left));

   function Length (Container : Set) return Count_Type
     with Global => null,
          Post => Length'Result <= Container.Capacity;

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

   --  the element at a place, from 1
   function Element_At (Container : Set; Index : Count_Type) return Element_Type
     with Global => null,
          Pre => Index in 1 .. 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 : Set) return Models.Sequence
     with Ghost,
          Global => null,
          Post => Models.Length (Model'Result) = Natural (Length (Container))
                  and then (for all I in 1 .. Length (Container) =>
                              Models.Equal_Elements (Models.Get (Model'Result, Natural (I)), Element_At (Container, I)));

   --  the element at each place of Right is Left's at the same place, or, from Skip on when
   --  Skip is not 0, at the next place (proof only)
   function Same_Elements (Left, Right : Set; Skip : Count_Type) return Boolean is
     (for all I in 1 .. Length (Right) =>
        (if Skip = 0 or else I < Skip
         then I <= Length (Left) and then Models.Equal_Elements (Element_At (Right, I), Element_At (Left, I))
         else I < Length (Left) and then Models.Equal_Elements (Element_At (Right, I), Element_At (Left, I + 1))))
     with Ghost;

   --  the place of the element equivalent to Item, or 0
   function Index_Of (Container : Set; Item : Element_Type) return Count_Type
     with Global => null,
          Post => Index_Of'Result <= Length (Container)
                  and then (if Index_Of'Result = 0
                            then (for all I in 1 .. Length (Container) => not Equivalent_Elements (Element_At (Container, I), Item))
                            else Equivalent_Elements (Element_At (Container, Index_Of'Result), Item)
                                 and then (for all I in 1 .. Index_Of'Result - 1 => not Equivalent_Elements (Element_At (Container, I), Item)));

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

   --  the place a new element takes: before the first greater one, or at the end
   function Insert_Place (Container : Set; Item : Element_Type) return Count_Type
     with Global => null,
          Pre => Length (Container) < Container.Capacity,
          Post => Insert_Place'Result in 1 .. Length (Container) + 1
                  and then (for all I in 1 .. Insert_Place'Result - 1 => not (Item < Element_At (Container, I)))
                  and then (if Insert_Place'Result <= Length (Container) then Item < Element_At (Container, Insert_Place'Result));

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

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

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

   --  New_Item joins the set, in its place
   procedure Insert (Container : in out Set; New_Item : Element_Type)
     with Global => null,
          Pre => not Contains (Container, New_Item) and then Length (Container) < Container.Capacity,
          Post => Length (Container) = Length (Container'Old) + 1
                  and then Models.Equal_Elements (Element_At (Container, Insert_Place (Container'Old, New_Item)), New_Item)
                  and then Same_Elements (Container, Container'Old, Insert_Place (Container'Old, New_Item));

   --  the element equivalent to New_Item is now New_Item
   procedure Replace (Container : in out Set; New_Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, New_Item),
          Post => Length (Container) = Length (Container'Old)
                  and then Models.Equal_Elements (Element_At (Container, Index_Of (Container'Old, New_Item)), New_Item)
                  and then (for all I in 1 .. Length (Container) =>
                              (if I /= Index_Of (Container'Old, New_Item) then
                                 Models.Equal_Elements (Element_At (Container, I), Element_At (Container'Old, I))));

   --  New_Item is in the set, in place of the element equivalent to it or new in its place
   procedure Include (Container : in out Set; New_Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, New_Item) or else Length (Container) < Container.Capacity,
          Post => (if Contains (Container'Old, New_Item) then
                     Length (Container) = Length (Container'Old)
                     and then Models.Equal_Elements (Element_At (Container, Index_Of (Container'Old, New_Item)), New_Item)
                     and then (for all I in 1 .. Length (Container) =>
                                 (if I /= Index_Of (Container'Old, New_Item) then
                                    Models.Equal_Elements (Element_At (Container, I), Element_At (Container'Old, I))))
                   else
                     Length (Container) = Length (Container'Old) + 1
                     and then Models.Equal_Elements (Element_At (Container, Insert_Place (Container'Old, New_Item)), New_Item)
                     and then Same_Elements (Container, Container'Old, Insert_Place (Container'Old, New_Item)));

   --  the element equivalent to Item is gone; those after it move one place nearer
   procedure Delete (Container : in out Set; Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, Item),
          Post => Length (Container) = Length (Container'Old) - 1
                  and then Same_Elements (Container'Old, Container, Index_Of (Container'Old, Item));

   --  the element equivalent to Item is gone, if there was one
   procedure Exclude (Container : in out Set; Item : Element_Type)
     with Global => null,
          Post => (if Contains (Container'Old, Item)
                   then Length (Container) = Length (Container'Old) - 1
                        and then Same_Elements (Container'Old, Container, Index_Of (Container'Old, Item))
                   else Length (Container) = Length (Container'Old)
                        and then Same_Elements (Container'Old, Container, 0));

   --  the same length, and each element of Left equal ("=") to the one equivalent to it in Right
   function "=" (Left, Right : Set) return Boolean
     with Global => null,
          Post => "="'Result = (Length (Left) = Length (Right)
                                and then (for all I in 1 .. Length (Left) =>
                                            Contains (Right, Element_At (Left, I))
                                            and then Element_At (Left, I) = Element_At (Right, Index_Of (Right, Element_At (Left, I)))));

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

   --  a set of Source's elements with room for Capacity, or for Source's length when Capacity
   --  is 0
   function Copy (Source : Set; Capacity : Count_Type := 0) return Set
     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 Length (Copy'Result) = Length (Source)
                  and then Same_Elements (Source, Copy'Result, 0);

   --  `for E of S` and `for all E of S =>...`: the elements, in their order
   function Iter_First (Container : Set) return Count_Type is (1);

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

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

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

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

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

   function Element_At (Container : Set; Index : Count_Type) return Element_Type is (Container.Items (Index));
end Ada.Containers.Bounded_Ordered_Sets;
