class SomethingLikeThat::TwoDArray

Holds a 2-dimensional numeric array. Unstable. We're holding out for a better numeric array library…

Public Class Methods

new(array) click to toggle source
Calls superclass method
# File lib/something_like_that/two_d_array.rb, line 7
def initialize(array)
  super array
end

Public Instance Methods

lengths() click to toggle source
# File lib/something_like_that/two_d_array.rb, line 24
def lengths
  x = length
  y = map(&:length).max
  min, max = [x, y].minmax
  OpenStruct.new(x: x, y: y, min: min, max: max)
end
maxima() click to toggle source

TODO: rename (incl. corresponding method call in Scorer)

# File lib/something_like_that/two_d_array.rb, line 12
def maxima
  max_vals = Array.new(length)
  double = dup
  max_vals.map.with_index { |_, i| i < lengths.min ? double.pop_max : 0 }
end
pop_max() click to toggle source
# File lib/something_like_that/two_d_array.rb, line 18
def pop_max
  max = flatten.max
  eliminate(coords(max))
  max
end

Private Instance Methods

coords(value) click to toggle source
# File lib/something_like_that/two_d_array.rb, line 38
def coords(value)
  x = index { |row| row.include?(value) }
  y = slice(x).index(value)
  OpenStruct.new(x: x, y: y)
end
eliminate(axes) click to toggle source
# File lib/something_like_that/two_d_array.rb, line 33
def eliminate(axes)
  delete_at(axes.x)
  each { |row| row.delete_at(axes.y) }
end