Class: CAMonOp

Inherits:
Object
  • Object
show all
Defined in:
lib/carray/lazy.rb,
yard-stubs/carray_lazy.rb

Overview


CAMonOp / CALazyMarker surface methods.

Materialise-on-first-touch for Enumerable receivers, MV export reject, inspect/dump_tree summary that does NOT materialise.

Constant Summary collapse

OP_NAMES =

op_id => :name reverse lookup

CArray::LAZY_MONOP_OP_IDS.invert.freeze

Copy and conversion collapse

Instance Method Summary collapse

Instance Method Details

#copy ⇒ CArray

Evaluates the expression and returns the result as a new entity.

If an expression evaluator is registered through CArray.expression_evaluator it is asked first; the ordinary element-wise walk is what happens when it declines, when none is registered, or when the array is small enough that walking is the faster answer.

Returns:

  • (CArray) —

    a newly evaluated entity.



54
# File 'yard-stubs/carray_lazy.rb', line 54

def copy; end

#dump_tree(indent = 0) ⇒ String

Returns an ASCII summary of the lazy expression tree. Does not materialise.

Parameters:

  • indent (Integer) (defaults to: 0) —

    leading indentation depth.

Returns:

  • (String)


594
595
596
597
598
599
600
601
602
603
# File 'lib/carray/lazy.rb', line 594

def dump_tree(indent = 0)
  pad = "  " * indent
  out = +"#{pad}#{op_label}\n"
  out << if parent.is_a?(CAMonOp)
           parent.dump_tree(indent + 1)
         else
           "#{"  " * (indent + 1)}#{parent.class}(#{parent.data_type_name}, #{parent.shape.inspect})\n"
         end
  out
end

#each({ |value| ... }) {|value| ... } ⇒ Object

Materialises self and yields each element of the resulting entity.

Yield Parameters:

  • value (Object)

Returns:

  • (Object)


610
611
612
# File 'lib/carray/lazy.rb', line 610

def each(&block)
  to_ca.each(&block)
end

#inspect ⇒ String

Returns a short summary of the lazy chain without materialising.

Returns:

  • (String)


578
579
580
# File 'lib/carray/lazy.rb', line 578

def inspect
  "#<CAMonOp #{op_label}(#{parent.inspect})>"
end

#sort(*args, **kwargs) ⇒ CArray

Materialises self into a copy and returns entity.sort(*args, **kwargs).

Returns:



625
626
627
# File 'lib/carray/lazy.rb', line 625

def sort(*args, **kwargs)
  copy.sort(*args, **kwargs)
end

#to_a ⇒ Array

Materialises self and returns the resulting Array.

Returns:



617
618
619
# File 'lib/carray/lazy.rb', line 617

def to_a
  to_ca.to_a
end

#to_ca ⇒ CArray

Evaluates the expression and returns the result as a new entity.

A one-operand element-wise operation holds no data of its own, so there is nothing to hand over unevaluated: unlike CArray#to_ca, which returns self, this materialises -- the Ruby Enumerable#to_a / lazy force convention.

The entity is detached from the operands, so writes to it reach nothing. writable: true is therefore refused rather than answered with a result that would swallow them.

Parameters:

  • writable (Boolean) —

    whether the caller needs writes to land back in the source; only false can be satisfied.

Returns:

  • (CArray) —

    a newly evaluated entity.

Raises:

  • (RuntimeError) —

    when writable: true is requested.



43
# File 'yard-stubs/carray_lazy.rb', line 43

def to_ca(writable: false); end

#to_memory_view ⇒ void

bulk-memory-view / Numo / Apache Arrow consumers go through the MV protocol, which requires a contiguous backing buffer. Lazy views have no backing buffer until materialised; raise with a clear hint.

MemoryView is consulted via to_memory_view_internal on the receiver in some setups; the safest cross-cutting hook is the to_memory_view method (defined by carray_memory_view producer). We override to raise. Importers that probe via rb_memory_view_get will hit the producer slot, which we DON'T currently override at C-level — but the MV producer for CAView rejects backing-bufferless views by default; tests below verify both Ruby-level and C-level reject.

This method returns an undefined value.

Not supported: a lazy view has no backing buffer.

Raises:

  • (TypeError) —

    always; call .copy first.

Raises:

  • (TypeError)


646
647
648
649
650
651
# File 'lib/carray/lazy.rb', line 646

def to_memory_view
  raise TypeError,
        "lazy view (CAMonOp) has no backing buffer; call `.copy` " \
        "first to materialise into an entity before exporting via " \
        "MemoryView."
end

#to_s ⇒ String

Alias of #inspect.

Returns:

  • (String)


585
586
587
# File 'lib/carray/lazy.rb', line 585

def to_s
  inspect
end