The HashConsMap
structure implements functional, finite maps keyed
by hash-consed objects. A balanced tree structure is used in the
representation.
Synopsis
signature HASH_CONS_MAP
structure HashConsMap : HASH_CONS_MAP
Interface
type 'a obj = 'a HashCons.obj
type ('a, 'b) map
val isEmpty : ('a, 'b) map -> bool
val singleton : ('a obj * 'b) -> ('a, 'b) map
val insert : ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
val insert' : (('a obj * 'b) * ('a, 'b) map) -> ('a, 'b) map
val insertWith : (('b * 'b) -> 'b)
-> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
val insertWithi : (('a obj * 'b * 'b) -> 'b)
-> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
val find : ('a, 'b) map * 'a obj -> 'b option
val lookup : ('a, 'b) map * 'a obj -> 'b
val inDomain : (('a, 'b) map * 'a obj) -> bool
val remove : ('a, 'b) map * 'a obj -> ('a, 'b) map * 'b
val empty : ('a, 'b) map
val numItems : ('a, 'b) map -> int
val listItems : ('a, 'b) map -> 'b list
val listItemsi : ('a, 'b) map -> ('a obj * 'b) list
val listKeys : ('a, 'b) map -> 'a obj list
val collate : ('b * 'b -> order) -> (('a, 'b) map * ('a, 'b) map) -> order
val unionWith : ('b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map)
-> ('a, 'b) map
val unionWithi : ('a obj * 'b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map)
-> ('a, 'b) map
val intersectWith : ('b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map)
-> ('a, 'd) map
val intersectWithi : ('a obj * 'b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map)
-> ('a, 'd) map
val mergeWith : ('b option * 'c option -> 'd option)
-> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
val mergeWithi : ('a obj * 'b option * 'c option -> 'd option)
-> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
val app : ('b -> unit) -> ('a, 'b) map -> unit
val appi : (('a obj * 'b) -> unit) -> ('a, 'b) map -> unit
val map : ('b -> 'c) -> ('a, 'b) map -> ('a, 'c) map
val mapi : ('a obj * 'b -> 'c) -> ('a, 'b) map -> ('a, 'c) map
val fold : ('b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c
val foldi : ('a obj * 'b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c
val filter : ('b -> bool) -> ('a, 'b) map -> ('a, 'b) map
val filteri : ('a obj * 'b -> bool) -> ('a, 'b) map -> ('a, 'b) map
val mapPartial : ('b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map
val mapPartiali : ('a obj * 'b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map
val exists : ('b -> bool) -> ('a, 'b) map -> bool
val existsi : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool
val all : ('b -> bool) -> ('a, 'b) map -> bool
val alli : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool
Description
In the description of operations below, we write \(\mathbf{dom}(m)\) for the domain of the map \(m\) (i.e, the set of keys for which \(m\) is defined), and \(\mathbf{rng}(m)\) for its range (i.e., the set \(\{ m(k)\;|\;k \in \mathbf{dom}(m) \}\)). It is also useful to view a map as the set of key-value pairs \(\{ (k, m(k))\;|\;k \in \mathbf{dom}(m) \}\), which we call the items of \(m\).
type 'a obj = 'a HashCons.obj
-
Hash-consed objects are the search keys for the finite maps.
type ('a, 'b) map
-
A finite map from
'a obj
values to'b
values. val empty : ('a, 'b) map
-
The empty map.
val singleton : ('a obj * 'b) -> ('a, 'b) map
-
singleton (obj, v)
creates the singleton map that mapsobj
tov
. val insert : ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
-
insert (m, obj, v)
adds the mapping fromobj
tov
tom
. This mapping overrides any previous mapping fromobj
. val insert' : (('a obj * 'b) * ('a, 'b) map) -> ('a, 'b) map
-
insert' ((obj, v), map)
adds the mapping fromobj
tov
tom
. This mapping overrides any previous mapping fromobj
. val insertWith : (('b * 'b) -> 'b) -> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
-
insertWith comb (m, obj, v)
adds the mapping fromobj
tovalue
tom
, wherevalue = comb(v', v)
, ifm
already contained a mapping fromobj
tov'
; otherwise,value = v
. val insertWithi : (('a obj * 'b * 'b) -> 'b) -> ('a, 'b) map * 'a obj * 'b -> ('a, 'b) map
-
insertWithi comb (m, obj, v)
adds the mapping fromobj
tovalue
tom
, wherevalue = comb(obj, v', v)
, ifm
already contained a mapping fromobj
tov'
; otherwise,value = v
. val find : ('a, 'b) map * 'a obj -> 'b option
-
find (m, obj)
returnsSOME v
, ifm
mapsobj
tov
andNONE
otherwise. val lookup : ('a, 'b) map * 'a obj -> 'b
-
lookup (m, obj)
returnsv
, ifm
mapsobj
tov
; otherwise it raises the exceptionNotFound
. val inDomain : (('a, 'b) map * 'a obj) -> bool
-
inDomain (m, obj)
returnstrue
ifobj
is in the domain ofm
. val remove : ('a, 'b) map * 'a obj -> ('a, 'b) map * 'b
-
remove (m, obj)
returns the pair(m', v)
, ifm
mapsobj
tov
and wherem'
ism
withobj
removed from its domain. Ifobj
is not in the domain ofm
, then it raises the exceptionNotFound
. val isEmpty : ('a, 'b) map -> bool
-
isEmpty m
returns true if, and only if,m
is empty. val numItems : ('a, 'b) map -> int
-
numItems m
returns the size ofm
's domain. val listItems : ('a, 'b) map -> 'b list
-
listItems m
returns a list of the values in the range ofm
. Note that this list will contain duplicates when multiple keys inm
's domain map to the same value. val listKeys : ('a, 'b) map -> 'a obj list
-
listKeys m
returns a list of the objects in the domain ofm
. val listItemsi : ('a, 'b) map -> ('a obj * 'b) list
-
listItemsi m
returns a list of(obj, v)
pairs, wherem
mapsobj
tov
. val collate : ('b * 'b -> order) -> (('a, 'b) map * ('a, 'b) map) -> order
-
collate cmpV (m1, m2)
returns the order of the two maps, wherecmpV
is used to compare the values in the domain. val unionWith : ('b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map) -> ('a, 'b) map
-
unionWith comb (m1, m2)
returns the union of the two maps, using the functioncomb
to combine values when there is a collision of keys. More formally, this expression returns the map\[ \begin{array}{l} \{ (k, \mathtt{m1}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \setminus \mathbf{dom}(\mathtt{m2}) \} \cup \\ \{ (k, \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m2}) \setminus \mathbf{dom}(\mathtt{m1}) \} \cup \\ \{ (k, \mathtt{comb}(\mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \} \end{array}\]For example, we could implement a multiset of objects by mapping objects to their multiplicity. Then, the union of two multisets could be defined by
fun union (ms1, ms2) = unionWith Int.+ (ms1, ms2)
val unionWithi : ('a obj * 'b * 'b -> 'b) -> (('a, 'b) map * ('a, 'b) map) -> ('a, 'b) map
-
unionWithi comb (m1, m2)
returns the union of the two maps, using the functioncomb
to combine values when there is a collision of keys. More formally, this expression returns the map\[ \begin{array}{l} \{ (k, \mathtt{m1}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \setminus \mathbf{dom}(\mathtt{m2}) \} \cup \\ \{ (k, \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m2}) \setminus \mathbf{dom}(\mathtt{m1}) \} \cup \\ \{ (k, \mathtt{comb}(k, \mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \} \end{array}\] val intersectWith : ('b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
-
intersectWith comb (m1, m2)
returns the intersection of the two maps, where the values in the range are a computed by applying the functioncomb
to the values from the two maps. More formally, this expression returns the map\[ \{ (k, \mathtt{comb}(\mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \}\] val intersectWithi : ('a obj * 'b * 'c -> 'd) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
-
intersectWithi comb (m1, m2)
returns the intersection of the two maps, where the values in the range are a computed by applying the functioncomb
to the kay and the values from the two maps. More formally, this expression returns the map\[ \{ (k, \mathtt{comb}(k, \mathtt{m1}(k), \mathtt{m2}(k)) \;|\;k \in \mathbf{dom}(\mathtt{m1}) \cap \mathbf{dom}(\mathtt{m2}) \}\] val mergeWith : ('b option * 'c option -> 'd option) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
-
mergeWith comb (m1, m2)
merges the two maps using the functioncomb
as a decision procedure for adding elements to the new map. For each object \(\mathtt{obj} \in \mathbf{dom}(\mathtt{m1}) \cup \mathbf{dom}(\mathtt{m2})\), we evaluatecomb(optV1, optV2)
, whereoptV1
isSOME v
if \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m1}\) and isNONE
if latexmath:[\mathtt{obj} \not\in \mathbf{dom}(\mathtt{m1}); likewise foroptV2
. Ifcomb(optV1, optV2)
returnsSOME v'
, then we add(obj, v')
to the result.The
mergeWith
function is a generalization of theunionWith
andintersectionWith
functions. val mergeWithi : ('a obj * 'b option * 'c option -> 'd option) -> (('a, 'b) map * ('a, 'c) map) -> ('a, 'd) map
-
mergeWithi comb (m1, m2)
merges the two maps using the functioncomb
as a decision procedure for adding elements to the new map. The difference between this function andmergeWith
is that thecomb
function takes thekey
value in addition to the optional values from the range. val app : ('b -> unit) -> ('a, 'b) map -> unit
-
app f m
applies the functionf
to the values in the range ofm
. val appi : (('a obj * 'b) -> unit) -> ('a, 'b) map -> unit
-
appi f map
applies the functionf
to the key-value pairs that definem
. val map : ('b -> 'c) -> ('a, 'b) map -> ('a, 'c) map
-
map f m
creates a new finite mapm'
by applying the functionf
to the values in the range ofm
. Thus, if \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), then(obj, f v)
will be inm'
. val mapi : ('a obj * 'b -> 'c) -> ('a, 'b) map -> ('a, 'c) map
-
mapi f m
creates a new finite mapm'
by applying the functionf
to the key-value pairs ofm
. Thus, if \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), then(obj, f(obj, v))
will be inm'
. val fold : ('b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c
-
fold f init m
folds the functionf
over the range ofm
usinginit
as the initial value. val foldi : ('a obj * 'b * 'c -> 'c) -> 'c -> ('a, 'b) map -> 'c
-
foldi f init m
folds the functionf
over the key-value pairs inm
usinginit
as the initial value. val filter : ('b -> bool) -> ('a, 'b) map -> ('a, 'b) map
-
filter pred m
filters out those items(obj, v)
fromm
, such thatpred v
returnsfalse
. More formally, this expression returns the map \(\{ (\mathtt{obj}, \mathtt{v})\;|\;\mathtt{obj} \in \mathbf{dom}(\mathtt{m}) \wedge \mathtt{pred}(\mathtt{v}) \}\). val filteri : ('a obj * 'b -> bool) -> ('a, 'b) map -> ('a, 'b) map
-
filteri pred m
filters out those items(obj, v)
fromm
, such thatpred(obj, v)
returnsfalse
. More formally, this expression returns the map \(\{ (\mathtt{obj}, \mathtt{v})\;|\;\mathtt{obj} \in \mathbf{dom}(\mathtt{m}) \wedge \mathtt{pred}(\mathtt{obj}, \mathtt{v}) \}\). val mapPartial : ('b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map
-
mapPartial f m
maps the partial functionf
over the items ofm
. More formally, this expression returns the map
val mapPartiali : ('a obj * 'b -> 'c option) -> ('a, 'b) map -> ('a, 'c) map
-
mapPartiali f m
maps the partial functionf
over the items ofm
. More formally, this expression returns the map
val exists : ('b -> bool) -> ('a, 'b) map -> bool
-
exists pred m
returnstrue
if, and only if, there exists an item \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), such thatpred v
returnstrue
. val existsi : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool
-
exists pred m
returnstrue
if, and only if, there exists an item \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\), such thatpred(obj, v)
returnstrue
. val all : ('b -> bool) -> ('a, 'b) map -> bool
-
all pred m
returnstrue
if, and only if,pred v
returnstrue
for all items \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\). val alli : ('a obj * 'b -> bool) -> ('a, 'b) map -> bool
-
all pred m
returnstrue
if, and only if,pred(obj, v)
returnstrue
for all items \((\mathtt{obj}, \mathtt{v}) \in \mathtt{m}\).