Class: SameSame::SymmetricalMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/same_same/symmetrical_matrix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size, lookup_proc = nil) ⇒ SymmetricalMatrix

Returns a new instance of SymmetricalMatrix.



5
6
7
8
9
# File 'lib/same_same/symmetrical_matrix.rb', line 5

def initialize( size, lookup_proc = nil )
  @size = size
  @lookup_proc = lookup_proc
  @matrix = []
end

Instance Attribute Details

#lookup_procObject (readonly)

Returns the value of attribute lookup_proc.



3
4
5
# File 'lib/same_same/symmetrical_matrix.rb', line 3

def lookup_proc
  @lookup_proc
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/same_same/symmetrical_matrix.rb', line 3

def size
  @size
end

Instance Method Details

#lookup(x, y) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/same_same/symmetrical_matrix.rb', line 16

def lookup(x,y)
  check_bounds(x,y)
  if matrix[x * size + y].nil?
    if block_given?
      set(x,y, yield(x,y))
    elsif lookup_proc
      set(x,y, lookup_proc.call(x,y))
    end
  end
  matrix[x * size + y]
end

#set(x, y, v) ⇒ Object



11
12
13
14
# File 'lib/same_same/symmetrical_matrix.rb', line 11

def set(x,y,v)
  check_bounds(x,y)
  matrix[x * size + y] = matrix[y * size + x] = v
end