Class: Omnizip::Algorithms::PPMd7::Context
- Inherits:
-
Object
- Object
- Omnizip::Algorithms::PPMd7::Context
- Includes:
- Constants
- Defined in:
- lib/omnizip/algorithms/ppmd7/context.rb
Overview
Represents a context node in the PPMd7 model
A context in PPMd is a sequence of symbols that have appeared previously. Each context tracks the symbols that have followed it and their frequencies. This forms a tree structure where each node represents a different context.
The context uses symbol states to track:
- Which symbols have appeared after this context
- How frequently each symbol has appeared
- Escape probabilities for unknown symbols
Direct Known Subclasses
Omnizip::Algorithms::PPMd8::Context, Formats::Rar::Compression::PPMd::Context
Constant Summary
Constants included from Constants
Omnizip::Algorithms::PPMd7::Constants::ALPHABET_SIZE, Omnizip::Algorithms::PPMd7::Constants::BIN_SCALE, Omnizip::Algorithms::PPMd7::Constants::BOT_VALUE, Omnizip::Algorithms::PPMd7::Constants::DEFAULT_MEM_SIZE, Omnizip::Algorithms::PPMd7::Constants::DEFAULT_ORDER, Omnizip::Algorithms::PPMd7::Constants::INIT_ESCAPE_FREQ, Omnizip::Algorithms::PPMd7::Constants::INTERVAL, Omnizip::Algorithms::PPMd7::Constants::INT_BITS, Omnizip::Algorithms::PPMd7::Constants::MAX_FREQ, Omnizip::Algorithms::PPMd7::Constants::MAX_MEM_SIZE, Omnizip::Algorithms::PPMd7::Constants::MAX_ORDER, Omnizip::Algorithms::PPMd7::Constants::MAX_STATES, Omnizip::Algorithms::PPMd7::Constants::MIN_MEM_SIZE, Omnizip::Algorithms::PPMd7::Constants::MIN_ORDER, Omnizip::Algorithms::PPMd7::Constants::PERIOD_BITS, Omnizip::Algorithms::PPMd7::Constants::PROB_TOTAL, Omnizip::Algorithms::PPMd7::Constants::SEE_CONTEXTS, Omnizip::Algorithms::PPMd7::Constants::SUFFIX_CONTEXTS, Omnizip::Algorithms::PPMd7::Constants::TOP_VALUE, Omnizip::Algorithms::PPMd7::Constants::UNIT_ALLOC_SIZE, Omnizip::Algorithms::PPMd7::Constants::UNIT_SIZE
Instance Attribute Summary collapse
-
#escape_freq ⇒ Object
Returns the value of attribute escape_freq.
-
#order ⇒ Object
readonly
Returns the value of attribute order.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#sum_freq ⇒ Object
readonly
Returns the value of attribute sum_freq.
Instance Method Summary collapse
-
#add_symbol(symbol, freq = 1) ⇒ SymbolState
Add a new symbol to this context.
-
#find_symbol(symbol) ⇒ SymbolState?
Find a symbol state in this context.
-
#initialize(order, suffix = nil) ⇒ Context
constructor
Initialize a new context.
-
#needs_escape? ⇒ Boolean
Check if context needs escape (has unseen symbols).
-
#num_symbols ⇒ Integer
Get the number of distinct symbols in this context.
-
#root? ⇒ Boolean
Check if this is a root context.
-
#symbols_by_frequency ⇒ Array<Integer>
Get all symbols in order of frequency.
-
#total_freq ⇒ Integer
Get total frequency including escape.
-
#update_symbol(symbol, increment = 1) ⇒ void
Update symbol frequency after encoding/decoding.
Constructor Details
#initialize(order, suffix = nil) ⇒ Context
Initialize a new context
47 48 49 50 51 52 53 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 47 def initialize(order, suffix = nil) @order = order @suffix = suffix @states = {} # symbol => SymbolState @sum_freq = 0 @escape_freq = INIT_ESCAPE_FREQ end |
Instance Attribute Details
#escape_freq ⇒ Object
Returns the value of attribute escape_freq.
41 42 43 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 41 def escape_freq @escape_freq end |
#order ⇒ Object (readonly)
Returns the value of attribute order.
40 41 42 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40 def order @order end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
40 41 42 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40 def states @states end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
40 41 42 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40 def suffix @suffix end |
#sum_freq ⇒ Object (readonly)
Returns the value of attribute sum_freq.
40 41 42 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 40 def sum_freq @sum_freq end |
Instance Method Details
#add_symbol(symbol, freq = 1) ⇒ SymbolState
Add a new symbol to this context
This is called when a symbol appears for the first time after this context.
71 72 73 74 75 76 77 78 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 71 def add_symbol(symbol, freq = 1) raise ArgumentError, "Symbol already exists" if @states[symbol] state = SymbolState.new(symbol, freq) @states[symbol] = state @sum_freq += freq state end |
#find_symbol(symbol) ⇒ SymbolState?
Find a symbol state in this context
59 60 61 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 59 def find_symbol(symbol) @states[symbol] end |
#needs_escape? ⇒ Boolean
Check if context needs escape (has unseen symbols)
107 108 109 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 107 def needs_escape? @states.size < ALPHABET_SIZE end |
#num_symbols ⇒ Integer
Get the number of distinct symbols in this context
128 129 130 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 128 def num_symbols @states.size end |
#root? ⇒ Boolean
Check if this is a root context
121 122 123 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 121 def root? @suffix.nil? end |
#symbols_by_frequency ⇒ Array<Integer>
Get all symbols in order of frequency
114 115 116 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 114 def symbols_by_frequency @states.values.sort_by { |s| -s.freq }.map(&:symbol) end |
#total_freq ⇒ Integer
Get total frequency including escape
100 101 102 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 100 def total_freq @sum_freq + @escape_freq end |
#update_symbol(symbol, increment = 1) ⇒ void
This method returns an undefined value.
Update symbol frequency after encoding/decoding
Increases the frequency count for the symbol and updates the total frequency sum for this context.
88 89 90 91 92 93 94 95 |
# File 'lib/omnizip/algorithms/ppmd7/context.rb', line 88 def update_symbol(symbol, increment = 1) state = @states[symbol] return unless state state.freq += increment @sum_freq += increment rescale_frequencies if @sum_freq > MAX_FREQ end |