Class: Omnizip::Algorithms::Zstandard::FSE::Encoder::BitCStream

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/zstandard/fse/encoder.rb

Overview

Forward bit writer (C BIT_CStream): accumulates at the low end and flushes whole bytes.

Instance Method Summary collapse

Constructor Details

#initializeBitCStream

Returns a new instance of BitCStream.



463
464
465
466
# File 'lib/omnizip/algorithms/zstandard/fse/encoder.rb', line 463

def initialize
  @container = 0
  @bit_pos = 0
end

Instance Method Details

#add_bits(value, nb_bits) ⇒ Object

Parameters:

  • value (Integer)

    bits in the low end

  • nb_bits (Integer)

    0..31



470
471
472
473
474
# File 'lib/omnizip/algorithms/zstandard/fse/encoder.rb', line 470

def add_bits(value, nb_bits)
  mask = (1 << nb_bits) - 1
  @container |= (value & mask) << @bit_pos
  @bit_pos += nb_bits
end

#closeString

Add the 1 end-mark bit and flush everything.

Returns:

  • (String)


489
490
491
492
493
494
# File 'lib/omnizip/algorithms/zstandard/fse/encoder.rb', line 489

def close
  add_bits(1, 1)
  flush
  @bytes << (@container & 0xFF) if @bit_pos.positive?
  @bytes.pack("C*")
end

#flushObject



476
477
478
479
480
481
482
483
484
# File 'lib/omnizip/algorithms/zstandard/fse/encoder.rb', line 476

def flush
  nb_bytes = @bit_pos >> 3
  @bytes ||= []
  nb_bytes.times do |i|
    @bytes << ((@container >> (8 * i)) & 0xFF)
  end
  @container >>= nb_bytes * 8
  @bit_pos &= 7
end