--  Verada predefined library: Ada.Containers.Bounded_Hashed_Maps (RM A.18.21), maps of at most
--  Capacity entries from keys to elements, in the design of Verada.Containers.Formal.Maps: the
--  entries are inside the object at places 1 .. Length, in the order they were inserted; a
--  key is looked up by Equivalent_Keys, one entry after another (Hash and Modulus are taken
--  as Ada's are and not needed); 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. Entries are reached by key or by place; there are no cursors (a cursor would
--  name its map by access, which the proof does not follow). Contracts compare keys and
--  elements with the models' Equal_Elements, their types' own equality; Equivalent_Keys and
--  the generic "=" are the ones lookup and the maps' "=" use, as in Ada.
with Verada.Containers.Functional.Sequences;

generic
   type Key_Type is private;
   type Element_Type is private;
   with function Hash (Key : Key_Type) return Hash_Type;
   with function Equivalent_Keys (Left, Right : Key_Type) return Boolean;
   with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ada.Containers.Bounded_Hashed_Maps is
   type Map (Capacity : Count_Type; Modulus : Hash_Type) is private
     with Default_Initial_Condition => Length (Map) = 0,
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Key);

   --  the modulus for a capacity, which a map's declaration gives with it
   function Default_Modulus (Capacity : Count_Type) return Hash_Type is (Hash_Type'Mod (Capacity));

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

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

   --  the key and the element of the entry at a place, from 1
   function Key_At (Container : Map; Index : Count_Type) return Key_Type
     with Global => null,
          Pre => Index in 1 .. Length (Container);

   function Element_At (Container : Map; Index : Count_Type) return Element_Type
     with Global => null,
          Pre => Index in 1 .. Length (Container);

   --  the functional models: the keys and the elements in order, as sequences
   package Key_Models is new Verada.Containers.Functional.Sequences (Key_Type);
   package Element_Models is new Verada.Containers.Functional.Sequences (Element_Type);

   function Keys_Model (Container : Map) return Key_Models.Sequence
     with Ghost,
          Global => null,
          Post => Key_Models.Length (Keys_Model'Result) = Natural (Length (Container))
                  and then (for all I in 1 .. Length (Container) =>
                              Key_Models.Equal_Elements (Key_Models.Get (Keys_Model'Result, Natural (I)), Key_At (Container, I)));

   function Elements_Model (Container : Map) return Element_Models.Sequence
     with Ghost,
          Global => null,
          Post => Element_Models.Length (Elements_Model'Result) = Natural (Length (Container))
                  and then (for all I in 1 .. Length (Container) =>
                              Element_Models.Equal_Elements (Element_Models.Get (Elements_Model'Result, Natural (I)), Element_At (Container, I)));

   --  the entry 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_Entries (Left, Right : Map; 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 Key_Models.Equal_Elements (Key_At (Right, I), Key_At (Left, I))
              and then Element_Models.Equal_Elements (Element_At (Right, I), Element_At (Left, I))
         else I < Length (Left)
              and then Key_Models.Equal_Elements (Key_At (Right, I), Key_At (Left, I + 1))
              and then Element_Models.Equal_Elements (Element_At (Right, I), Element_At (Left, I + 1))))
     with Ghost;

   --  the place of the entry whose key is equivalent to Key, or 0
   function Index_Of (Container : Map; Key : Key_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_Keys (Key_At (Container, I), Key))
                            else Equivalent_Keys (Key_At (Container, Index_Of'Result), Key)
                                 and then (for all I in 1 .. Index_Of'Result - 1 => not Equivalent_Keys (Key_At (Container, I), Key)));

   function Contains (Container : Map; Key : Key_Type) return Boolean is (Index_Of (Container, Key) /= 0);

   --  the element of Key's entry
   function Element (Container : Map; Key : Key_Type) return Element_Type is
     (Element_At (Container, Index_Of (Container, Key)))
     with Pre => Contains (Container, Key);

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

   --  a new entry for Key, at the end
   procedure Insert (Container : in out Map; Key : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => not Contains (Container, Key) and then Length (Container) < Container.Capacity,
          Post => Length (Container) = Length (Container'Old) + 1
                  and then Key_Models.Equal_Elements (Key_At (Container, Length (Container)), Key)
                  and then Element_Models.Equal_Elements (Element_At (Container, Length (Container)), New_Item)
                  and then Same_Entries (Container, Container'Old, 0);

   --  Key's entry now has Key and New_Item
   procedure Replace (Container : in out Map; Key : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, Key),
          Post => Length (Container) = Length (Container'Old)
                  and then Key_Models.Equal_Elements (Key_At (Container, Index_Of (Container'Old, Key)), Key)
                  and then Element_Models.Equal_Elements (Element_At (Container, Index_Of (Container'Old, Key)), New_Item)
                  and then (for all I in 1 .. Length (Container) =>
                              (if I /= Index_Of (Container'Old, Key) then
                                 Key_Models.Equal_Elements (Key_At (Container, I), Key_At (Container'Old, I))
                                 and then Element_Models.Equal_Elements (Element_At (Container, I), Element_At (Container'Old, I))));

   --  Key's entry has Key and New_Item, replaced or new at the end
   procedure Include (Container : in out Map; Key : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, Key) or else Length (Container) < Container.Capacity,
          Post => (if Contains (Container'Old, Key) then
                     Length (Container) = Length (Container'Old)
                     and then Key_Models.Equal_Elements (Key_At (Container, Index_Of (Container'Old, Key)), Key)
                     and then Element_Models.Equal_Elements (Element_At (Container, Index_Of (Container'Old, Key)), New_Item)
                     and then (for all I in 1 .. Length (Container) =>
                                 (if I /= Index_Of (Container'Old, Key) then
                                    Key_Models.Equal_Elements (Key_At (Container, I), Key_At (Container'Old, I))
                                    and then Element_Models.Equal_Elements (Element_At (Container, I), Element_At (Container'Old, I))))
                   else
                     Length (Container) = Length (Container'Old) + 1
                     and then Key_Models.Equal_Elements (Key_At (Container, Length (Container)), Key)
                     and then Element_Models.Equal_Elements (Element_At (Container, Length (Container)), New_Item)
                     and then Same_Entries (Container, Container'Old, 0));

   --  Key's entry is gone; those after it move one place nearer
   procedure Delete (Container : in out Map; Key : Key_Type)
     with Global => null,
          Pre => Contains (Container, Key),
          Post => Length (Container) = Length (Container'Old) - 1
                  and then Same_Entries (Container'Old, Container, Index_Of (Container'Old, Key));

   --  Key's entry is gone, if there was one
   procedure Exclude (Container : in out Map; Key : Key_Type)
     with Global => null,
          Post => (if Contains (Container'Old, Key)
                   then Length (Container) = Length (Container'Old) - 1
                        and then Same_Entries (Container'Old, Container, Index_Of (Container'Old, Key))
                   else Length (Container) = Length (Container'Old)
                        and then Same_Entries (Container'Old, Container, 0));

   --  the same length, and each key of Left in Right with an element equal to Left's ("=")
   function "=" (Left, Right : Map) return Boolean
     with Global => null,
          Post => "="'Result = (Length (Left) = Length (Right)
                                and then (for all I in 1 .. Length (Left) =>
                                            Contains (Right, Key_At (Left, I))
                                            and then Element_At (Left, I) = Element (Right, Key_At (Left, I))));

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

   --  a map of Source's entries with room for Capacity, or for Source's length when Capacity
   --  is 0
   function Copy (Source : Map; Capacity : Count_Type := 0) return Map
     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_Entries (Source, Copy'Result, 0);

   --  `for K of M` and `for all K of M =>...`: the keys, in their order
   function Iter_First (Container : Map) return Count_Type is (1);

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

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

   function Iter_Key (Container : Map; Position : Count_Type) return Key_Type is
     (Key_At (Container, Position))
     with Pre => Iter_Has_Element (Container, Position);
private
   type Key_Array is array (Count_Type range <>) of Key_Type;
   type Element_Array is array (Count_Type range <>) of Element_Type;

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

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

   function Key_At (Container : Map; Index : Count_Type) return Key_Type is (Container.Keys (Index));

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