DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Verada.Containers.Hash_Maps

Verada predefined library: a hash map from integers, a proof enclave. Its entries live in place in one table, open addressing with linear probing, which doubles when it is half full; the heap engine proves the operations against the map's model, its keys and their elements in the order the keys were first added, so that clients treat a map as that value. Only memory limits the size: Include raises Storage_Error when memory cannot hold one more entry (Verada.Storage).

generic
   type Element_Type is private;

Key_Sequences

package Key_Sequences is new Verada.Containers.Functional.Sequences (Integer);

Element_Sequences

package Element_Sequences is new Verada.Containers.Functional.Sequences (Element_Type);

Map_Model

type Map_Model is record
   Keys : Key_Sequences.Sequence;
   Elements : Element_Sequences.Sequence;
end record;

a map's value: its keys, each once, in the order they were first added, and each key's element at the key's position

Has_Key

function Has_Key (M : Map_Model; K : Integer) return Boolean is
  (for some I in 1 .. Key_Sequences.Length (M.Keys) => Key_Sequences.Get (M.Keys, I) = K)
  with Ghost;

whether a model has a key

Position

function Position (M : Map_Model; K : Integer) return Positive
  with Ghost,
       Pre => Has_Key (M, K),
       Post => Position'Result <= Key_Sequences.Length (M.Keys) and then Key_Sequences.Get (M.Keys, Position'Result) = K;

the position of a key a model has

Included_Keys

function Included_Keys (M : Map_Model; K : Integer) return Key_Sequences.Sequence is
  (if Has_Key (M, K) then M.Keys else Key_Sequences.Add (M.Keys, K))
  with Ghost, Pre => Key_Sequences.Length (M.Keys) < Verada.Storage.Length_Bound;

a model with K's element E: a new key last, or an existing key's element replaced in place

Included_Elements

function Included_Elements (M : Map_Model; K : Integer; E : Element_Type) return Element_Sequences.Sequence is
  (if Has_Key (M, K) then Element_Sequences.Set (M.Elements, Position (M, K), E) else Element_Sequences.Add (M.Elements, E))
  with Ghost,
       Pre => Key_Sequences.Length (M.Keys) < Verada.Storage.Length_Bound
              and then Key_Sequences.Length (M.Keys) = Element_Sequences.Length (M.Elements);

Map

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

Model

function Model (M : Map) return Map_Model
  with Ghost,
       Post => Key_Sequences.Length (Model'Result.Keys) = Element_Sequences.Length (Model'Result.Elements);

Length

function Length (M : Map) return Natural
  with Post => Length'Result = Key_Sequences.Length (Model (M).Keys);

Contains

function Contains (M : Map; K : Integer) return Boolean
  with Post => Contains'Result = Has_Key (Model (M), K);

Element

function Element (M : Map; K : Integer) return Element_Type
  with Pre => Has_Key (Model (M), K),
       Post => (for all I in 1 .. Length (M) =>
                  (if Key_Sequences.Get (Model (M).Keys, I) = K then Element_Sequences.Get (Model (M).Elements, I) = Element'Result));

K's element

Include

procedure Include (M : in out Map; K : Integer; E : Element_Type)
  with Post => Model (M).Keys = Included_Keys (Model (M)'Old, K)
               and Model (M).Elements = Included_Elements (Model (M)'Old, K, E);

K's element E: a new key last, or an existing key's element replaced in place

Clear

procedure Clear (M : in out Map)
  with Post => Length (M) = 0;

every key removed (and the table freed: a map that ends is cleared)