DEVELOPMENT EDITION · 769442725406
Library Reference
On this page

Generic package Verada.Containers.Unbounded.Dictionaries

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.

generic
   type Key_Type is private;
   type Element_Type is private;

Key_Array

type Key_Array is array (Positive range <>) of Key_Type;

Element_Array

type Element_Array is array (Positive range <>) of Element_Type;

Same_Except

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;

the arrays have the same values, except perhaps at Index

Removed

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;

A is B without its element At, element by element (for the keys, and for the values)

Removed

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;

Dictionary

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);

Length

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

(below Length_Bound, as memory holds no more: a for k in d loop's cursor always moves on, and the loop ends)

Key_At

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

the key, and its value, at a position in the order of first addition

Value_At

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

Key_Model

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));

the keys and the values in order, from 1

Value_Model

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));

Position

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));

the position of Key, or 0 when it is not a key

Contains

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

Value

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]

Include

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));

Python's d[k] = v: Key's value becomes New_Item, a new key comes last, and the other keys and values stay

Kept_Unless

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;

the values Old had at the first positions are kept but those of Other's keys

Get

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's value, or Default when Key is not a key (Python's d.get(k, default))

Delete

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);

Key and its value taken out, the other keys and values keeping their order (Python's del d[k] and statement d.pop(k))

Pop

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);

as Delete, Item the value Key had (Python's v = d.pop(k))

Update

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);

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

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);

Target becomes a copy of Source (Python's d.copy())

Clear

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

Iter_First

function Iter_First (Container : Dictionary) return Natural is (1);

for k in d and all(... for k in d): the keys in order

Iter_Next

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

Iter_Has_Element

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

Iter_Key

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

Is_Empty

function Is_Empty (Container : Dictionary) return Boolean is (Length (Container) = 0)
  with Global => null;

whether it has no keys (Varc's m.is_empty())