Sha256: 2508a59a7c5d7901458258de159b383b8c58dc0906df22a9e493bd363b397429

Contents?: true

Size: 988 Bytes

Versions: 1

Compression:

Stored size: 988 Bytes

Contents

require_relative 'column'

class ObjectTable::MaskedColumn < NArray
  attr_accessor :indices, :parent

  EMPTY = NArray[]

  def self.mask(parent, indices)
    if parent.empty?
      masked = parent.slice(indices)
    else
      masked = parent.slice(false, indices)
    end

    if masked.rank <= 0
      column = new(masked.typecode, 0)
    else
      column = cast(masked)
    end

    column.parent = parent
    column.indices = indices
    column
  end

#   make destructive methods affect parent
  %w{ []= fill! indgen! indgen random! map! collect! conj! imag= mod! add! div! sbt! mul! }.each do |op|
    define_method(op) do |*args, &block|
      result = super(*args, &block)
      parent[false, indices] = self if parent
      result
    end
  end

  def clone
    return NArray.new(typecode, 0) if empty?
    NArray.cast(self).clone
  end

  def coerce_rev(other, operator)
    return other.send(operator, EMPTY) if empty?
    other.send(operator, NArray.cast(self))
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
object_table-0.4.1 lib/object_table/masked_column.rb