Class: DS::TriMatrix

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

Overview

Class implements Triangular Matrix (lower).

Instance Method Summary collapse

Constructor Details

#initialize(init = 0) ⇒ TriMatrix

Creates new triangular matrix.



5
6
7
8
# File 'lib/ds/arrays/tri_matrix.rb', line 5

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

Instance Method Details

#[](x, y) ⇒ Object

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



11
12
13
14
15
# File 'lib/ds/arrays/tri_matrix.rb', line 11

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).



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

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

#sizeObject



29
30
31
# File 'lib/ds/arrays/tri_matrix.rb', line 29

def size
  @max
end

#to_aObject



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

def to_a
  @store
end