Class: Omnizip::Algorithms::LZMA::XzState

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/algorithms/lzma/xz_state.rb

Overview

XZ Utils-compatible LZMA state machine

Tracks encoding context via 12-state machine to predict optimal probability models for upcoming symbols.

Based on: xz/src/liblzma/lzma/lzma_common.h

Constant Summary collapse

STATE_LIT_LIT =

12 LZMA states (matching XZ Utils exactly)

0
STATE_MATCH_LIT_LIT =

literal after literal

1
STATE_REP_LIT_LIT =

literal after literal after match

2
STATE_SHORTREP_LIT_LIT =

literal after literal after rep

3
STATE_MATCH_LIT =

literal after literal after shortrep

4
STATE_REP_LIT =

literal after match

5
STATE_SHORTREP_LIT =

literal after rep

6
STATE_LIT_MATCH =

literal after shortrep

7
STATE_LIT_LONGREP =

match after literal

8
STATE_LIT_SHORTREP =

longrep after literal

9
STATE_NONLIT_MATCH =

shortrep after literal

10
STATE_NONLIT_REP =

match after non-literal

11
LIT_STATES =

rep after non-literal

7
STATE_NAMES =

State names for debugging

{
  STATE_LIT_LIT => "STATE_LIT_LIT",
  STATE_MATCH_LIT_LIT => "STATE_MATCH_LIT_LIT",
  STATE_REP_LIT_LIT => "STATE_REP_LIT_LIT",
  STATE_SHORTREP_LIT_LIT => "STATE_SHORTREP_LIT_LIT",
  STATE_MATCH_LIT => "STATE_MATCH_LIT",
  STATE_REP_LIT => "STATE_REP_LIT",
  STATE_SHORTREP_LIT => "STATE_SHORTREP_LIT",
  STATE_LIT_MATCH => "STATE_LIT_MATCH",
  STATE_LIT_LONGREP => "STATE_LIT_LONGREP",
  STATE_LIT_SHORTREP => "STATE_LIT_SHORTREP",
  STATE_NONLIT_MATCH => "STATE_NONLIT_MATCH",
  STATE_NONLIT_REP => "STATE_NONLIT_REP",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial = STATE_LIT_LIT) ⇒ XzState

Returns a new instance of XzState.



31
32
33
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 31

def initialize(initial = STATE_LIT_LIT)
  @value = initial
end

Instance Attribute Details

#valueObject

States 0-6 indicate previous was literal



29
30
31
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 29

def value
  @value
end

Instance Method Details

#dupObject

Create a copy of this state



71
72
73
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 71

def dup
  XzState.new(@value)
end

#literal_state?Boolean

Check if previous symbol was literal

Returns:

  • (Boolean)


66
67
68
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 66

def literal_state?
  @value < LIT_STATES
end

#resetObject

Reset to initial state



76
77
78
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 76

def reset
  @value = STATE_LIT_LIT
end

#to_sObject

String representation for debugging



86
87
88
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 86

def to_s
  STATE_NAMES[@value] || "INVALID(#{@value})"
end

#update_literalObject

Update state after encoding literal Matches XZ Utils update_literal() macro



37
38
39
40
41
42
43
44
45
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 37

def update_literal
  @value = if @value <= STATE_SHORTREP_LIT_LIT
             STATE_LIT_LIT
           elsif @value <= STATE_LIT_SHORTREP
             @value - 3
           else
             @value - 6
           end
end

#update_long_repObject

Update state after encoding long rep match Matches XZ Utils update_long_rep() macro



55
56
57
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 55

def update_long_rep
  @value = @value < LIT_STATES ? STATE_LIT_LONGREP : STATE_NONLIT_REP
end

#update_matchObject

Update state after encoding match Matches XZ Utils update_match() macro



49
50
51
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 49

def update_match
  @value = @value < LIT_STATES ? STATE_LIT_MATCH : STATE_NONLIT_MATCH
end

#update_short_repObject

Update state after encoding short rep (1 byte) Matches XZ Utils update_short_rep() macro



61
62
63
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 61

def update_short_rep
  @value = @value < LIT_STATES ? STATE_LIT_SHORTREP : STATE_NONLIT_REP
end

#valid?Boolean

Check if state is valid

Returns:

  • (Boolean)


81
82
83
# File 'lib/omnizip/algorithms/lzma/xz_state.rb', line 81

def valid?
  @value.between?(STATE_LIT_LIT, STATE_NONLIT_REP)
end