Module: Huffify
- Defined in:
- lib/huffify.rb,
lib/huffify/version.rb
Defined Under Namespace
Classes: HuffNode
Constant Summary collapse
- VERSION =
"0.1.3"
Class Method Summary collapse
Class Method Details
.decode(bits, huffman_tree) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/huffify.rb', line 23 def self.decode(bits, huffman_tree) return '' if huffman_tree.nil? return huffman_tree.char * bits.length if huffman_tree.char text = "" cur_node = huffman_tree bits.each_char do |bit| cur_node = bit == '0' ? cur_node.left : cur_node.right if cur_node.char text += cur_node.char cur_node = huffman_tree end end text end |
.encode(text) ⇒ Object
17 18 19 20 21 |
# File 'lib/huffify.rb', line 17 def self.encode(text) huffman_tree = build_huffman_tree(text) codes = get_char_codes(huffman_tree) { encoded_text: text.each_char.map { |char| codes[char] }.join, huffman_tree: huffman_tree } end |