Sha256: 2e15c6f318e309f090fa71d25f53ad0a0935f06832feca52c4acf6af46a4a39b

Contents?: true

Size: 832 Bytes

Versions: 4

Compression:

Stored size: 832 Bytes

Contents

require 'narray'

class ObjectTable::BasicGrid < Hash
  ARRAY_LIKE = [Array, Range]

#   def self.[](*args)
#     grid = super
#     grid._ensure_uniform_columns!
#   end

  def _ensure_uniform_columns!(rows = nil)
    arrays, scalars = partition{|k, v| ARRAY_LIKE.any?{|cls| v.is_a?(cls)} }
    narrays, scalars = scalars.partition{|k, v| v.is_a?(NArray) }

    unique_rows = arrays.map{|k, v| v.count}
    unique_rows += narrays.map{|k, v| v.shape.last or 0}
    unique_rows = unique_rows.uniq

    if rows
      raise "Differing number of rows: #{unique_rows}" unless unique_rows.empty? or unique_rows == [rows]
    else
      raise "Differing number of rows: #{unique_rows}" if unique_rows.length > 1
      rows = (unique_rows[0] or 1)
    end

    scalars.each do |k, v|
      self[k] = [v] * rows
    end

    self
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
object_table-0.2.4 lib/object_table/basic_grid.rb
object_table-0.2.3 lib/object_table/basic_grid.rb
object_table-0.2.2 lib/object_table/basic_grid.rb
object_table-0.2.0 lib/object_table/basic_grid.rb