Sha256: 8504bb153f4b46d39ce793afe7551edfeea60a6def98cfdb26ba8eca1490662c

Contents?: true

Size: 943 Bytes

Versions: 2

Compression:

Stored size: 943 Bytes

Contents

require 'narray'

module ObjectTable::Column

  def self.length_of(array)
    case array
    when Array then array.length
    when NArray then (array.shape.last or 0)
    else nil
    end
  end


  def self.stack(*columns); _stack(columns); end

  def self._stack(columns)
    columns = columns.reject(&:empty?)
    return NArray[] if columns.empty?
    return columns[0].clone if columns.length == 1

    if columns.map{|x| x.shape}.uniq.length == 1
      new_col = NArray.to_na(columns)
      new_col = new_col.reshape(*new_col.shape[0...-2], new_col.shape[-2] * new_col.shape[-1])
      return new_col
    end

    new_rows = columns.map{|x| x.shape[-1]}.reduce(:+)
    first_col = columns.first
    new_col = NArray.new(first_col.typecode, *first_col.shape[0...-1], new_rows)

    columns.reduce(0) do |row, col|
      end_row = row + col.shape[-1]
      new_col[false, row ... end_row] = col
      end_row
    end

    new_col
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
object_table-0.4.1 lib/object_table/column.rb
object_table-0.4.0 lib/object_table/column.rb