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_keyvalues to'bvalues. val empty : 'a map-
The empty map.
val isEmpty : 'a map -> bool-
isEmpty mreturns true if, and only if,mis empty. val singleton : (Key.ord_key * 'a) -> 'a map-
singleton (key, v)creates the singleton map that mapskeytov. val insert : 'a map * Key.ord_key * 'a -> 'a map-
insert (m, key, v)adds the mapping fromkeytovtom. This mapping overrides any previous mapping fromkey. val insert' : ((Key.ord_key * 'a) * 'a map) -> 'a map-
insert' ((key, v), map)adds the mapping fromkeytovtom. 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 fromkeytovaluetom, wherevalue = comb(v', v), ifmalready contained a mapping fromkeytov'; 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 fromkeytovaluetom, wherevalue = comb(key, v', v), ifmalready contained a mapping fromkeytov'; otherwise,value = v. val find : 'a map * Key.ord_key -> 'a option-
find (m, key)returnsSOME v, ifmmapskeytovandNONEotherwise. val lookup : 'a map * Key.ord_key -> 'a-
lookup (m, key)returnsv, ifmmapskeytov; otherwise it raises the exceptionNotFound. val inDomain : ('a map * Key.ord_key) -> bool-
inDomain (m, key)returnstrueifkeyis in the domain ofm. val remove : 'a map * Key.ord_key -> 'a map * 'a-
remove (m, key)returns the pair(m', v), ifmmapskeytovand wherem'ismwithkeyremoved from its domain. Ifkeyis 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), ifmmapskeytovand wherem'ismwithkeyremoved from its domain. Ifkeyis not in the domain ofm, then it returnsNONE. val first : 'a map -> 'a option-
first mreturnsSOME itemwhenitemis the value associated with the first (or smallest) key in the domain of the mapm. It returnsNONEwhen the map is empty. val firsti : 'a map -> (Key.ord_key * 'a) option-
first mreturnsSOME(key, item)whenkeyis the first (or smallest) key in the domain of the mapmandkeymaps toitem. It returnsNONEwhen the map is empty. val numItems : 'a map -> int-
numItems mreturns the size ofm's domain. val listItems : 'a map -> 'a list-
listItems mreturns 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 mreturns a list of the key-value pairs inm. val listKeys : 'a map -> Key.ord_key list-
listKeys mreturns 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 allxin 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, wherecmpVis 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)returnstrueif the domain ofm2is a subset of the domain ofm1and if, for allxin 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 functioncombto 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 functioncombto 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 functioncombto 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 functioncombto 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 functioncombas 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), whereoptV1isSOME vif \((\mathtt{key}, \mathtt{v}) \in \mathtt{m1}\) and isNONEif 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
mergeWithfunction is a generalization of theunionWithandintersectionWithfunctions. 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 functioncombas a decision procedure for adding elements to the new map. The difference between this function andmergeWithis that thecombfunction takes thekeyvalue in addition to the optional values from the range. val app : ('a -> unit) -> 'a map -> unit-
app f mapplies the functionfto the values in the range ofm. val appi : ((Key.ord_key * 'a) -> unit) -> 'a map -> unit-
appi f mapapplies the functionfto the key-value pairs that definem. val map : ('a -> 'b) -> 'a map -> 'b map-
map f mcreates a new finite mapm'by applying the functionfto 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 mcreates a new finite mapm'by applying the functionfto 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 mfolds the functionfover the range ofmusinginitas 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 mfolds the functionfover the key-value pairs inmusinginitas 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 mfolds the functionfover the range ofmusinginitas 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 mfolds the functionfover the key-value pairs inmusinginitas the initial value. Items are processed in decreasing order of their key values. val filter : ('a -> bool) -> 'a map -> 'a map-
filter pred mfilters out those items(key, v)fromm, such thatpred vreturnsfalse. 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 mfilters 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 mmaps the partial functionfover the items ofm. More formally, this expression returns the map
val mapPartiali : (Key.ord_key * 'a -> 'b option) -> 'a map -> 'b map-
mapPartiali f mmaps the partial functionfover the items ofm. More formally, this expression returns the map
val exists : ('a -> bool) -> 'a map -> bool-
exists pred mreturnstrueif, and only if, there exists an item \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\), such thatpred vreturnstrue. val existsi : (Key.ord_key * 'a -> bool) -> 'a map -> bool-
exists pred mreturnstrueif, 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 mreturnstrueif, and only if,pred vreturnstruefor all items \((\mathtt{key}, \mathtt{v}) \in \mathtt{m}\). val alli : (Key.ord_key * 'a -> bool) -> 'a map -> bool-
all pred mreturnstrueif, and only if,pred(key, v)returnstruefor 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
AtomMapstructure 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
IntRedBlackMapstructure 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.