huffify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +8 -0
- data/lib/huffify/version.rb +5 -0
- data/lib/huffify.rb +64 -0
- data/sig/huffify/huff_node.rbs +12 -0
- data/sig/huffify.rbs +14 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bcd01691951ba6965369cffe9a2c8e124162f0c53fc7d81a8e230a1f529558d4
|
4
|
+
data.tar.gz: 6b4cf6c8b86fb7ea9e3cf37bb880a7e87c6d8854df5b8aca868fd8f6416981ca
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3f1a39bbc56b684f9a88670c671247877d6556638adf3ea407550e29924f2f687195ec7dcfd3c6add898c843a0ac75cbe1a468eb16d4cfc4be7f027bbc7193d
|
7
|
+
data.tar.gz: 3d065482d67930e1bf3b25b89a332d51d06d8d54107cc84fae18c2dbf5c243adff61abbb28840492b12a93df610560e2ad13a92debdf1b62173c91f5a8837994
|
data/.rspec
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Ivan Mihun
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Huffify
|
2
|
+
Huffify is a Ruby gem that provides functionality to encode and decode text data using Huffman encoding, a lossless data compression algorithm. Huffman encoding efficiently compresses data by assigning shorter codes to more frequent symbols and longer codes to less frequent symbols.
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
To install the gem, add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'huff'
|
10
|
+
```
|
11
|
+
|
12
|
+
Then execute:
|
13
|
+
|
14
|
+
```bash
|
15
|
+
bundle install
|
16
|
+
```
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
gem install huffman_encoder
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Basic Encoding and Decoding
|
27
|
+
|
28
|
+
Here's how you can use the gem to encode and decode a string using Huffman encoding:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'huff'
|
32
|
+
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
|
33
|
+
|
34
|
+
result = encode(text)
|
35
|
+
|
36
|
+
decoded_text = decode(result[:encoded_text], result[:huffman_tree])
|
37
|
+
|
38
|
+
decoded_text == text # true
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
## License
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/huffify.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'pqueue'
|
3
|
+
require_relative "huff/version"
|
4
|
+
|
5
|
+
module Huffify
|
6
|
+
class HuffNode
|
7
|
+
attr_accessor :char, :frequency, :left, :right
|
8
|
+
|
9
|
+
def initialize(char, frequency, left = nil, right = nil)
|
10
|
+
@char = char
|
11
|
+
@frequency = frequency
|
12
|
+
@left = left
|
13
|
+
@right = right
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def encode(text)
|
18
|
+
huffman_tree = build_huffman_tree(text)
|
19
|
+
codes = get_char_codes(huffman_tree)
|
20
|
+
{ encoded_text: text.each_char.map { |char| codes[char] }.join, huffman_tree: huffman_tree }
|
21
|
+
end
|
22
|
+
|
23
|
+
def decode(bits, huffman_tree)
|
24
|
+
text = ""
|
25
|
+
cur_node = huffman_tree
|
26
|
+
bits.each_char do |bit|
|
27
|
+
cur_node = bit == '0' ? cur_node.left : cur_node.right
|
28
|
+
if cur_node.char
|
29
|
+
text += cur_node.char
|
30
|
+
cur_node = huffman_tree
|
31
|
+
end
|
32
|
+
end
|
33
|
+
text
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def create_occurrences_map(text)
|
39
|
+
occurrences_map = Hash.new(0)
|
40
|
+
text.each_char { |char| occurrences_map[char] += 1 }
|
41
|
+
occurrences_map
|
42
|
+
end
|
43
|
+
|
44
|
+
def build_huffman_tree(text)
|
45
|
+
occurrences_map = create_occurrences_map(text)
|
46
|
+
pq = PQueue.new { |a, b| a.frequency < b.frequency }
|
47
|
+
occurrences_map.each { |char, count| pq.push(HuffNode.new(char, count)) }
|
48
|
+
until pq.size == 1
|
49
|
+
first, second = pq.pop, pq.pop
|
50
|
+
pq.push(HuffNode.new(nil, first.frequency + second.frequency, first, second))
|
51
|
+
end
|
52
|
+
pq.pop
|
53
|
+
end
|
54
|
+
|
55
|
+
def get_char_codes(node, code = '', codes = {})
|
56
|
+
if node.char
|
57
|
+
codes[node.char] = code
|
58
|
+
else
|
59
|
+
get_char_codes(node.left, code + '0', codes)
|
60
|
+
get_char_codes(node.right, code + '1', codes)
|
61
|
+
end
|
62
|
+
codes
|
63
|
+
end
|
64
|
+
end
|
data/sig/huffify.rbs
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Huffify
|
2
|
+
VERSION: String
|
3
|
+
|
4
|
+
def encode: (String text) -> Hash[Symbol, HuffNode | String]
|
5
|
+
def decode: (String bits, HuffNode tree) -> String
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def build_huffman_tree: (String text) -> HuffNode
|
10
|
+
|
11
|
+
def create_occurrences_map: (String text) -> Hash[String, Integer]
|
12
|
+
|
13
|
+
def get_char_codes: (HuffNode node, String code, Hash[String, String]) -> Hash[String, String]
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: huffify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Mihun
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-10-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pqueue
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.0
|
27
|
+
description: Huffify is a Ruby gem that provides functionality to encode and decode
|
28
|
+
text data using Huffman encoding, a lossless data compression algorithm. Huffman
|
29
|
+
encoding efficiently compresses data by assigning shorter codes to more frequent
|
30
|
+
symbols and longer codes to less frequent symbols.
|
31
|
+
email:
|
32
|
+
- vsk76090@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- ".rspec"
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- lib/huffify.rb
|
42
|
+
- lib/huffify/version.rb
|
43
|
+
- sig/huffify.rbs
|
44
|
+
- sig/huffify/huff_node.rbs
|
45
|
+
homepage: https://github.com/ivan-22-3-5/huff
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
allowed_push_host: https://rubygems.org
|
50
|
+
homepage_uri: https://github.com/ivan-22-3-5/huff
|
51
|
+
source_code_uri: https://github.com/ivan-22-3-5/huff
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.0.0
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.5.16
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: A Ruby gem for lossless data compression using Huffman encoding.
|
71
|
+
test_files: []
|