Class: DS::Array2D

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

Overview

Implements two dimensional array.

Instance Method Summary collapse

Constructor Details

#initialize(size = 1, init = 0) ⇒ Array2D

Creates new two dimensional array. Init is the default value of the array.



8
9
10
11
# File 'lib/ds/matrixes/array_2d.rb', line 8

def initialize(size=1,init=0)
  @init = init
  @store = Array.new(size){Array.new(size){init}}
end

Instance Method Details

#[](x, y) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/ds/matrixes/array_2d.rb', line 14

def [](x,y)
  if @store[x].nil?
    @store[x] = []
    @init
  elsif @store[x][y].nil?
    @init
  else
    @store[x][y]
  end
end

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



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

def []=(x,y,v)
  @store[x] = [] if @store[x].nil?
  @store[x][y] = v
end

#to_aObject



30
31
32
# File 'lib/ds/matrixes/array_2d.rb', line 30

def to_a
  @store.flatten
end