Class: DS::TriMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/matrixes/tri_matrix.rb

Instance Method Summary collapse

Constructor Details

#initialize(init = 0) ⇒ TriMatrix

Creates new triangular matrix.



7
8
9
# File 'lib/ds/matrixes/tri_matrix.rb', line 7

def initialize(init=0)
  @store =ExpandableArray.new(0,init)
end

Instance Method Details

#[](x, y) ⇒ Object

Returns element at index x,y (or y,x).



12
13
14
15
16
# File 'lib/ds/matrixes/tri_matrix.rb', line 12

def [](x,y)
  x,y = y,x if y > x
  index = (x*x+x)/2 +y
  @store[index]
end

#[]=(x, y, v) ⇒ Object

Sets the element at index x,y (or y,x).



19
20
21
22
23
# File 'lib/ds/matrixes/tri_matrix.rb', line 19

def []=(x,y,v)
  x,y = y,x if y>x
  index = (x*x+x)/2 +y
  @store[index] = v
end

#to_aObject



25
26
27
# File 'lib/ds/matrixes/tri_matrix.rb', line 25

def to_a
  @store
end