Class: Omnizip::Algorithms::LZMA::LengthEncoder

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/algorithms/lzma/xz_probability_models.rb

Overview

Length encoder with probability models and price tables

Encodes match lengths using a 3-tier structure:

  • Low: lengths 2-9 (choice=0, 3 bits)
  • Mid: lengths 10-17 (choice=1, choice2=0, 3 bits)
  • High: lengths 18-273 (choice=1, choice2=1, 8 bits)

Constant Summary

Constants included from Constants

Constants::BIT_MODEL_TOTAL, Constants::COMPRESSION_LEVEL_DEFAULT, Constants::COMPRESSION_LEVEL_MAX, Constants::COMPRESSION_LEVEL_MIN, Constants::DICT_SIZE_MAX, Constants::DICT_SIZE_MIN, Constants::DIST_ALIGN_BITS, Constants::DIST_ALIGN_SIZE, Constants::DIST_SLOT_FAST_LIMIT, Constants::END_POS_MODEL_INDEX, Constants::EOS_MARKER, Constants::INIT_PROBS, Constants::LEN_HIGH_SYMBOLS, Constants::LEN_LOW_SYMBOLS, Constants::LEN_MID_SYMBOLS, Constants::LIT_SIZE_MAX, Constants::MATCH_LEN_MAX, Constants::MATCH_LEN_MIN, Constants::MOVE_BITS, Constants::NUM_DIRECT_BITS, Constants::NUM_DIST_SLOTS, Constants::NUM_DIST_SLOT_BITS, Constants::NUM_FULL_DISTANCES, Constants::NUM_LEN_HIGH_BITS, Constants::NUM_LEN_LOW_BITS, Constants::NUM_LEN_MID_BITS, Constants::NUM_LEN_TO_POS_STATES, Constants::NUM_LIT_CONTEXT_BITS_MAX, Constants::NUM_LIT_POS_BITS_MAX, Constants::NUM_POS_BITS_MAX, Constants::NUM_STATES, Constants::POS_STATES_MAX, Constants::START_POS_MODEL_INDEX, Constants::TOP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pb) ⇒ LengthEncoder

Initialize length encoder

Parameters:

  • pb (Integer)

    Number of position bits



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 169

def initialize(pb)
  @pb = pb
  @num_pos_states = 1 << pb

  # Choice bits
  @choice = XzBufferedRangeEncoder::Probability.new
  @choice2 = XzBufferedRangeEncoder::Probability.new

  # Low lengths (per position state)
  @low = Array.new(@num_pos_states) do
    Array.new(LEN_LOW_SYMBOLS) { XzBufferedRangeEncoder::Probability.new }
  end

  # Mid lengths (per position state)
  @mid = Array.new(@num_pos_states) do
    Array.new(LEN_MID_SYMBOLS) { XzBufferedRangeEncoder::Probability.new }
  end

  # High lengths (shared across position states)
  @high = Array.new(LEN_HIGH_SYMBOLS) { XzBufferedRangeEncoder::Probability.new }

  # Price tables (updated incrementally)
  # prices[pos_state][length - MATCH_LEN_MIN]
  @prices = Array.new(@num_pos_states) do
    Array.new(MATCH_LEN_MAX - MATCH_LEN_MIN + 1, 0)
  end

  # Counters for price table updates
  @counters = Array.new(@num_pos_states, 0)
end

Instance Attribute Details

#choiceObject (readonly)

Returns the value of attribute choice.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def choice
  @choice
end

#choice2Object (readonly)

Returns the value of attribute choice2.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def choice2
  @choice2
end

#countersObject (readonly)

Returns the value of attribute counters.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def counters
  @counters
end

#highObject (readonly)

Returns the value of attribute high.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def high
  @high
end

#lowObject (readonly)

Returns the value of attribute low.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def low
  @low
end

#midObject (readonly)

Returns the value of attribute mid.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def mid
  @mid
end

#pricesObject (readonly)

Returns the value of attribute prices.



164
165
166
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 164

def prices
  @prices
end

Instance Method Details

#decrement_counter(pos_state) ⇒ Boolean

Decrement counter for position state

Parameters:

  • pos_state (Integer)

    Position state

Returns:

  • (Boolean)

    True if counter reached zero



243
244
245
246
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 243

def decrement_counter(pos_state)
  @counters[pos_state] -= 1
  @counters[pos_state] <= 0
end

#get_price(pos_state, length) ⇒ Integer

Get price for encoding length at position state

Parameters:

  • pos_state (Integer)

    Position state (0 to 2^pb - 1)

  • length (Integer)

    Match length (2 to 273)

Returns:

  • (Integer)

    Price in price units



226
227
228
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 226

def get_price(pos_state, length)
  @prices[pos_state][length - MATCH_LEN_MIN]
end

#resetObject

Reset all models to initial state



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 201

def reset
  @choice.value = BIT_MODEL_TOTAL >> 1
  @choice2.value = BIT_MODEL_TOTAL >> 1
  @low.each do |models|
    models.each do |prob|
      prob.value = BIT_MODEL_TOTAL >> 1
    end
  end
  @mid.each do |models|
    models.each do |prob|
      prob.value = BIT_MODEL_TOTAL >> 1
    end
  end
  @high.each { |prob| prob.value = BIT_MODEL_TOTAL >> 1 }

  # Reset price tables
  @prices.each { |pos_prices| pos_prices.fill(0) }
  @counters.fill(0)
end

#reset_counter(pos_state, value) ⇒ Object

Reset counter for position state

Parameters:

  • pos_state (Integer)

    Position state

  • value (Integer)

    Counter value



252
253
254
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 252

def reset_counter(pos_state, value)
  @counters[pos_state] = value
end

#set_price(pos_state, length, price) ⇒ Object

Set price for length at position state

Parameters:

  • pos_state (Integer)

    Position state

  • length (Integer)

    Match length

  • price (Integer)

    Price value



235
236
237
# File 'lib/omnizip/algorithms/lzma/xz_probability_models.rb', line 235

def set_price(pos_state, length, price)
  @prices[pos_state][length - MATCH_LEN_MIN] = price
end