--  Verada.Containers.Unbounded.Dictionaries (Vibe's `dict`): maps of any size from
--  keys to values, which remember the order their keys were first added in, as Python's dict
--  does; on the heap and freed where the map dies (its keys and values are vectors, which
--  are reclaimed types). The map is limited:
--  copies are made explicitly. Keys and values are seen by position, from 1, in that order.
--  Lookup is a search; a hashed index can come later behind the same contracts.
with Verada.Containers.Unbounded.Vectors;
with Verada.Storage;

generic
   type Key_Type is private;
   type Element_Type is private;
package Verada.Containers.Unbounded.Dictionaries is
   type Key_Array is array (Positive range <>) of Key_Type;
   type Element_Array is array (Positive range <>) of Element_Type;

   --  the arrays have the same values, except perhaps at Index
   function Same_Except (A, B : Element_Array; Index : Natural) return Boolean is
     (A'First = B'First and then A'Last = B'Last
      and then (for all I in A'Range => (if I /= Index then A (I) = B (I))))
     with Ghost;

   --  A is B without its element At, element by element (for the keys, and for the values)
   function Removed (A, B : Key_Array; At_Index : Positive) return Boolean is
     (A'First = 1 and then B'First = 1 and then B'Length = A'Length + 1 and then At_Index <= B'Length
      and then (for all J in 1 .. At_Index - 1 => A (J) = B (J))
      and then (for all J in At_Index .. A'Length => A (J) = B (J + 1)))
     with Ghost;

   function Removed (A, B : Element_Array; At_Index : Positive) return Boolean is
     (A'First = 1 and then B'First = 1 and then B'Length = A'Length + 1 and then At_Index <= B'Length
      and then (for all J in 1 .. At_Index - 1 => A (J) = B (J))
      and then (for all J in At_Index .. A'Length => A (J) = B (J + 1)))
     with Ghost;

   type Dictionary is limited private
     with Default_Initial_Condition => Length (Dictionary) = 0,
          Iterable => (First => Iter_First, Next => Iter_Next, Has_Element => Iter_Has_Element, Element => Iter_Key);

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

   --  the key, and its value, at a position in the order of first addition
   function Key_At (Container : Dictionary; Position : Positive) return Key_Type
     with Global => null,
          Pre => Position <= Length (Container);

   function Value_At (Container : Dictionary; Position : Positive) return Element_Type
     with Global => null,
          Pre => Position <= Length (Container);

   --  the keys and the values in order, from 1
   function Key_Model (Container : Dictionary) return Key_Array
     with Global => null,
          Post => Key_Model'Result'First = 1 and then Key_Model'Result'Last = Length (Container)
                  and then (for all I in 1 .. Length (Container) => Key_Model'Result (I) = Key_At (Container, I));

   function Value_Model (Container : Dictionary) return Element_Array
     with Global => null,
          Post => Value_Model'Result'First = 1 and then Value_Model'Result'Last = Length (Container)
                  and then (for all I in 1 .. Length (Container) => Value_Model'Result (I) = Value_At (Container, I));

   --  the position of Key, or 0 when it is not a key
   function Position (Container : Dictionary; Key : Key_Type) return Natural
     with Global => null,
          Post => Position'Result <= Length (Container)
                  and then (if Position'Result = 0
                            then (for all I in 1 .. Length (Container) => Key_At (Container, I) /= Key)
                            else Key_At (Container, Position'Result) = Key
                                 and then (for all I in 1 .. Position'Result - 1 => Key_At (Container, I) /= Key));

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

   --  Python's `d[k]`
   function Value (Container : Dictionary; Key : Key_Type) return Element_Type is
     (Value_At (Container, Position (Container, Key)))
     with Pre => Contains (Container, Key);

   --  Python's `d[k] = v`: Key's value becomes New_Item, a new key comes last, and the other
   --  keys and values stay
   procedure Include (Container : in out Dictionary; Key : Key_Type; New_Item : Element_Type)
     with Global => null, Always_Terminates,
          Post => (Contains (Container, Key) and then Value (Container, Key) = New_Item)
                  and Length (Container) = Length (Container)'Old + (if Contains (Container, Key)'Old then 0 else 1)
                  and Key_Model (Container) (1 .. Length (Container)'Old) = Key_Model (Container)'Old
                  and Same_Except (Value_Model (Container) (1 .. Length (Container)'Old), Value_Model (Container)'Old, Position (Container, Key));

   --  the values Old had at the first positions are kept but those of Other's keys
   function Kept_Unless (Container, Other : Dictionary; Old : Element_Array) return Boolean is
     (Old'First = 1 and then Old'Length <= Length (Container)
      and then (for all I in 1 .. Old'Length =>
                  (if not Contains (Other, Key_At (Container, I)) then Value_At (Container, I) = Old (I))))
     with Ghost;

   --  Key's value, or Default when Key is not a key (Python's `d.get(k, default)`)
   function Get (Container : Dictionary; Key : Key_Type; Default : Element_Type) return Element_Type is
     (if Contains (Container, Key) then Value (Container, Key) else Default);

   --  Key and its value taken out, the other keys and values keeping their order (Python's
   --  `del d[k]` and statement `d.pop(k)`)
   procedure Delete (Container : in out Dictionary; Key : Key_Type)
     with Global => null, Always_Terminates,
          Pre => Contains (Container, Key),
          Post => not Contains (Container, Key)
                  and Length (Container) = Length (Container)'Old - 1
                  and Removed (Key_Model (Container), Key_Model (Container)'Old, Position (Container, Key)'Old)
                  and Removed (Value_Model (Container), Value_Model (Container)'Old, Position (Container, Key)'Old);

   --  as Delete, Item the value Key had (Python's `v = d.pop(k)`)
   procedure Pop (Container : in out Dictionary; Key : Key_Type; Item : out Element_Type)
     with Global => null, Always_Terminates,
          Pre => Contains (Container, Key),
          Post => Item = Value (Container, Key)'Old
                  and not Contains (Container, Key)
                  and Length (Container) = Length (Container)'Old - 1
                  and Removed (Key_Model (Container), Key_Model (Container)'Old, Position (Container, Key)'Old)
                  and Removed (Value_Model (Container), Value_Model (Container)'Old, Position (Container, Key)'Old);

   --  each of Other's keys, in its order, given its value there, as `d[k] = v` gives it
   --  (Python's `d.update(other)`): the old keys stay where they were, those of Other that
   --  were not keys come after them, and an old key that is not Other's keeps its value
   procedure Update (Container : in out Dictionary; Other : Dictionary)
     with Global => null, Always_Terminates,
          Post => (for all I in 1 .. Length (Other) =>
                     Contains (Container, Key_At (Other, I))
                     and then Value (Container, Key_At (Other, I)) = Value (Other, Key_At (Other, I)))
                  and Length (Container) in Length (Container)'Old .. Length (Container)'Old + Length (Other)
                  and Key_Model (Container) (1 .. Length (Container)'Old) = Key_Model (Container)'Old
                  and (for all I in Length (Container)'Old + 1 .. Length (Container) => Contains (Other, Key_At (Container, I)))
                  and Kept_Unless (Container, Other, Value_Model (Container)'Old);

   --  Target becomes a copy of Source (Python's `d.copy()`)
   procedure Copy (Source : Dictionary; Target : in out Dictionary)
     with Global => null, Always_Terminates,
          Post => Key_Model (Target) = Key_Model (Source) and Value_Model (Target) = Value_Model (Source);

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

   --  `for k in d` and `all(... for k in d)`: the keys in order
   function Iter_First (Container : Dictionary) return Natural is (1);

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

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

   function Iter_Key (Container : Dictionary; Position : Natural) return Key_Type is
     (Key_At (Container, Position))
     with Pre => Iter_Has_Element (Container, Position);

   --  whether it has no keys (Varc's `m.is_empty()`)
   function Is_Empty (Container : Dictionary) return Boolean is (Length (Container) = 0)
     with Global => null;

private
   package Key_Vectors is new Verada.Containers.Unbounded.Vectors (Key_Type);
   package Value_Vectors is new Verada.Containers.Unbounded.Vectors (Element_Type);

   --  the keys and values side by side
   type Dictionary is limited record
      Keys : Key_Vectors.Vector;
      Values : Value_Vectors.Vector;
   end record
     with Type_Invariant =>
       Key_Vectors.Length (Dictionary.Keys) = Value_Vectors.Length (Dictionary.Values)
       --  (each key once)
       and then (for all I in 1 .. Key_Vectors.Length (Dictionary.Keys) =>
                   (for all J in 1 .. I - 1 => Key_Vectors.Element (Dictionary.Keys, J) /= Key_Vectors.Element (Dictionary.Keys, I)));

   function Length (Container : Dictionary) return Natural is (Key_Vectors.Length (Container.Keys));

   function Key_At (Container : Dictionary; Position : Positive) return Key_Type is
     (Key_Vectors.Element (Container.Keys, Position));

   function Value_At (Container : Dictionary; Position : Positive) return Element_Type is
     (Value_Vectors.Element (Container.Values, Position));

   function Key_Model (Container : Dictionary) return Key_Array is
     (Key_Array (Key_Vectors.Model (Container.Keys)));

   function Value_Model (Container : Dictionary) return Element_Array is
     (Element_Array (Value_Vectors.Model (Container.Values)));
end Verada.Containers.Unbounded.Dictionaries;
