Class: Omnizip::Algorithms::Deflate64::HuffmanCoder

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

Overview

Huffman coding for Deflate64

Constant Summary collapse

LENGTH_CODES_MAP =

Length code mapping

{
  3 => 257, 4 => 258, 5 => 259, 6 => 260, 7 => 261,
  8 => 262, 9 => 263, 10 => 264, 11 => 265, 12 => 266,
  13 => 267, 14 => 268, 15 => 269, 16 => 270, 17 => 271,
  18 => 272, 19 => 273, 20 => 274, 21 => 275, 22 => 276,
  23 => 277, 24 => 278, 25 => 279, 26 => 280, 27 => 281,
  28 => 282, 29 => 283, 30 => 284, 31 => 285
}.freeze
DISTANCE_CODES_MAP =

Distance code mapping

(0..29).to_a.freeze

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

#initializeHuffmanCoder

Returns a new instance of HuffmanCoder.



25
26
27
28
# File 'lib/omnizip/algorithms/deflate64/huffman_coder.rb', line 25

def initialize
  @literal_tree = nil
  @distance_tree = nil
end

Instance Attribute Details

#distance_treeObject (readonly)

Returns the value of attribute distance_tree.



23
24
25
# File 'lib/omnizip/algorithms/deflate64/huffman_coder.rb', line 23

def distance_tree
  @distance_tree
end

#literal_treeObject (readonly)

Returns the value of attribute literal_tree.



23
24
25
# File 'lib/omnizip/algorithms/deflate64/huffman_coder.rb', line 23

def literal_tree
  @literal_tree
end

Instance Method Details

#decode(bitstream, literal_tree, distance_tree) ⇒ Array<Hash>

Decode bitstream using Huffman coding

Parameters:

  • bitstream (String)

    Encoded data

  • literal_tree (Hash)

    Literal Huffman tree

  • distance_tree (Hash)

    Distance Huffman tree

Returns:

  • (Array<Hash>)

    Decoded tokens



53
54
55
56
57
58
# File 'lib/omnizip/algorithms/deflate64/huffman_coder.rb', line 53

def decode(bitstream, literal_tree, distance_tree)
  @literal_tree = literal_tree
  @distance_tree = distance_tree

  decode_tokens(bitstream)
end

#encode(tokens) ⇒ String

Encode tokens using Huffman coding

Parameters:

  • tokens (Array<Hash>)

    LZ77 tokens

Returns:

  • (String)

    Encoded bitstream



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/omnizip/algorithms/deflate64/huffman_coder.rb', line 34

def encode(tokens)
  # Build frequency tables
  literal_freqs = build_literal_frequencies(tokens)
  distance_freqs = build_distance_frequencies(tokens)

  # Build Huffman trees
  @literal_tree = build_tree(literal_freqs)
  @distance_tree = build_tree(distance_freqs)

  # Encode tokens
  encode_tokens(tokens)
end