Class: Zfp::Codec

Inherits:
Object
  • Object
show all
Defined in:
lib/zfp/codec.rb

Constant Summary collapse

VALID_TYPES =
%i[float double int32 int64].freeze
VALID_MODES =
%i[fixed_rate fixed_precision fixed_accuracy reversible].freeze

Instance Method Summary collapse

Constructor Details

#initialize(type:, shape:, mode:, numo: false, **params) ⇒ Codec

Returns a new instance of Codec.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/zfp/codec.rb', line 8

def initialize(type:, shape:, mode:, numo: false, **params)
  validate_type!(type)
  validate_mode!(mode)
  validate_shape!(shape)
  validate_params!(mode, params)
  @type   = type
  @shape  = shape
  @mode   = mode
  @params = params
  @numo   = numo
end

Instance Method Details

#compress(data) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/zfp/codec.rb', line 20

def compress(data)
  buf   = TypeCoercion.to_buffer(data, @type)
  field = Field.new(@type, @shape, buf)
  Stream.new(@mode, @params).compress(field)
ensure
  field&.free
end

#decompress(bytes) ⇒ Object



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

def decompress(bytes)
  count     = @shape.reduce(:*)
  elem_size = TypeCoercion::ELEMENT_SIZE[@type]
  out_buf   = ::FFI::MemoryPointer.new(:uint8, count * elem_size)
  field     = Field.new(@type, @shape, out_buf)
  Stream.new(@mode, @params).decompress(field, bytes)
  TypeCoercion.from_buffer(out_buf, @type, @shape, @numo)
ensure
  field&.free
end

#pack(data) ⇒ Object



39
40
41
42
43
44
# File 'lib/zfp/codec.rb', line 39

def pack(data)
  as_numo    = @numo || TypeCoercion.numo?(data)
  compressed = compress(data)
  Packer.encode(compressed, type: @type, shape: @shape, mode: @mode,
                params: @params, numo: as_numo)
end