class BidirectionalHash

Hash that allows lookup by key and value

Public Class Methods

new() click to toggle source
# File lib/cpp_dependency_graph/bidirectional_hash.rb, line 5
def initialize
  @forward = Hash.new { |h, k| h[k] = [] }
  @reverse = Hash.new { |h, k| h[k] = [] }
end

Public Instance Methods

fetch(key) click to toggle source
# File lib/cpp_dependency_graph/bidirectional_hash.rb, line 16
def fetch(key)
  fetch_from(@forward, key)
end
insert(key, value) click to toggle source
# File lib/cpp_dependency_graph/bidirectional_hash.rb, line 10
def insert(key, value)
  @forward[key].push(value)
  @reverse[value].push(key)
  value
end
rfetch(value) click to toggle source
# File lib/cpp_dependency_graph/bidirectional_hash.rb, line 20
def rfetch(value)
  fetch_from(@reverse, value)
end

Protected Instance Methods

fetch_from(hash, key) click to toggle source
# File lib/cpp_dependency_graph/bidirectional_hash.rb, line 26
def fetch_from(hash, key)
  return nil unless hash.key?(key)

  v = hash[key]
  v.length == 1 ? v.first : v.dup
end