--  Verada predefined library: arenas of at most Capacity elements reached through handles
--  (shared-mutable-state.md, ledger D78), for graphs that ownership alone cannot express:
--  shared referents, cycles, parent links. A handle is a slot and a generation; removing an
--  element makes its handles invalid, so that using one is a proof failure rather than a read
--  of a reused slot. Each operation says that every other element is unchanged.
generic
   type Element_Type is private;
   Capacity : Positive;
package Verada.Containers.Arenas is
   --  a handle: no handle (0), or a generation and a slot, as Generation * Capacity + Slot
   subtype Handle is Long_Long_Integer range 0 .. Long_Long_Integer'Last;

   No_Handle : constant Handle := 0;

   type Arena is private
     with Default_Initial_Condition => (for all H in Handle => not Valid (Arena, H));

   function Valid (A : Arena; H : Handle) return Boolean
     with Global => null,
          Post => (if Valid'Result then H /= No_Handle);

   function Element (A : Arena; H : Handle) return Element_Type
     with Global => null,
          Pre => Valid (A, H);

   --  whether another element fits
   function Has_Room (A : Arena) return Boolean
     with Global => null;

   procedure Insert (A : in out Arena; Item : Element_Type; H : out Handle)
     with Global => null,
          Pre => Has_Room (A),
          Post => Valid (A, H) and then Element (A, H) = Item
                  and then not Valid (A'Old, H)
                  and then (for all G in Handle => (if G /= H then Valid (A, G) = Valid (A'Old, G)))
                  and then (for all G in Handle => (if G /= H and then Valid (A'Old, G) then Element (A, G) = Element (A'Old, G)));

   procedure Replace_Element (A : in out Arena; H : Handle; Item : Element_Type)
     with Global => null,
          Pre => Valid (A, H),
          Post => Valid (A, H) and then Element (A, H) = Item
                  and then Has_Room (A) = Has_Room (A'Old)
                  and then (for all G in Handle => Valid (A, G) = Valid (A'Old, G))
                  and then (for all G in Handle => (if G /= H and then Valid (A'Old, G) then Element (A, G) = Element (A'Old, G)));

   --  the element goes; its handles become invalid for good
   procedure Remove (A : in out Arena; H : Handle)
     with Global => null,
          Pre => Valid (A, H),
          Post => not Valid (A, H)
                  and then (for all G in Handle => (if G /= H then Valid (A, G) = Valid (A'Old, G)))
                  and then (for all G in Handle => (if G /= H and then Valid (A'Old, G) then Element (A, G) = Element (A'Old, G)));
private
   --  a slot's generation counts the elements it has held; one that has run out of
   --  generations is never used again
   Last_Generation : constant := (Long_Long_Integer'Last - 2 ** 31) / 2 ** 31;

   type Slot is record
      Used : Boolean := False;
      Generation : Long_Long_Integer range 0 .. Last_Generation := 0;
      Value : Element_Type;
   end record;
   --  a slot's value is set when it is first used: Element's precondition keeps every read
   --  to used slots
   pragma Annotate (GNATprove, Intentional, "not fully initialized", "unused slots are never read");

   type Slot_Array is array (1 .. Capacity) of Slot;

   type Arena is record
      Slots : Slot_Array;
   end record;

   function Slot_Of (H : Handle) return Positive is (Positive ((H - 1) mod Long_Long_Integer (Capacity) + 1))
     with Pre => H /= No_Handle;

   function Generation_Of (H : Handle) return Long_Long_Integer is ((H - 1) / Long_Long_Integer (Capacity))
     with Pre => H /= No_Handle;

   function Valid (A : Arena; H : Handle) return Boolean is
     (H /= No_Handle
      and then Slot_Of (H) <= Capacity
      and then A.Slots (Slot_Of (H)).Used
      and then A.Slots (Slot_Of (H)).Generation = Generation_Of (H));

   function Element (A : Arena; H : Handle) return Element_Type is (A.Slots (Slot_Of (H)).Value);

   function Has_Room (A : Arena) return Boolean is
     (for some I in 1 .. Capacity => not A.Slots (I).Used and then A.Slots (I).Generation < Last_Generation);
end Verada.Containers.Arenas;
