--  Verada.Containers.Unbounded.Maps: maps of any size from keys to
--  elements, on the heap and freed where the map dies (a reclaimed type), the unbounded twin of
--  the bounded maps. The map is limited: copies are made explicitly. Its models are the arrays
--  of its keys (each once, in insertion order) and of their elements, from 1. Only memory
--  limits the size: Insert raises Storage_Error when memory cannot hold the result
--  (Verada.Storage).
with Verada.Storage;

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

   type Map is limited private
     with Default_Initial_Condition => Length (Map) = 0,
          Finalizable => (Finalize => Clear, Relaxed_Finalization => True);

   function Length (Container : Map) return Natural
     with Global => null,
          Post => Length'Result < Verada.Storage.Length_Bound;

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

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

   function Key (Container : Map; Index : Positive) return Key_Type
     with Global => null,
          Pre => Index <= Length (Container),
          Post => Key'Result = Keys_Model (Container) (Index);

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

   --  the first position of K, or 0 when no key is K
   function Find (Container : Map; K : Key_Type) return Natural
     with Global => null,
          Post => (if Find'Result = 0 then (for all I in 1 .. Length (Container) => Key (Container, I) /= K)
                   else Find'Result <= Length (Container) and then Key (Container, Find'Result) = K
                        and then (for all I in 1 .. Find'Result - 1 => Key (Container, I) /= K));

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

   function Value (Container : Map; K : Key_Type) return Element_Type is
     (Element (Container, Find (Container, K)))
     with Pre => Contains (Container, K);

   --  the elements of Container are those of Old, except perhaps for K
   function Same_Elements_Except (Container : Map; Old : Element_Array; K : Key_Type) return Boolean is
     (Old'Length = Length (Container)
      and then (Old'Length = 0 or else Old'First = 1)
      and then (for all I in 1 .. Length (Container) => Key (Container, I) = K or else Element (Container, I) = Old (I)))
     with Ghost;

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

   --  a new entry for K, at the end
   procedure Insert (Container : in out Map; K : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => not Contains (Container, K),
          Post => Keys_Model (Container) = Keys_Model (Container)'Old & K
                  and Elements_Model (Container) = Elements_Model (Container)'Old & New_Item;

   --  K now maps to New_Item
   procedure Replace (Container : in out Map; K : Key_Type; New_Item : Element_Type)
     with Global => null,
          Pre => Contains (Container, K),
          Post => Keys_Model (Container) = Keys_Model (Container)'Old
                  and Length (Container) = Length (Container)'Old
                  and (for all I in 1 .. Length (Container) => (if Key (Container, I) = K then Element (Container, I) = New_Item))
                  and Same_Elements_Except (Container, Elements_Model (Container)'Old, K);

   procedure Copy (Source : Map; Target : in out Map)
     with Global => null,
          Post => Keys_Model (Target) = Keys_Model (Source) and then Elements_Model (Target) = Elements_Model (Source);
private
   type Key_Array_Access is access Key_Array;
   type Element_Array_Access is access Element_Array;

   --  both null for the empty map; otherwise they designate the keys and their elements, from 1
   type Map is limited record
      Keys : Key_Array_Access;
      Items : Element_Array_Access;
   end record
     with Type_Invariant =>
       (Map.Keys = null) = (Map.Items = null)
       and then (Map.Keys = null
                 or else (Map.Keys'First = 1 and then Map.Keys'Length > 0
                          and then Map.Items'First = 1 and then Map.Items'Length = Map.Keys'Length
                          and then Map.Keys'Length < Verada.Storage.Length_Bound));

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

   function Keys_Model (Container : Map) return Key_Array is
     (if Container.Keys = null then [] else Container.Keys.all);

   function Elements_Model (Container : Map) return Element_Array is
     (if Container.Items = null then [] else Container.Items.all);

   function Key (Container : Map; Index : Positive) return Key_Type is
     (Container.Keys (Index));

   function Element (Container : Map; Index : Positive) return Element_Type is
     (Container.Items (Index));
end Verada.Containers.Unbounded.Maps;
