--  Verada.Containers.Unbounded.Ordered_Sets: sets of any size, kept in
--  increasing order of "<" (which must be a strict order), on the heap and freed where the set
--  dies (a reclaimed type), the unbounded twin of the bounded ordered sets. The set is limited:
--  copies are made explicitly. Its model is the array of its elements in order, Model, from 1.
--  Only memory limits the size: an operation that adds an element raises Storage_Error when
--  memory cannot hold the result (Verada.Storage).
with Verada.Storage;

generic
   type Element_Type is private;
   with function "<" (Left, Right : Element_Type) return Boolean is <>;
package Verada.Containers.Unbounded.Ordered_Sets is
   type Element_Array is array (Positive range <>) of Element_Type;

   function Has (A : Element_Array; Item : Element_Type) return Boolean is
     (for some I in A'Range => A (I) = Item)
     with Ghost;

   type Set is limited private
     with Default_Initial_Condition => Length (Set) = 0,
          Finalizable => (Finalize => Clear, Relaxed_Finalization => True),
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Element);

   --  (below Length_Bound, as memory holds no more: a `for e in s` loop's cursor always moves
   --  on, and the loop ends)
   function Length (Container : Set) return Natural
     with Global => null,
          Post => Length'Result < Verada.Storage.Length_Bound;

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

   function Model (Container : Set) return Element_Array
     with Global => null,
          Post => Model'Result'Length = Length (Container)
                  and then (Model'Result'Length = 0 or else Model'Result'First = 1);

   function Element (Container : Set; Index : Positive) return Element_Type
     with Global => null,
          Pre => Index <= Length (Container),
          Post => Element'Result = Model (Container) (Index);

   function Contains (Container : Set; Item : Element_Type) return Boolean is
     (for some I in 1 .. Length (Container) => Element (Container, I) = Item);

   --  every element of Container is Item or an element of A
   function Only_Added (Container : Set; A : Element_Array; Item : Element_Type) return Boolean is
     (for all I in 1 .. Length (Container) => Element (Container, I) = Item or else Has (A, Element (Container, I)))
     with Ghost;

   --  every element of Container is an element of A
   function Only_Kept (Container : Set; A : Element_Array) return Boolean is
     (for all I in 1 .. Length (Container) => Has (A, Element (Container, I)))
     with Ghost;

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

   --  Item joins the set, in its place in the order
   procedure Insert (Container : in out Set; Item : Element_Type)
     with Global => null, Always_Terminates,
          Pre => not Contains (Container, Item),
          Post => Length (Container) = Length (Container)'Old + 1
                  and Contains (Container, Item)
                  and (for all X of Model (Container)'Old => Contains (Container, X))
                  and Only_Added (Container, Model (Container)'Old, Item);

   --  Item is in the set afterwards (Python's `s.add(x)`): it joins, unless it is there
   procedure Include (Container : in out Set; Item : Element_Type)
     with Global => null, Always_Terminates,
          Post => Contains (Container, Item)
                  and Length (Container) = Length (Container)'Old + (if Contains (Container, Item)'Old then 0 else 1);

   --  Item is not in the set afterwards (Python's `s.discard(x)`): it leaves, if it is there;
   --  the other elements stay, and no other joins
   procedure Exclude (Container : in out Set; Item : Element_Type)
     with Global => null, Always_Terminates,
          Post => not Contains (Container, Item)
                  and Length (Container) = Length (Container)'Old - (if Contains (Container, Item)'Old then 1 else 0)
                  and (for all X of Model (Container)'Old => (if X /= Item then Contains (Container, X)))
                  and Only_Kept (Container, Model (Container)'Old);

   --  Item, which is in the set, leaves it (Python's `s.remove(x)`)
   procedure Remove (Container : in out Set; Item : Element_Type)
     with Global => null, Always_Terminates,
          Pre => Contains (Container, Item),
          Post => not Contains (Container, Item)
                  and Length (Container) = Length (Container)'Old - 1
                  and (for all X of Model (Container)'Old => (if X /= Item then Contains (Container, X)))
                  and Only_Kept (Container, Model (Container)'Old);

   --  the elements of either (Python's `s | t`)
   function "or" (Left, Right : Set) return Set
     with Global => null,
          Post => Length ("or"'Result) <= Length (Left) + Length (Right)
                  and then (for all I in 1 .. Length ("or"'Result) =>
                              Contains (Left, Element ("or"'Result, I)) or else Contains (Right, Element ("or"'Result, I)))
                  and then (for all I in 1 .. Length (Left) => Contains ("or"'Result, Element (Left, I)))
                  and then (for all I in 1 .. Length (Right) => Contains ("or"'Result, Element (Right, I)));

   --  the elements of both (Python's `s & t`)
   function "and" (Left, Right : Set) return Set
     with Global => null,
          Post => Length ("and"'Result) <= Length (Left)
                  and then (for all I in 1 .. Length ("and"'Result) =>
                              Contains (Left, Element ("and"'Result, I)) and then Contains (Right, Element ("and"'Result, I)))
                  and then (for all I in 1 .. Length (Left) =>
                              (if Contains (Right, Element (Left, I)) then Contains ("and"'Result, Element (Left, I))));

   --  the elements of Left that are not Right's (Python's `s - t`)
   function "-" (Left, Right : Set) return Set
     with Global => null,
          Post => Length ("-"'Result) <= Length (Left)
                  and then (for all I in 1 .. Length ("-"'Result) =>
                              Contains (Left, Element ("-"'Result, I)) and then not Contains (Right, Element ("-"'Result, I)))
                  and then (for all I in 1 .. Length (Left) =>
                              (if not Contains (Right, Element (Left, I)) then Contains ("-"'Result, Element (Left, I))));

   --  the elements of one but not the other (Python's `s ^ t`)
   function "xor" (Left, Right : Set) return Set
     with Global => null,
          Post => Length ("xor"'Result) <= Length (Left) + Length (Right)
                  and then (for all I in 1 .. Length ("xor"'Result) =>
                              Contains (Left, Element ("xor"'Result, I)) /= Contains (Right, Element ("xor"'Result, I)))
                  and then (for all I in 1 .. Length (Left) =>
                              (if not Contains (Right, Element (Left, I)) then Contains ("xor"'Result, Element (Left, I))))
                  and then (for all I in 1 .. Length (Right) =>
                              (if not Contains (Left, Element (Right, I)) then Contains ("xor"'Result, Element (Right, I))));

   --  every element of Left is Right's (Python's `s <= t`, `s.issubset(t)`)
   function "<=" (Left, Right : Set) return Boolean is
     (for all I in 1 .. Length (Left) => Contains (Right, Element (Left, I)));

   --  the same elements (Python's `s == t`)
   function "=" (Left, Right : Set) return Boolean is
     (Left <= Right and then Right <= Left);

   --  a subset that is not the whole of Right (Python's `s < t`)
   function "<" (Left, Right : Set) return Boolean is
     (Left <= Right and then not (Right <= Left));

   --  Python's `s >= t` and `s > t`
   function ">=" (Left, Right : Set) return Boolean is (Right <= Left);

   function ">" (Left, Right : Set) return Boolean is (Right < Left);

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

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

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

   function Iter_Element (Container : Set; Position : Natural) return Element_Type is
     (Element (Container, Position))
     with Pre => Iter_Has_Element (Container, Position);

   procedure Copy (Source : Set; Target : in out Set)
     with Global => null, Always_Terminates,
          Post => Model (Target) = Model (Source);
private
   type Element_Array_Access is access Element_Array;

   --  Data is null for the empty set; otherwise it designates the elements, from 1
   type Set is limited record
      Data : Element_Array_Access;
   end record
     with Type_Invariant =>
       Set.Data = null
       or else (Set.Data'First = 1 and then Set.Data'Length > 0 and then Set.Data'Length < Verada.Storage.Length_Bound
                --  (each element once)
                and then (for all I in Set.Data'Range => (for all J in 1 .. I - 1 => Set.Data (J) /= Set.Data (I))));

   function Length (Container : Set) return Natural is
     (if Container.Data = null then 0 else Container.Data'Length);

   function Model (Container : Set) return Element_Array is
     (if Container.Data = null then [] else Container.Data.all);

   function Element (Container : Set; Index : Positive) return Element_Type is
     (Container.Data (Index));
end Verada.Containers.Unbounded.Ordered_Sets;
