Parent

Included Modules

Multimap

Multimap is a generalization of a map or associative array abstract data type in which more than one value may be associated with and returned for a given key.

Example

require 'multimap'
map = Multimap.new
map["a"] = 100
map["b"] = 200
map["a"] = 300
map["a"]                              # -> [100, 300]
map["b"]                              # -> [200]
map.keys                              # -> #<Multiset: {a, a, b}>

Public Class Methods

Multimap[ [key =>|, value]* ] => multimap click to toggle source

Creates a new multimap populated with the given objects.

Multimap["a", 100, "b", 200]       #=> {"a"=>[100], "b"=>[200]}
Multimap["a" => 100, "b" => 200]   #=> {"a"=>[100], "b"=>[200]}
# File lib/rack/mount/vendor/multimap/multimap.rb, line 30
def self.[](*args)
  default = []

  if args.size == 2 && args.last.is_a?(Hash)
    default = args.shift
  elsif !args.first.is_a?(Hash) && args.size % 2 == 1
    default = args.shift
  end

  if args.size == 1 && args.first.is_a?(Hash)
    args[0] = args.first.inject({}) { |hash, (key, value)|
      unless value.is_a?(default.class)
        value = (default.dup << value)
      end
      hash[key] = value
      hash
    }
  else
    index = 0
    args.map! { |value|
      unless index % 2 == 0 || value.is_a?(default.class)
        value = (default.dup << value)
      end
      index += 1
      value
    }
  end

  map = new
  map.instance_variable_set(:@hash, Hash[*args])
  map.default = default
  map
end
new => multimap click to toggle source
new(default) => multimap

Returns a new, empty multimap.

map = Multimap.new(Set.new)
h["a"] = 100
h["b"] = 200
h["a"]           #=> [100].to_set
h["c"]           #=> [].to_set
# File lib/rack/mount/vendor/multimap/multimap.rb, line 75
def initialize(default = [])
  @hash = Hash.new(default)
end

Public Instance Methods

[](key) click to toggle source

Retrieves the value object corresponding to the *keys object.

# File lib/rack/mount/vendor/multimap/multimap.rb, line 91
def [](key)
  @hash[key]
end
[]=(key, value) click to toggle source
Alias for: store
containers => array click to toggle source

Returns a new array populated with the containers from map. See also Multimap#keys and Multimap#values.

map = Multimap["a" => 100, "b" => [200, 300]]
map.containers   #=> [[100], [200, 300]]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 507
def containers
  containers = []
  each_container { |container| containers << container }
  containers
end
delete(key, value) => value click to toggle source
delete(key) => value

Deletes and returns a key-value pair from map. If only key is given, all the values matching that key will be deleted.

map = Multimap["a" => 100, "b" => [200, 300]]
map.delete("b", 300) #=> 300
map.delete("a")      #=> [100]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 126
def delete(key, value = nil)
  if value
    @hash[key].delete(value)
  else
    @hash.delete(key)
  end
end
delete_if {| key, value | block } → map click to toggle source

Deletes every key-value pair from map for which block evaluates to true.

map = Multimap["a" => 100, "b" => [200, 300]]
map.delete_if {|key, value| value >= 300 }
  #=> Multimap["a" => 100, "b" => 200]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 315
def delete_if
  each_association do |key, container|
    container.delete_if do |value|
      yield [key, value]
    end
  end
  self
end
each { |key, value| block } => map click to toggle source

Calls block for each key/value pair in map, passing the key and value to the block as a two-element array.

map = Multimap["a" => 100, "b" => [200, 300]]
map.each { |key, value| puts "#{key} is #{value}" }

produces:

a is 100
b is 200
b is 300
# File lib/rack/mount/vendor/multimap/multimap.rb, line 148
def each
  each_pair do |key, value|
    yield [key, value]
  end
end
each_association { |key, container| block } => map click to toggle source

Calls block once for each key/container in map, passing the key and container to the block as parameters.

map = Multimap["a" => 100, "b" => [200, 300]]
map.each_association { |key, container| puts "#{key} is #{container}" }

produces:

a is [100]
b is [200, 300]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 167
def each_association(&block)
  @hash.each_pair(&block)
end
each_container { |container| block } => map click to toggle source

Calls block for each container in map, passing the container as a parameter.

map = Multimap["a" => 100, "b" => [200, 300]]
map.each_container { |container| puts container }

produces:

[100]
[200, 300]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 184
def each_container
  each_association do |_, container|
    yield container
  end
end
each_key { |key| block } => map click to toggle source

Calls block for each key in hsh, passing the key as a parameter.

map = Multimap["a" => 100, "b" => [200, 300]]
map.each_key { |key| puts key }

produces:

a
b
b
# File lib/rack/mount/vendor/multimap/multimap.rb, line 204
def each_key
  each_pair do |key, _|
    yield key
  end
end
each_pair { |key_value_array| block } => map click to toggle source

Calls block for each key/value pair in map, passing the key and value as parameters.

map = Multimap["a" => 100, "b" => [200, 300]]
map.each_pair { |key, value| puts "#{key} is #{value}" }

produces:

a is 100
b is 200
b is 300
# File lib/rack/mount/vendor/multimap/multimap.rb, line 224
def each_pair
  each_association do |key, values|
    values.each do |value|
      yield key, value
    end
  end
end
each_value { |value| block } => map click to toggle source

Calls block for each key in map, passing the value as a parameter.

map = Multimap["a" => 100, "b" => [200, 300]]
map.each_value { |value| puts value }

produces:

100
200
300
# File lib/rack/mount/vendor/multimap/multimap.rb, line 246
def each_value
  each_pair do |_, value|
    yield value
  end
end
has_value?(value) => true or false click to toggle source
value?(value) => true or false

Returns true if the given value is present for any key in map.

map = Multimap["a" => 100, "b" => [200, 300]]
map.has_value?(300)   #=> true
map.has_value?(999)   #=> false
# File lib/rack/mount/vendor/multimap/multimap.rb, line 286
def has_value?(value)
  values.include?(value)
end
Also aliased as: value?
include?(key) click to toggle source

Returns true if the given key is present in Multimap.

# File lib/rack/mount/vendor/multimap/multimap.rb, line 398
def include?(key)
  keys.include?(key)
end
Also aliased as: member?
index(value) => key click to toggle source

Returns the key for a given value. If not found, returns nil.

map = Multimap["a" => 100, "b" => [200, 300]]
map.index(100)   #=> "a"
map.index(200)   #=> "b"
map.index(999)   #=> nil
# File lib/rack/mount/vendor/multimap/multimap.rb, line 301
def index(value)
  invert[value]
end
invert => multimap click to toggle source

Returns a new multimap created by using map’s values as keys, and the keys as values.

map = Multimap["n" => 100, "m" => 100, "d" => [200, 300]]
map.invert #=> Multimap[100 => ["n", "m"], 200 => "d", 300 => "d"]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 377
def invert
  h = self.class.new(default.dup)
  each_pair { |key, value| h[value] = key }
  h
end
keys => multiset click to toggle source

Returns a new Multiset populated with the keys from this hash. See also Multimap#values and Multimap#containers.

map = Multimap["a" => 100, "b" => [200, 300], "c" => 400]
map.keys   #=> Multiset.new(["a", "b", "b", "c"])
# File lib/rack/mount/vendor/multimap/multimap.rb, line 391
def keys
  keys = Multiset.new
  each_key { |key| keys << key }
  keys
end
length() click to toggle source
Alias for: size
member?(key) click to toggle source
Alias for: include?
merge(other_map) => multimap click to toggle source

Returns a new multimap containing the contents of other_map and the contents of map.

map1 = Multimap["a" => 100, "b" => 200]
map2 = Multimap["a" => 254, "c" => 300]
map2.merge(map2) #=> Multimap["a" => 100, "b" => [200, 254], "c" => 300]
map1             #=> Multimap["a" => 100, "b" => 200]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 428
def merge(other)
  dup.update(other)
end
merge!(other) click to toggle source
Alias for: update
reject {| key, value | block } → map click to toggle source

Same as Multimap#delete_if, but works on (and returns) a copy of the map. Equivalent to map.dup.delete_if.

# File lib/rack/mount/vendor/multimap/multimap.rb, line 331
def reject(&block)
  dup.delete_if(&block)
end
reject! {| key, value | block } → map or nil click to toggle source

Equivalent to Multimap#delete_if, but returns nil if no changes were made.

# File lib/rack/mount/vendor/multimap/multimap.rb, line 341
def reject!(&block)
  old_size = size
  delete_if(&block)
  old_size == size ? nil : self
end
replace(other_map) => map click to toggle source

Replaces the contents of map with the contents of other_map.

map = Multimap["a" => 100, "b" => 200]
map.replace({ "c" => 300, "d" => 400 })
#=> Multimap["c" => 300, "d" => 400]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 356
def replace(other)
  case other
  when Array
    @hash.replace(self.class[self.default, *other])
  when Hash
    @hash.replace(self.class[self.default, other])
  when self.class
    @hash.replace(other)
  else
    raise ArgumentError
  end
end
select { |key, value| block } => multimap click to toggle source

Returns a new Multimap consisting of the pairs for which the block returns true.

map = Multimap["a" => 100, "b" => 200, "c" => 300]
map.select { |k,v| k > "a" }  #=> Multimap["b" => 200, "c" => 300]
map.select { |k,v| v < 200 }  #=> Multimap["a" => 100]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 465
def select
  inject(self.class.new) { |map, (key, value)|
    map[key] = value if yield([key, value])
    map
  }
end
length => fixnum click to toggle source
size => fixnum

Returns the number of key-value pairs in the map.

map = Multimap["a" => 100, "b" => [200, 300], "c" => 400]
map.length        #=> 4
map.delete("a")   #=> 100
map.length        #=> 3
# File lib/rack/mount/vendor/multimap/multimap.rb, line 413
def size
  values.size
end
Also aliased as: length
map[key] = value => value click to toggle source
store(key, value) => value

Associates the value given by value with the key given by key. Unlike a regular hash, multiple can be assoicated with the same value.

map = Multimap["a" => 100, "b" => 200]
map["a"] = 9
map["c"] = 4
map   #=> {"a" => [100, 9], "b" => [200], "c" => [4]}
# File lib/rack/mount/vendor/multimap/multimap.rb, line 107
def store(key, value)
  update_container(key) do |container|
    container << value
    container
  end
end
Also aliased as: []=
to_a => array click to toggle source

Converts map to a nested array of [key, value] arrays.

map = Multimap["a" => 100, "b" => [200, 300], "c" => 400]
map.to_a   #=> [["a", 100], ["b", 200], ["b", 300], ["c", 400]]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 480
def to_a
  ary = []
  each_pair do |key, value|
    ary << [key, value]
  end
  ary
end
to_hash => hash click to toggle source

Converts map to a basic hash.

map = Multimap["a" => 100, "b" => [200, 300]]
map.to_hash   #=> { "a" => [100], "b" => [200, 300] }
# File lib/rack/mount/vendor/multimap/multimap.rb, line 495
def to_hash
  @hash.dup
end
merge!(other_map) => multimap click to toggle source
update(other_map) => multimap

Adds each pair from other_map to map.

map1 = Multimap["a" => 100, "b" => 200]
map2 = Multimap["b" => 254, "c" => 300]

map1.merge!(map2)
#=> Multimap["a" => 100, "b" => [200, 254], "c" => 300]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 443
def update(other)
  case other
  when self.class
    other.each_pair { |key, value| store(key, value) }
  when Hash
    update(self.class[self.default, other])
  else
    raise ArgumentError
  end
  self
end
Also aliased as: merge!
value?(value) click to toggle source
Alias for: has_value?
values => array click to toggle source

Returns a new array populated with the values from map. See also Multimap#keys and Multimap#containers.

map = Multimap["a" => 100, "b" => [200, 300]]
map.values   #=> [100, 200, 300]
# File lib/rack/mount/vendor/multimap/multimap.rb, line 521
def values
  values = []
  each_value { |value| values << value }
  values
end
values_at(*keys) click to toggle source

Return an array containing the values associated with the given keys.

# File lib/rack/mount/vendor/multimap/multimap.rb, line 528
def values_at(*keys)
  @hash.values_at(*keys)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.