huffify 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of huffify might be problematic. Click here for more details.
- 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 +62 -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: 845609542a9ee0c5c71059d998828ab7b6d88f2f50c1b9be019c4dd53eff2bea
|
4
|
+
data.tar.gz: 0a5b18ee7ca29489c9bda0e83858207e86eb24aea1e2524e1f6ff3f37fb68998
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 042cb5b5c293462af59d837ffa680b82761590ffb5cc2afd4938d88bec14afa7fa86214c9aacee0d91cc0669790f424e7263066f8947b41a74f001739f4f0cf4
|
7
|
+
data.tar.gz: 1dae9271c9da5dc6503d5259e518055e0be643e45bf469493a4abfbaeb4011a433c3c230f9ff457b8e882b59898966168461749a7c8973baf68432a96209bd54
|
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,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'pqueue'
|
3
|
+
require_relative "huffify/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 self.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 self.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_class_method def self.create_occurrences_map(text)
|
37
|
+
occurrences_map = Hash.new(0)
|
38
|
+
text.each_char { |char| occurrences_map[char] += 1 }
|
39
|
+
occurrences_map
|
40
|
+
end
|
41
|
+
|
42
|
+
private_class_method def self.build_huffman_tree(text)
|
43
|
+
occurrences_map = create_occurrences_map(text)
|
44
|
+
pq = PQueue.new { |a, b| a.frequency < b.frequency }
|
45
|
+
occurrences_map.each { |char, count| pq.push(HuffNode.new(char, count)) }
|
46
|
+
until pq.size == 1
|
47
|
+
first, second = pq.pop, pq.pop
|
48
|
+
pq.push(HuffNode.new(nil, first.frequency + second.frequency, first, second))
|
49
|
+
end
|
50
|
+
pq.pop
|
51
|
+
end
|
52
|
+
|
53
|
+
private_class_method def self.get_char_codes(node, code = '', codes = {})
|
54
|
+
if node.char
|
55
|
+
codes[node.char] = code
|
56
|
+
else
|
57
|
+
get_char_codes(node.left, code + '0', codes)
|
58
|
+
get_char_codes(node.right, code + '1', codes)
|
59
|
+
end
|
60
|
+
codes
|
61
|
+
end
|
62
|
+
end
|
data/sig/huffify.rbs
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Huffify
|
2
|
+
VERSION: String
|
3
|
+
|
4
|
+
def self.encode: (String text) -> Hash[Symbol, HuffNode | String]
|
5
|
+
def self.decode: (String bits, HuffNode tree) -> String
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def self.build_huffman_tree: (String text) -> HuffNode
|
10
|
+
|
11
|
+
def self.create_occurrences_map: (String text) -> Hash[String, Integer]
|
12
|
+
|
13
|
+
def self.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.2
|
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: []
|