Class: Omnizip::Implementations::Base::StateMachineBase Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/omnizip/implementations/base/state_machine_base.rb

Overview

This class is abstract.

Subclasses must implement #update!

Abstract base class for LZMA state machines.

Both XZ Utils and 7-Zip use a 12-state finite state machine (FSM) for tracking the encoding context. However, they have different transition tables between states.

The 12 states are:

  • States 0-6: Literal mode (last symbol was a literal)
  • States 7-9: Match mode (last symbol was a match)
  • States 10-11: Short rep match mode (last symbol was a rep match of length 1)

State transitions depend on whether the current symbol is:

  • A literal (byte)
  • A match (length + distance)
  • A repeated match (using rep0-rep3)

Subclasses must provide their specific transition table via the #update! method.

Constant Summary collapse

STATES =

All valid LZMA states (0-11)

(0..11)
LITERAL_MATCHED_STATES =

States where literal encoding should use matched literal mode (i.e., compare with match byte at rep0)

[7, 8, 9, 10, 11].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = 0) ⇒ StateMachineBase

Initialize the state machine.

Parameters:

  • initial_state (Integer) (defaults to: 0)

    Initial state value (default: 0)



59
60
61
62
63
64
65
66
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 59

def initialize(initial_state = 0)
  unless STATES.include?(initial_state)
    raise ArgumentError,
          "Invalid initial state: #{initial_state}, must be 0-11"
  end

  @state = initial_state
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



54
55
56
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 54

def state
  @state
end

Instance Method Details

#literal_mode?Boolean

Check if currently in literal mode.

States 0-6 are considered "literal mode" where the last symbol was a literal byte.

Returns:

  • (Boolean)

    true if in literal mode (states 0-6)



131
132
133
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 131

def literal_mode?
  @state < 7
end

#match_mode?Boolean

Check if currently in match mode.

States 7-11 are considered "match mode" where the last symbol was a match of some kind.

Returns:

  • (Boolean)

    true if in match mode (states 7-11)



141
142
143
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 141

def match_mode?
  @state >= 7
end

#reset!void

This method returns an undefined value.

Reset to initial state.



71
72
73
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 71

def reset!
  @state = 0
end

#update!(is_match, is_short_rep: false) ⇒ void

This method returns an undefined value.

Update state based on encoding decision.

Both XZ Utils and 7-Zip use different transition tables. Subclasses must implement this with their specific table.

Parameters:

  • is_match (Boolean)

    true if last symbol was a match, false if literal

  • is_short_rep (Boolean) (defaults to: false)

    true if last symbol was a short rep (length=1)

Raises:

  • (NotImplementedError)

    Always raised in base class



84
85
86
87
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 84

def update!(is_match, is_short_rep: false)
  raise NotImplementedError,
        "#{self.class} must implement #update!"
end

#update_literal!void

This method returns an undefined value.

Update state for literal encoding.

This is a convenience method that calls #update! with is_match=false.



94
95
96
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 94

def update_literal!
  update!(false)
end

#update_long_rep!void

This method returns an undefined value.

Update state for long rep match (length>1).

This is a convenience method that calls #update! with appropriate params.



121
122
123
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 121

def update_long_rep!
  update!(true, is_short_rep: false)
end

#update_match!(_distance = nil) ⇒ void

This method returns an undefined value.

Update state for match encoding.

This is a convenience method that calls #update! with is_match=true.



103
104
105
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 103

def update_match!(_distance = nil)
  update!(true)
end

#update_short_rep!void

This method returns an undefined value.

Update state for short rep match (length=1).

This is a convenience method that calls #update! with appropriate params.



112
113
114
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 112

def update_short_rep!
  update!(true, is_short_rep: true)
end

#use_matched_literal?Boolean

Check if matched literal encoding should be used.

In states 7-11, literal encoding should compare with the match byte at rep0 for better compression.

Returns:

  • (Boolean)

    true if matched literal should be used



151
152
153
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 151

def use_matched_literal?
  LITERAL_MATCHED_STATES.include?(@state)
end

#valueInteger

Get state as integer.

This is used for probability model indexing.

Returns:

  • (Integer)

    Current state value (0-11)



160
161
162
# File 'lib/omnizip/implementations/base/state_machine_base.rb', line 160

def value
  @state
end