Class: Omnizip::Algorithms::Deflate64::LZ77Encoder

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

Overview

LZ77 encoder with 64KB sliding window for Deflate64

Constant Summary

Constants included from Constants

Constants::BLOCK_TYPE_DYNAMIC, Constants::BLOCK_TYPE_FIXED, Constants::BLOCK_TYPE_STORED, Constants::DICTIONARY_SIZE, Constants::DISTANCE_CODES, Constants::END_OF_BLOCK, Constants::GOOD_MATCH, Constants::HASH_SHIFT, Constants::HASH_SIZE, Constants::LENGTH_CODES, Constants::LITERAL_CODES, Constants::MAX_CHAIN_LENGTH, Constants::MAX_DISTANCE, Constants::MAX_DISTANCE_CODE_LENGTH, Constants::MAX_LAZY_MATCH, Constants::MAX_LITERAL_CODE_LENGTH, Constants::MAX_MATCH_LENGTH, Constants::MIN_MATCH_LENGTH, Constants::NICE_MATCH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window_size = DICTIONARY_SIZE) ⇒ LZ77Encoder

Returns a new instance of LZ77Encoder.



12
13
14
15
16
17
# File 'lib/omnizip/algorithms/deflate64/lz77_encoder.rb', line 12

def initialize(window_size = DICTIONARY_SIZE)
  @window_size = window_size
  @window = []
  @hash_table = {}
  @position = 0
end

Instance Attribute Details

#window_sizeObject (readonly)

Returns the value of attribute window_size.



10
11
12
# File 'lib/omnizip/algorithms/deflate64/lz77_encoder.rb', line 10

def window_size
  @window_size
end

Instance Method Details

#find_matches(data) ⇒ Array<Hash>

Find matches in data and return array of literals and match tokens

Parameters:

  • data (String)

    Input data to compress

Returns:

  • (Array<Hash>)

    Array of match tokens



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/omnizip/algorithms/deflate64/lz77_encoder.rb', line 23

def find_matches(data)
  tokens = []
  pos = 0

  while pos < data.bytesize
    match = find_longest_match(pos, data)

    if match && match[:length] >= MIN_MATCH_LENGTH
      tokens << {
        type: :match,
        length: match[:length],
        distance: match[:distance],
      }
      pos += match[:length]
    else
      tokens << {
        type: :literal,
        value: data.getbyte(pos),
      }
      pos += 1
    end

    update_window(data, pos)
  end

  tokens
end