interface NewDictionary
import
:Iter
:Order
export
insertError, lookupError :Exception with :String end
(* Exception handling for 'insert' und 'lookup'. *)
T (Element, Key <:Ok) <:Ok
(* Dictionary of elements of type 'Element'
with keys of type 'Key'. *)
Let Hooks(Element, Key <:Ok) =
Tuple
getKey(:Element) :Key
keyOrder :Order.T(Key)
fmtKey(:Key) :String
fmtElement(:Element) :String
end
(* Set of elemantary function for dictionary handling. *)
new(Element, Key <:Ok hooks :Hooks(Element Key))
:T(Element Key)
(* Builds an empty dictionary being handled by functions
in tuple 'hooks'. *)
empty(Element, Key <:Ok dict :T(Element Key)) :Bool
(* Checks if 'dict' is empty. *)
insert(Element, Key <:Ok dict :T(Element Key)
element :Element) :Ok
(* Inserts an 'element' into 'dict'. If 'getKey(element)'
already exists, then 'raise insertError with
...fmtElement(element)'. *)
enter(Element, Key <:Ok dict :T(Element Key)
element :Element) :Ok
(* Inserts an 'element' into 'dict'. If the key retrieved by
'getKey(element)' already exists in 'dict', then override
the former entry, so that the new 'element' is really inserted. *)
lookup(Element, Key <:Ok dict :T(Element Key)
key :Key) :Element
(* Searches for 'key' and returns the element found in 'dict'.
If search is unsuccesful 'raise lookupError with ...fmtKey(key)'. *)
elements(Element, Key <:Ok dict :T(Element Key))
:Iter.T(Element)
(* Returns an iteration over the elements of 'dict'. *)
create(Element, Key <:Ok hooks :Hooks(Element Key)
from :Iter.T(Element)) :T(Element Key)
(* Creates a dictionary from the iteration 'from'.
'hooks' contains functions for dictionary handling
(see 'new'). *)
end;
Imagine this "dictionary" as a kind of database relation with elements that are identified by a key attribute.Insert or enter serveral persons into your dictionary and try to look them up. Don't forget to test the dictionary on exceptions, too.
Construct an iteration of Persons and make a dictionary out of it afterwards.
Test this second dictionary by looking up persons.