module GRCommons::GRCommonUtils

This module provides functionality common to GR and GR3.

Constants

SUPPORTED_TYPES

This constants is used in the test.

Public Instance Methods

create_ffi_pointer(type) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 104
def create_ffi_pointer(type)
  case type
  when Hash
    typ = type.keys[0]
    len = type.values[0]
    Fiddley::MemoryPointer.new(typ, len)
  else
    Fiddley::MemoryPointer.new(type)
  end
end
double(data) click to toggle source

convert Ruby Array or NArray into packed string.

# File lib/gr_commons/gr_common_utils.rb, line 60
def double(data)
  if narray?(data)
    Numo::DFloat.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:double, data.to_a.flatten)
  end
end
equal_length(*args) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 10
def equal_length(*args)
  lengths = args.map(&:length)
  unless lengths.all? { |l| l == lengths[0] }
    raise ArgumentError,
          'Sequences must have same length.'
  end

  lengths[0]
end
float(data) click to toggle source

convert Ruby Array or NArray into packed string.

# File lib/gr_commons/gr_common_utils.rb, line 69
def float(data)
  if narray?(data)
    Numo::SFloat.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:float, data.to_a.flatten)
  end
end
inquiry(types) { |ptr| ... } click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 89
def inquiry(types)
  case types
  when Hash, Symbol
    ptr = create_ffi_pointer(types)
    yield(ptr)
    read_ffi_pointer(ptr, types)
  when Array
    pts = types.map { |type| create_ffi_pointer(type) }
    yield(*pts)
    pts.zip(types).map { |pt, type| read_ffi_pointer(pt, type) }
  else
    raise ArgumentError
  end
end
inquiry_double(&block) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 85
def inquiry_double(&block)
  inquiry(:double, &block)
end
inquiry_int(&block) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 77
def inquiry_int(&block)
  inquiry(:int, &block)
end
inquiry_uint(&block) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 81
def inquiry_uint(&block)
  inquiry(:uint, &block)
end
int(data) click to toggle source

convert Ruby Array or NArray into packed string.

# File lib/gr_commons/gr_common_utils.rb, line 42
def int(data)
  if narray?(data)
    Numo::Int32.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:int32, data.to_a.flatten)
  end
end
narray?(data) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 126
def narray?(data)
  defined?(Numo::NArray) && data.is_a?(Numo::NArray)
end
read_ffi_pointer(pt, type) click to toggle source
# File lib/gr_commons/gr_common_utils.rb, line 115
def read_ffi_pointer(pt, type)
  case type
  when Hash
    typ = type.keys[0]
    len = type.values[0]
    pt.public_send("read_array_of_#{typ}", len)
  else
    pt.public_send("read_#{type}")
  end
end
uint(data) click to toggle source

convert Ruby Array or NArray into packed string.

# File lib/gr_commons/gr_common_utils.rb, line 51
def uint(data)
  if narray?(data)
    Numo::UInt32.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:uint32, data.to_a.flatten)
  end
end
uint16(data) click to toggle source

convert Ruby Array or NArray into packed string.

# File lib/gr_commons/gr_common_utils.rb, line 33
def uint16(data)
  if narray?(data)
    Numo::UInt16.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:uint16, data.to_a.flatten)
  end
end
uint8(data) click to toggle source

convert Ruby Array or NArray into packed string.

# File lib/gr_commons/gr_common_utils.rb, line 24
def uint8(data)
  if narray?(data)
    Numo::UInt8.cast(data).to_binary
  else
    Fiddley::Utils.array2str(:uint8, data.to_a.flatten)
  end
end