The ORD_MAP
signature defines an interface to finite maps
over ordered keys. The SML/NJ Library provides a number of
different implementations of this interface. Functors are
provided for constructing maps for user-defined key types;
in addition, a number of instances for specific types
are also provided.
Synopsis
signature ORD_MAP
structure AtomMap : ORD_MAP where type Key.ord_key = Atom.atom
structure AtomBinaryMap : ORD_MAP where type Key.ord_key = Atom.atom
structure AtomRedBlackMap : ORD_MAP where type Key.ord_key = Atom.atom
structure IntBinaryMap : ORD_MAP where type Key.ord_key = int
structure IntListMap : ORD_MAP where type Key.ord_key = int
structure IntRedBlackMap : ORD_MAP where type Key.ord_key = int
structure WordRedBlackMap : ORD_MAP where type Key.ord_key = word
Interface
structure Key : ORD_KEY
type 'a map
val empty : 'a map
val isEmpty : 'a map -> bool
val singleton : (Key.ord_key * 'a) -> 'a map
val insert : 'a map * Key.ord_key * 'a -> 'a map
val insert' : ((Key.ord_key * 'a) * 'a map) -> 'a map
val insertWith : ('a * 'a -> 'a) -> 'a map * Key.ord_key * 'a -> 'a map
val insertWithi : (Key.ord_key * 'a * 'a -> 'a) -> 'a map * Key.ord_key * 'a -> 'a map
val find : 'a map * Key.ord_key -> 'a option
val lookup : 'a map * Key.ord_key -> 'a
val inDomain : ('a map * Key.ord_key) -> bool
val remove : 'a map * Key.ord_key -> 'a map * 'a
val findAndRemove : 'a map * Key.ord_key -> ('a map * 'a) option
val first : 'a map -> 'a option
val firsti : 'a map -> (Key.ord_key * 'a) option
val numItems : 'a map -> int
val listItems : 'a map -> 'a list
val listItemsi : 'a map -> (Key.ord_key * 'a) list
val listKeys : 'a map -> Key.ord_key list
val collate : ('a * 'a -> order) -> ('a map * 'a map) -> order
val extends : ('a * 'b -> order) -> ('a map * 'b map) -> bool
val unionWith : ('a * 'a -> 'a) -> ('a map * 'a map) -> 'a map
val unionWithi : (Key.ord_key * 'a * 'a -> 'a) -> ('a map * 'a map) -> 'a map
val intersectWith : ('a * 'b -> 'c) -> ('a map * 'b map) -> 'c map
val intersectWithi : (Key.ord_key * 'a * 'b -> 'c) -> ('a map * 'b map) -> 'c map
val mergeWith : ('a option * 'b option -> 'c option)
-> ('a map * 'b map) -> 'c map
val mergeWithi : (Key.ord_key * 'a option * 'b option -> 'c option)
-> ('a map * 'b map) -> 'c map
val app : ('a -> unit) -> 'a map -> unit
val appi : ((Key.ord_key * 'a) -> unit) -> 'a map -> unit
val map : ('a -> 'b) -> 'a map -> 'b map
val mapi : (Key.ord_key * 'a -> 'b) -> 'a map -> 'b map
val foldl : ('a * 'b -> 'b) -> 'b -> 'a map -> 'b
val foldli : (Key.ord_key * 'a * 'b -> 'b) -> 'b -> 'a map -> 'b
val foldr : ('a * 'b -> 'b) -> 'b -> 'a map -> 'b
val foldri : (Key.ord_key * 'a * 'b -> 'b) -> 'b -> 'a map -> 'b
val filter : ('a -> bool) -> 'a map -> 'a map
val filteri : (Key.ord_key * 'a -> bool) -> 'a map -> 'a map
val mapPartial : ('a -> 'b option) -> 'a map -> 'b map
val mapPartiali : (Key.ord_key * 'a -> 'b option) -> 'a map -> 'b map
val exists : ('a -> bool) -> 'a map -> bool
val existsi : (Key.ord_key * 'a -> bool) -> 'a map -> bool
val all : ('a -> bool) -> 'a map -> bool
val alli : (Key.ord_key * 'a -> bool) -> 'a map -> bool
Description
structure Key : ORD_KEY
-
This substructure defines the type of keys used to index the maps and the comparison function used to order them.
type 'a map
-
A finite map from
Key.ord_key
values to'b
values. val empty : 'a map
-
The empty map.
val isEmpty : 'a map -> bool
-
isEmpty m
returns true if, and only if,m
is empty. val singleton : (Key.ord_key * 'a) -> 'a map
-
singleton (key, v)
creates the singleton map that mapskey
tov
. val insert : 'a map * Key.ord_key * 'a -> 'a map
-
insert (m, key, v)
adds the mapping fromkey
tov
tom
. This mapping overrides any previous mapping fromkey
. val insert' : ((Key.ord_key * 'a) * 'a map) -> 'a map
-
insert' ((key, v), map)
adds the mapping fromkey
tov
tom
. This mapping overrides any previous mapping fromkey
. val insertWith : ('a * 'a -> 'a) -> 'a map * Key.ord_key * 'a -> 'a map
-
insertWith comb (m, key, v)
adds the mapping fromkey
tovalue
tom
, wherevalue = comb(v', v)
, ifm
already contained a mapping fromkey
tov'
; otherwise,value = v
. val insertWithi : (Key.ord_key * 'a * 'a -> 'a) -> 'a map * Key.ord_key * 'a -> 'a map
-
insertWithi comb (m, key, v)
adds the mapping fromkey
tovalue
tom
, wherevalue = comb(key, v', v)
, ifm
already contained a mapping fromkey
tov'
; otherwise,value = v
. val find : 'a map * Key.ord_key -> 'a option
-
find (m, key)
returnsSOME v
, ifm
mapskey
tov
andNONE
otherwise. val lookup : 'a map * Key.ord_key -> 'a
-
lookup (m, key)
returnsv
, ifm
mapskey
tov
; otherwise it raises the exceptionNotFound
. val inDomain : ('a map * Key.ord_key) -> bool
-
inDomain (m, key)
returnstrue
ifkey
is in the domain ofm
. val remove : 'a map * Key.ord_key -> 'a map * 'a
-
remove (m, key)
returns the pair(m', v)
, ifm
mapskey
tov
and wherem'
ism
withkey
removed from its domain. Ifkey
is not in the domain ofm
, then it raises the exceptionNotFound
. val findAndRemove : 'a map * Key.ord_key -> ('a map * 'a) option
-
findAndRemove (m, key)
returnsSOME(m', v)
, ifm
mapskey
tov
and wherem'
ism
withkey
removed from its domain. Ifkey
is not in the domain ofm
, then it returnsNONE
. val first : 'a map -> 'a option
-
first m
returnsSOME item
whenitem
is the value associated with the first (or smallest) key in the domain of the mapm
. It returnsNONE
when the map is empty. val firsti : 'a map -> (Key.ord_key * 'a) option
-
first m
returnsSOME(key, item)
whenkey
is the first (or smallest) key in the domain of the mapm
andkey
maps toitem
. It returnsNONE
when the map is empty. val numItems : 'a map -> int
-
numItems m
returns the size ofm
's domain. val listItems : 'a map -> 'a 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 listItemsi : 'a map -> (Key.ord_key * 'a) list
-
listItemsi m
returns a list of the key-value pairs inm
. val listKeys : 'a map -> Key.ord_key list
-
listKeys m
returns a list of the keys in the domain ofm
. val equiv : ('a * 'b -> order) -> ('a map * 'b map) -> bool
-
equiv eqV (m1, m2)
returns true if the two maps have the same domains and if, for allx
in the domain of the maps,eqV(lookup(m1, x), lookup(m2, x))
evaluates totrue
. val collate : ('a * 'b -> order) -> ('a map * 'b map) -> order
-
collate cmpV (m1, m2)
returns the order of the two maps, wherecmpV
is used to compare the values in the range of the maps. val extends : ('a * 'b -> order) -> ('a map * 'b map) -> bool
-
extends exV (m1, m2)
returnstrue
if the domain ofm2
is a subset of the domain ofm1
and if, for allx
in the domain ofm2
,exV(lookup(m1, x), lookup(m2, x))
evaluates totrue
. val unionWith : ('a * 'a -> 'a) -> ('a map * 'a map) -> 'a 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 keys by mapping keys to their multiplicity. Then, the union of two multisets could be defined by
fun union (ms1, ms2) = unionWith Int.+ (ms1, ms2)
val unionWithi : (Key.ord_key * 'a * 'a -> 'a) -> ('a map * 'a map) -> 'a 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 : ('a * 'b -> 'c) -> ('a map * 'b map) -> 'c 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 : (Key.ord_key * 'a * 'b -> 'c) -> ('a map * 'b map) -> 'c 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 : ('a option * 'b option -> 'c option) -> ('a map * 'b map) -> 'c 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 key \(\mathtt{key} \in \mathbf{dom}(\mathtt{m1}) \cup \mathbf{dom}(\mathtt{m2})\), we evaluatecomb(optV1, optV2)
, whereoptV1
isSOME v
if \((\mathtt{key}, \mathtt{v}) \in \mathtt{m1}\) and isNONE
if latexmath:[\mathtt{key} \not\in \mathbf{dom}(\mathtt{m1}); likewise foroptV2
. Ifcomb(optV1, optV2)
returnsSOME v'
, then we add(key, v')
to the result.The
mergeWith
function is a generalization of theunionWith
andintersectionWith
functions. val mergeWithi : (Key.ord_key * 'a option * 'b option -> 'c option) -> ('a map * 'b map) -> 'c 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 : ('a -> unit) -> 'a map -> unit
-
app f m
applies the functionf
to the values in the range ofm
. val appi : ((Key.ord_key * 'a) -> unit) -> 'a map -> unit
-
appi f map
applies the functionf
to the key-value pairs that definem
. val map : ('a -> 'b) -> 'a map -> 'b map
-
map f m
creates a new finite mapm'
by applying the functionf
to the values in the range ofm
. Thus, if \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\), then(key, f v)
will be inm'
. val mapi : (Key.ord_key * 'a -> 'b) -> 'a map -> 'b map
-
mapi f m
creates a new finite mapm'
by applying the functionf
to the key-value pairs ofm
. Thus, if \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\), then(key, f(key, v))
will be inm'
. val foldl : ('a * 'b -> 'b) -> 'b -> 'a map -> 'b
-
foldl fl init m
folds the functionf
over the range ofm
usinginit
as the initial value. Items are processed in increasing order of their key values. val foldli : (Key.ord_key * 'a * 'b -> 'b) -> 'b -> 'a map -> 'b
-
foldli f init m
folds the functionf
over the key-value pairs inm
usinginit
as the initial value. Items are processed in increasing order of their key values. val foldr : ('a * 'b -> 'b) -> 'b -> 'a map -> 'b
-
foldr fl init m
folds the functionf
over the range ofm
usinginit
as the initial value. Items are processed in decreasing order of their key values. val foldri : (Key.ord_key * 'a * 'b -> 'b) -> 'b -> 'a map -> 'b
-
foldri f init m
folds the functionf
over the key-value pairs inm
usinginit
as the initial value. Items are processed in decreasing order of their key values. val filter : ('a -> bool) -> 'a map -> 'a map
-
filter pred m
filters out those items(key, v)
fromm
, such thatpred v
returnsfalse
. More formally, this expression returns the map \(\{ (\mathtt{key}, \mathtt{v})\;|\;\mathtt{key} \in \mathbf{dom}(\mathtt{m}) \wedge \mathtt{pred}(\mathtt{v}) \}\). val filteri : (Key.ord_key * 'a -> bool) -> 'a map -> 'a map
-
filteri pred m
filters out those items(key, v)
fromm
, such thatpred(key, v)
returnsfalse
. More formally, this expression returns the map \(\{ (\mathtt{key}, \mathtt{v})\;|\;\mathtt{key} \in \mathbf{dom}(\mathtt{m}) \wedge \mathtt{pred}(\mathtt{key}, \mathtt{v}) \}\). val mapPartial : ('a -> 'b option) -> 'a map -> 'b map
-
mapPartial f m
maps the partial functionf
over the items ofm
. More formally, this expression returns the map
val mapPartiali : (Key.ord_key * 'a -> 'b option) -> 'a map -> 'b map
-
mapPartiali f m
maps the partial functionf
over the items ofm
. More formally, this expression returns the map
val exists : ('a -> bool) -> 'a map -> bool
-
exists pred m
returnstrue
if, and only if, there exists an item \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\), such thatpred v
returnstrue
. val existsi : (Key.ord_key * 'a -> bool) -> 'a map -> bool
-
exists pred m
returnstrue
if, and only if, there exists an item \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\), such thatpred(key, v)
returnstrue
. val all : ('a -> bool) -> 'a map -> bool
-
all pred m
returnstrue
if, and only if,pred v
returnstrue
for all items \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\). val alli : (Key.ord_key * 'a -> bool) -> 'a map -> bool
-
all pred m
returnstrue
if, and only if,pred(key, v)
returnstrue
for all items \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\).
Instances
structure AtomMap
-
This structure is an alias for
AtomRedBlackMap
.
structure AtomBinaryMap
-
Maps over atoms implemented using balanced binary trees. Note that it is recommended that one use the
AtomMap
structure as it provides better performance.
structure AtomRedBlackMap
-
Maps over atoms implemented using red-black trees.
structure IntBinaryMap
-
Maps over ints implemented using balanced binary trees. Note that it is recommended that one use the
IntRedBlackMap
structure as it provides better performance.
structure IntListMap
-
Maps over words implemented using sorted lists. This implementation is fast for small sets, but does not scale well to large sizes.
structure IntRedBlackMap
-
Maps over ints implemented using red-black binary trees.
structure WordRedBlackMap
-
Maps over words implemented using red-black binary trees.