Sha256: eb439b66ba23fd835381121bc3560d58c8e872642a196380f1679aef9661a120

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require 'ndtypes/errors'

begin
  require "ruby_ndtypes.so"
rescue LoadError
  require 'ruby_ndtypes/ruby_ndtypes.so'
end

class NDTypes
  Struct.new("ApplySpec", :flags, :outer_dims, :nin, :nout, :nargs, :types)
  ApplySpec = Struct::ApplySpec
  
  # It so happens that over riding the .new method in a super class also
  # tampers with the default .new method for class that inherit from the
  # super class (Index in this case). Thus we first alias the original
  # new method (from Object) to __new__ when the Index class is evaluated,
  # and then we use an inherited hook such that the old new method (from
  # Object) is once again the default .new for the subclass.
  # Refer http://blog.sidu.in/2007/12/rubys-new-as-factory.html
  class << self
    alias :__new__ :new

    def inherited subclass
      class << subclass
        alias :new :__new__
      end
    end
  end
  
  def dup
    str = self.serialize
    NDT.deserialize str
  end

  # We over-ride the .new method so that even sending an NDTypes object can
  # will allow the .new method to act as copy constructor and simply return
  # copy of the argument.
  def self.new *args, &block
    type = args.first

    return type.dup if type.is_a?(NDTypes)

    allocate.tap { |i| i.send :initialize, *args, &block }
  end

  def at n,  dtype: nil
    _at(n, dtype)
  end

  def apply in_types, out: nil
    _apply(in_types, out)
  end

  def inspect
    str = "#<NDTypes:#{object_id}>\n"
    str += "\t" + to_s
    str
  end
end

NDT = NDTypes

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ndtypes-0.2.0dev8 lib/ndtypes.rb