maps

Overview

Module containing methods shared across map implementations.

Summary

makeCreates and returns a new Map, containing the mappings supplied.
addMappingsAdds mappings to this Map and returns Map to allow chaining.
makeSetModifies the methods of the supplied map in place to create an object following the set contract.
orderedEqualsReturns true iff both maps contain the same mappings AND the same iteration order.
toStringReturns a string representation of map.
unorderedEqualsReturns true if both maps have the same size and contain the same mappings.
valIterEquivalent to Map:iter() except that iteration steps return elements in the order val,key instead of key,val.

Detail

Map:make(mappings [, vals])

Creates and returns a new Map, containing the mappings supplied.

input can be one of several types:

  1. A single Lua table {a=1,b=2,c=3,d=4,e=5}
  2. A single iteration which returns two values per iteration step: iter.zip("abcde",iter.count())
  3. Two iterables which both return one value per iteration step - this is a shorthand for 2.
  4. Any table with an iter method which, when called, returns an iteration satisfying 2.

So, the following are all equivalent:
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})

Map:addMappings(mappings, vals)

Adds mappings to this Map and returns Map to allow chaining.

input can be one of several types:

  1. A single Lua table {a=1,b=2,c=3,d=4,e=5}
  2. A single iteration which returns two values per iteration step: iter.zip("abcde",iter.count())
  3. Two iterables which both return one value per iteration step - this is a shorthand for 2.
  4. Any table with an iter method which, when called, returns an iteration satisfying 2.

So, the following are all equivalent:
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})

maps.makeSet(map)

Modifies the methods of the supplied map in place to create an object following the set contract.

This function is used to create the SkipSet and LinkedSet 'classes': SkipSet = maps.makeSet(SkipMap:new()) LinkedSet = maps.makeSet(LinkedMap:new())

maps.orderedEquals(map1, map2)

Returns true iff both maps contain the same mappings AND the same iteration order.

This is typically not the function that would be used for equality comparisons of map. See maps.unorderedEquals.

maps.toString(map)

Returns a string representation of map.

Example:

> x = HashMap:make{a=1,b=2,c=3,d=4}
> print(x)
{a=1, d=4, c=3, b=2}

maps.unorderedEquals(map1, map2)

Returns true if both maps have the same size and contain the same mappings.

Map:valIter()

Equivalent to Map:iter() except that iteration steps return elements in the order val,key instead of key,val.