Module: Zfp::TypeCoercion

Defined in:
lib/zfp/type_coercion.rb

Constant Summary collapse

RUBY_TO_ZFP_TYPE =
{
  int32:  Zfp::FFI::ZFP_TYPE_INT32,
  int64:  Zfp::FFI::ZFP_TYPE_INT64,
  float:  Zfp::FFI::ZFP_TYPE_FLOAT,
  double: Zfp::FFI::ZFP_TYPE_DOUBLE
}.freeze
PACK_FORMAT =
{
  float:  "e*",
  double: "E*",
  int32:  "l<*",
  int64:  "q<*"
}.freeze
ELEMENT_SIZE =
{
  float:  4,
  double: 8,
  int32:  4,
  int64:  8
}.freeze

Class Method Summary collapse

Class Method Details

.detect_shape(data) ⇒ Object



38
39
40
41
# File 'lib/zfp/type_coercion.rb', line 38

def detect_shape(data)
  return nil unless numo?(data)
  data.shape
end

.detect_type(data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/zfp/type_coercion.rb', line 27

def detect_type(data)
  return nil unless numo?(data)
  case data
  when Numo::SFloat then :float
  when Numo::DFloat then :double
  when Numo::Int32  then :int32
  when Numo::Int64  then :int64
  else raise Zfp::InvalidType, "Unsupported Numo type: #{data.class}"
  end
end

.from_buffer(ptr, type, shape, as_numo) ⇒ Object



51
52
53
# File 'lib/zfp/type_coercion.rb', line 51

def from_buffer(ptr, type, shape, as_numo)
  as_numo ? buffer_to_numo(ptr, type, shape) : buffer_to_array(ptr, type, shape)
end

.numo?(data) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/zfp/type_coercion.rb', line 43

def numo?(data)
  defined?(Numo::NArray) && data.is_a?(Numo::NArray)
end

.to_buffer(data, type) ⇒ Object



47
48
49
# File 'lib/zfp/type_coercion.rb', line 47

def to_buffer(data, type)
  numo?(data) ? numo_to_buffer(data, type) : array_to_buffer(data, type)
end