A Multimap is a map in which each key maps to a collection of values. Multimaps merely provide a few convenience methods for behavior that is achievable with a regular Map.
When add(key, val) is called, Multimap first checks to see if there is already a collection stored against key. If yes, val is added to that collection. If no (val is the very first value to be stored against key), then a new collection is allocated and val is added to that collection. For example:
> m = Multimap:new()
> m:add("R. White","'Tater Salad")
> m:add("R. White", "The 'Tater")
> m:add("G. Pitcher", "The Ace")
> m:addValues("G. Pitcher", {"Ace", "The Kid"})
> print(m)
{G. Pitcher=["The Ace", "Ace", "The Kid"],
R. White =["'Tater Salad", "The 'Tater"]}
Both the map implementation and values collection type can be specified in the Multimap constructor. By default, the map implementation is a HashMap and the values collection is a Vector. But, for instance:
> m = Multimap:new(HashMap, SkipSet)
...
> m:add("R. White", "The 'Tater")
> m:add("R. White", "The 'Tater")
> print(m:get("R. White")) -- The 'Tater only appears once, since the
{"'Tater Salad", "The 'Tater"} -- values collection is a set
Multimaps provide several other convenience methods, see the method documentation.
new | Creates and returns a new Multimap, using the supplied map implementation and values collection. |
make | Creates and returns a new Map, containing the mappings supplied. |
get | Returns the collection of values stored against key, or nil if no mapping exists. |
__eq | Returns true if self and other both have the same set of keys and collections of values. |
add | Adds val to the collection stored against key or to a new collection if no values are yet stored against key. |
allValsIter | Returns an iterator over all the values stored in this map. |
allocate | Allocate an empty collection of values for key. |
decorate | Decorates the supplied map's add method to have multimap behavior. |
get | Returns the collection of values stored against key, or nil if no mapping exists. |
iter | Returns an iterator over this map, or, if key is specified, over the values collection stored against key. |
remove | Remove all values stored against key, or, if value is specified, remove only value in the collection stored against key. |
size | Returns the number of keys in this Multimap. |
test | Unit test. |
valSize | Returns the number of values in this Multimap. |
toString | Returns a string representation of map. |
addMappings | Adds mappings to this Map and returns Map to allow chaining. |
HashMap | Table-based implementation of a map with support for user-defined hash functions and nil values. |
Vector | Table implementation of a general purpose vector. |
containsAll | Returns true iff collection:contains(e) for e in iter(iterable). |
removeAll | Equivalent to calling set:remove(e) for e in iter(elements). |
valIter | Equivalent to Map:iter() except that iteration steps return elements in the order val,key instead of key,val. |
Multimap:new(mapType, valuesCollection)mapType defaults to self.cDefaultMapType, which is initially HashMap, and valuesCollection defaults to self.cDefaultValuesType, which is initally Vector.
Map:make(mappings [, vals])input can be one of several types:
Map:make{a=1,b=2,c=3,d=4,e=5}
== Map:make(iter.zip("abcde",iter.count()))
== Map:make("abcde",iter.count())
== Map:make(Map:make{a=1,b=2,c=3,d=4,e=5})
Multimap:get(key)Synonym: operator () ( so a:get(b) == a(b) ) Examples:
{a=[1,2,3]}:get(a) == [1,2,3]
{a=[1,2,3]}:get(b) == nil
Multimap:__eq(other)
Multimap:add(key, val)Examples:
{a=[1,2]}:add(a,3) results in {a=[1,2,3]}
{}:add(a,3) results in {a=[3]}
Multimap:addValues(key, vals)for v in iter(vals) do self:add(key,v) end
Multimap:allValsIter()Example:
mmap = {a=[1,2,3],b=[4,5,6]}
print( vector(mmap:allValsIter()) )
[1, 2, 3, 4, 5, 6]
Multimap:allocate(key)
Multimap:contains(key)
Multimap:decorate(map, valuesType)After this method is called, map.add(key,val) will first check to see if there is a collection stored against key. If yes, val is added to that collection. If not, val is added to a newly allocated collection of type valuesType and the new collection is stored against key.
It also adds a method, valSize, which returns the total number of values in the map.
Multimap:get(key)Synonym: operator () ( so a:get(b) == a(b) ) Examples:
{a=[1,2,3]}:get(a) == [1,2,3]
{a=[1,2,3]}:get(b) == nil
Multimap:iter([key])If key is specified but maps to nil, an empty iterator is returned.
Multimap:remove(key [,value])
Multimap:size()Multimap:valSize() returns the number of values in this Multimap.
Multimap:test()
Multimap:valSize()Multimap:size() returns the number of keys in this Multimap.
maps.toString(map)Example:
> x = HashMap:make{a=1,b=2,c=3,d=4}
> print(x)
{a=1, d=4, c=3, b=2}
Map:addMappings(mappings, vals)input can be one of several types:
Map:addMappings{a=1,b=2,c=3,d=4,e=5}
== Map:addMappings(iter.zip("abcde",iter.count()))
== Map:addMappings("abcde",iter.count())
== Map:addMappings(Map:addMappings{a=1,b=2,c=3,d=4,e=5})
collections.containsAll(collection, elements)
sets.removeAll(set, elements)
Map:valIter()