Class: Fathom::AdjacencyMatrix

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fathom/data/adjacency_matrix.rb

Instance Method Summary collapse

Constructor Details

#initialize(default = 0) ⇒ AdjacencyMatrix

Returns a new instance of AdjacencyMatrix.



6
7
8
9
# File 'lib/fathom/data/adjacency_matrix.rb', line 6

def initialize(default=0)
  @default = default
  @store = Hash.new(default)
end

Instance Method Details

#[](parent, child) ⇒ Object



11
12
13
# File 'lib/fathom/data/adjacency_matrix.rb', line 11

def [](parent, child)
  @store[[parent, child]]
end

#[]=(parent, child, value) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/fathom/data/adjacency_matrix.rb', line 15

def []=(parent, child, value)
  if value == @default
    @store.delete([parent, child]) # preserves a little memory
  else
    @store[[parent, child]] = value
  end
end

#eachObject



23
24
25
# File 'lib/fathom/data/adjacency_matrix.rb', line 23

def each
  @store.each {|k, v| yield(k,v)}
end