huffman_coding 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -4
- data/README.md +20 -0
- data/lib/huffman_coding.rb +17 -5
- data/lib/huffman_coding/version.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69c40c3a094b970779e30fcbfe69d35c7a4cf809c3c48e02c35f19b83b48f0f8
|
4
|
+
data.tar.gz: f07d0502b4fd076ce2bdd80bccf0859cd9efba9f3e33f969f7785ae282fded57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 986342ab4c5e9b97cbe5fcabd61565cc3495c1a234812afb4fab4e1d1f68b0bf6e90c3e6d596185e66a62f1509f1ce2e0039667cb16d2d896c5b279406d6398a
|
7
|
+
data.tar.gz: 793b94fae62acff621660ff6b0274ccff7cff513cb1b8936d82122840dc3fc82e2ac4b5100a74231aef1b15b43373ed6910236a7b871b75acdcdcc61f24ce035
|
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
|
-
## Change Log
|
2
|
-
|
3
|
-
### v0.0.1 2020/05/16
|
4
|
-
- [#
|
1
|
+
## Change Log
|
2
|
+
|
3
|
+
### [v1.0.0](https://github.com/khiav223577/huffman_coding/compare/v0.0.1...v1.0.0) 2020/05/16
|
4
|
+
- [#3](https://github.com/khiav223577/huffman_coding/pull/3) add frozen_string_literal (@khiav223577)
|
5
|
+
- [#2](https://github.com/khiav223577/huffman_coding/pull/2) Doesn't need activerecord in runtime (@khiav223577)
|
6
|
+
|
7
|
+
### v0.0.1 2020/05/16
|
8
|
+
- [#1](https://github.com/khiav223577/huffman_coding/pull/1) First implementation (@khiav223577)
|
data/README.md
CHANGED
@@ -25,6 +25,26 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
## Usage
|
27
27
|
|
28
|
+
### Encode
|
29
|
+
```rb
|
30
|
+
coding = HuffmanCoding.encode('A short test.'.each_char)
|
31
|
+
coding.binary
|
32
|
+
# => "6\x88BwV"
|
33
|
+
|
34
|
+
coding.last_byte_bits
|
35
|
+
# => 8
|
36
|
+
|
37
|
+
coding.code_table
|
38
|
+
# => {"e"=>"111", "."=>"110", "t"=>"10", " "=>"011", "s"=>"010", "A"=>"0011", "h"=>"0010", "o"=>"0001", "r"=>"0000"}
|
39
|
+
```
|
40
|
+
|
41
|
+
### Decode
|
42
|
+
```rb
|
43
|
+
coding = HuffmanCoding.new("6\x88BwV", 8 "e"=>"111", "."=>"110", "t"=>"10", " "=>"011", "s"=>"010", "A"=>"0011", "h"=>"0010", "o"=>"0001", "r"=>"0000")
|
44
|
+
HuffmanCoding.decode(coding)
|
45
|
+
# => 'A short test.'
|
46
|
+
```
|
47
|
+
|
28
48
|
|
29
49
|
## Development
|
30
50
|
|
data/lib/huffman_coding.rb
CHANGED
@@ -5,6 +5,18 @@ require 'huffman_coding/level_nodes'
|
|
5
5
|
require 'huffman_coding/node'
|
6
6
|
require 'huffman_coding/utils/tally'
|
7
7
|
|
8
|
+
class HuffmanCoding
|
9
|
+
attr_reader :binary
|
10
|
+
attr_reader :last_byte_bits
|
11
|
+
attr_reader :code_table
|
12
|
+
|
13
|
+
def initialize(binary, last_byte_bits, code_table)
|
14
|
+
@binary = binary
|
15
|
+
@last_byte_bits = last_byte_bits
|
16
|
+
@code_table = code_table.freeze
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
8
20
|
class << HuffmanCoding
|
9
21
|
def encode(input_array, frequencies = HuffmanCoding::Utils.tally(input_array))
|
10
22
|
if frequencies.size == 1
|
@@ -18,12 +30,12 @@ class << HuffmanCoding
|
|
18
30
|
last_byte_bits = result_binary_string.size % 8
|
19
31
|
last_byte_bits = 8 if last_byte_bits == 0
|
20
32
|
binary = result_binary_string.scan(/.{1,8}/).map{|s| s.to_i(2) }.pack('C*')
|
21
|
-
return binary, last_byte_bits, code_table
|
33
|
+
return new(binary, last_byte_bits, code_table)
|
22
34
|
end
|
23
35
|
|
24
|
-
def decode(
|
36
|
+
def decode(coding)
|
25
37
|
text = String.new
|
26
|
-
code_table = code_table.invert
|
38
|
+
code_table = coding.code_table.invert
|
27
39
|
previous = String.new
|
28
40
|
|
29
41
|
add_binary_char = proc do |binary_char|
|
@@ -34,7 +46,7 @@ class << HuffmanCoding
|
|
34
46
|
end
|
35
47
|
end
|
36
48
|
|
37
|
-
bytes = binary.bytes.map{|s| s.to_s(2) } # bytes = ['101000', '11', '10101111', ...]
|
49
|
+
bytes = coding.binary.bytes.map{|s| s.to_s(2) } # bytes = ['101000', '11', '10101111', ...]
|
38
50
|
last_byte = bytes.pop
|
39
51
|
|
40
52
|
bytes.each do |byte|
|
@@ -42,7 +54,7 @@ class << HuffmanCoding
|
|
42
54
|
byte.each_char{|s| add_binary_char[s] }
|
43
55
|
end
|
44
56
|
|
45
|
-
(last_byte_bits - last_byte.size).times{ add_binary_char['0'] }
|
57
|
+
(coding.last_byte_bits - last_byte.size).times{ add_binary_char['0'] }
|
46
58
|
last_byte.each_char{|s| add_binary_char[s] }
|
47
59
|
|
48
60
|
return text
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: huffman_coding
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- khiav reoy
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-05-
|
11
|
+
date: 2020-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|