Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 159 lines (147 sloc) 4.068 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
require 'multimap'

# NestedMultimap allows values to be assoicated with a nested
# set of keys.
class NestedMultimap < Multimap
  # call-seq:
  # multimap[*keys] = value => value
  # multimap.store(*keys, value) => value
  #
  # Associates the value given by <i>value</i> with multiple key
  # given by <i>keys</i>.
  #
  # map = NestedMultimap.new
  # map["a"] = 100
  # map["a", "b"] = 101
  # map["a"] = 102
  # map #=> {"a"=>{"b"=>[100, 101, 102], default => [100, 102]}}
  def store(*args)
    keys = args
    value = args.pop

    raise ArgumentError, 'wrong number of arguments (1 for 2)' unless value

    if keys.length > 1
      update_container(keys.shift) do |container|
        container = self.class.new(container) unless container.is_a?(self.class)
        container[*keys] = value
        container
      end
    elsif keys.length == 1
      super(keys.first, value)
    else
      self << value
    end
  end
  alias_method :[]=, :store

  # call-seq:
  # multimap << obj => multimap
  #
  # Pushes the given object on to the end of all the containers.
  #
  # map = NestedMultimap["a" => [100], "b" => [200, 300]]
  # map << 300
  # map["a"] #=> [100, 300]
  # map["c"] #=> [300]
  def <<(value)
    @hash.each_value { |container| container << value }
    self.default << value
    self
  end

  # call-seq:
  # multimap[*keys] => value
  # multimap[key1, key2, key3] => value
  #
  # Retrieves the <i>value</i> object corresponding to the
  # <i>*keys</i> object.
  def [](*keys)
    i, l, r, k = 0, keys.length, self, self.class
    while r.is_a?(k)
      r = i < l ? r._internal_hash[keys[i]] : r.default
      i += 1
    end
    r
  end

  # call-seq:
  # multimap.each_association { |key, container| block } => multimap
  #
  # Calls <i>block</i> once for each key/container in <i>map</i>, passing
  # the key and container to the block as parameters.
  #
  # map = NestedMultimap.new
  # map["a"] = 100
  # map["a", "b"] = 101
  # map["a"] = 102
  # map["c"] = 200
  # map.each_association { |key, container| puts "#{key} is #{container}" }
  #
  # <em>produces:</em>
  #
  # ["a", "b"] is [100, 101, 102]
  # "c" is [200]
  def each_association
    super() do |key, container|
      if container.respond_to?(:each_association)
        container.each_association do |nested_key, value|
          yield [key, nested_key].flatten, value
        end
      else
        yield key, container
      end
    end
  end

  # call-seq:
  # multimap.each_container_with_default { |container| block } => map
  #
  # Calls <i>block</i> for every container in <i>map</i> including
  # the default, passing the container as a parameter.
  #
  # map = NestedMultimap.new
  # map["a"] = 100
  # map["a", "b"] = 101
  # map["a"] = 102
  # map.each_container_with_default { |container| puts container }
  #
  # <em>produces:</em>
  #
  # [100, 101, 102]
  # [100, 102]
  # []
  def each_container_with_default(&block)
    @hash.each_value do |container|
      iterate_over_container(container, &block)
    end
    iterate_over_container(default, &block)
    self
  end

  # call-seq:
  # multimap.containers_with_default => array
  #
  # Returns a new array populated with all the containers from
  # <i>map</i> including the default.
  #
  # map = NestedMultimap.new
  # map["a"] = 100
  # map["a", "b"] = 101
  # map["a"] = 102
  # map.containers_with_default #=> [[100, 101, 102], [100, 102], []]
  def containers_with_default
    containers = []
    each_container_with_default { |container| containers << container }
    containers
  end

  def inspect #:nodoc:
    super.gsub(/\}$/, ", default => #{default.inspect}}")
  end

  private
    def iterate_over_container(container)
      if container.respond_to?(:each_container_with_default)
        container.each_container_with_default do |value|
          yield value
        end
      else
        yield container
      end
    end
end

begin
  require 'nested_multimap_ext'
rescue LoadError
end
Something went wrong with that request. Please try again.