Class: Huffman

Inherits:
Object
  • Object
show all
Includes:
HuffmanTw
Defined in:
lib/huffman_tw.rb

Overview

The Huffman class holds compression methods.

Instance Method Summary collapse

Instance Method Details

#compress(data) ⇒ Object

Compresses given data using the huffman algorithm.

The data has to be of type String

The returned value will be an Array of Integers

An ArgumentError is raised if the data exceeds the maximum size of 2048



28
29
30
31
32
# File 'lib/huffman_tw.rb', line 28

def compress(data)
  raise 'Data has to be a string' if data.class != String

  huff_compress(data, data.size)
end

#decompress(data) ⇒ Object

Decompresses given data using the huffman algorithm.

The data has to be an Array of Inetegers

The returned value will be an Array of Integers

An ArgumentError is raised if the data exceeds the maximum size of 2048



42
43
44
45
46
47
# File 'lib/huffman_tw.rb', line 42

def decompress(data)
  raise 'Data has to be an Array of bytes' if data.class != Array

  data = data.pack('C*')
  huff_decompress(data, data.size)
end