Class: BidirectionalHash
- Inherits:
-
Object
- Object
- BidirectionalHash
- Defined in:
- lib/cpp_dependency_graph/bidirectional_hash.rb
Overview
Hash that allows lookup by key and value
Instance Method Summary collapse
- #fetch(key) ⇒ Object
-
#initialize ⇒ BidirectionalHash
constructor
A new instance of BidirectionalHash.
- #insert(key, value) ⇒ Object
- #rfetch(value) ⇒ Object
Constructor Details
#initialize ⇒ BidirectionalHash
Returns a new instance of BidirectionalHash.
5 6 7 8 |
# 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 |
Instance Method Details
#fetch(key) ⇒ Object
16 17 18 |
# File 'lib/cpp_dependency_graph/bidirectional_hash.rb', line 16 def fetch(key) fetch_from(@forward, key) end |
#insert(key, value) ⇒ Object
10 11 12 13 14 |
# 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) ⇒ Object
20 21 22 |
# File 'lib/cpp_dependency_graph/bidirectional_hash.rb', line 20 def rfetch(value) fetch_from(@reverse, value) end |