class Array2D

Attributes

state[RW]

Public Class Methods

new(rows, columns, value=nil) click to toggle source
# File lib/array_2d.rb, line 5
def initialize(rows, columns, value=nil)
  @state = Array.new(rows) { Array.new(columns) { value } }
end

Public Instance Methods

==(o) click to toggle source
# File lib/array_2d.rb, line 29
def ==(o)
  o.class == self.class && o.state == state
end
[](x, y) click to toggle source
# File lib/array_2d.rb, line 45
def [](x, y)
  case x
  when Integer
    case y
    when Integer
      @state[x][y]
    when Range
      if y.size <= column_size
        subarray = Array.new(y.to_a.size)
        y.each {|yi| subarray[yi - y.first] = @state[x][yi]}
        subarray
      else
        raise IndexError, "Indices are out of range"
      end
    end
  when Range
    case y
    when Integer
      if x.size <= row_size
        subarray = Array.new(x.to_a.size)
        x.each {|xi| subarray[xi - x.first] = @state[xi][y]}
        subarray
      else
        raise IndexError, "Indices are out of range"
      end
    when Range
      if x.size <= row_size && y.size <= column_size
        subarray = Array2D.new(x.to_a.size, y.to_a.size)
        x.each do |xi|
          y.each do |yi|
            subarray.state[xi - x.first][yi - y.first] = @state[xi][yi]
          end
        end
        subarray
      else
        raise IndexError, "Indices are out of range"
      end
    end
  end
end
[]=(x, y, value) click to toggle source
# File lib/array_2d.rb, line 86
def []=(x, y, value)
  case x
  when Integer
    case y
    when Integer
      @state[x][y] = value
    when Range
      if value.is_a?(Array) && y.size == value.size
        y.each {|yi| @state[x][yi] = value[yi - y.first]}
      elsif value.is_a?(Array) && y.size != value.size
        raise AssignmentError, "Value array is not the same size as subarray"
      else
        y.each {|yi| @state[x][yi] = value}
      end    
    end
  when Range
    case y
    when Integer
      if value.is_a?(Array) && x.size == value.size
        x.each {|xi| @state[xi][y] = value[xi - x.first]}
      elsif value.is_a?(Array) && x.size != value.size
        raise AssignmentError, "Value array is not the same size as subarray"
      else
        x.each {|xi| @state[xi][y] = value}
      end
    when Range
      x.each do |xi|
        y.each do |yi|
          if value.is_a?(Array2D) && [x.size, y.size] == value.size
            @state[xi][yi] = value[xi - x.first, yi - y.first]
          elsif value.is_a?(Array2D) && [x.size, y.size] != value.size
            raise AssignmentError, "Value 2d array is not the same size as subarray"
          else
            @state[xi][yi] = value
          end
        end
      end
    end
  end
end
column_size() click to toggle source
# File lib/array_2d.rb, line 41
def column_size
  @state[0].size
end
each() { |e| ... } click to toggle source
# File lib/array_2d.rb, line 9
def each(&block)
  @state.each do |row|
    row.each do |e|
      yield e
    end
  end
end
each_with_index() { |e, [row_index, column_index]| ... } click to toggle source
# File lib/array_2d.rb, line 17
def each_with_index(&block)
  @state.each_with_index do |row, row_index|
    row.each_with_index do |e, column_index|
      yield e, [row_index, column_index]
    end
  end   
end
row_size() click to toggle source
# File lib/array_2d.rb, line 37
def row_size
  @state.size
end
size() click to toggle source
# File lib/array_2d.rb, line 33
def size
  [row_size, column_size]
end
to_s() click to toggle source
# File lib/array_2d.rb, line 25
def to_s
  @state.to_s
end