huffify 0.1.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 53c605656504e6f7092d359b51e8d2c590d9c6c06a79b06a21ca5ad147e2adfc
4
+ data.tar.gz: a3935745bd84ab3bb94a0f741fb8ee1f25baf5f14384939ed479f9ce1b161f91
5
+ SHA512:
6
+ metadata.gz: 762a45e92782a924d60964d401b99cb21e4a52c13dcc505b6f7b618ee54e823e4ac202eb086b5fd481eca4a479f62b7f93ab9e41df7a52bf71c2fb5ee08ef167
7
+ data.tar.gz: ddbe0f6e2354b65f3c182b294690164dec04b670d6c05dfd7912b291e8c12b275f5a3ebde71d57e4b6eda2cef824a1d77e391e3ea4d8dcd1b5c8080d2819595c
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
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 'huffify'
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 huffify
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 'huffify'
32
+ text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
33
+
34
+ result = Huffify::encode(text)
35
+
36
+ decoded_text = Huffify::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
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Huffify
4
+ VERSION = "0.1.3"
5
+ end
data/lib/huffify.rb ADDED
@@ -0,0 +1,67 @@
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
+ return '' if huffman_tree.nil?
25
+ return huffman_tree.char * bits.length if huffman_tree.char
26
+
27
+ text = ""
28
+ cur_node = huffman_tree
29
+ bits.each_char do |bit|
30
+ cur_node = bit == '0' ? cur_node.left : cur_node.right
31
+ if cur_node.char
32
+ text += cur_node.char
33
+ cur_node = huffman_tree
34
+ end
35
+ end
36
+ text
37
+ end
38
+
39
+ private_class_method def self.create_occurrences_map(text)
40
+ occurrences_map = Hash.new(0)
41
+ text.each_char { |char| occurrences_map[char] += 1 }
42
+ occurrences_map
43
+ end
44
+
45
+ private_class_method def self.build_huffman_tree(text)
46
+ occurrences_map = create_occurrences_map(text)
47
+ pq = PQueue.new { |a, b| a.frequency < b.frequency }
48
+ occurrences_map.each { |char, count| pq.push(HuffNode.new(char, count)) }
49
+ until pq.size == 1
50
+ first, second = pq.pop, pq.pop
51
+ pq.push(HuffNode.new(nil, first.frequency + second.frequency, first, second))
52
+ end unless pq.empty?
53
+ pq.pop
54
+ end
55
+
56
+ private_class_method def self.get_char_codes(node, code = '', codes = {})
57
+ return {} if node.nil?
58
+ return {node.char => '0'} if code.empty? and codes.empty? and node.char
59
+ if node.char
60
+ codes[node.char] = code
61
+ else
62
+ get_char_codes(node.left, code + '0', codes)
63
+ get_char_codes(node.right, code + '1', codes)
64
+ end
65
+ codes
66
+ end
67
+ end
@@ -0,0 +1,12 @@
1
+ module Huffify
2
+ class HuffNode
3
+ @char: String
4
+ @frequency: Integer
5
+
6
+ attr_accessor char: String | nil
7
+ attr_accessor frequency: Integer
8
+ attr_accessor left: HuffNode | nil
9
+ attr_accessor right: HuffNode | nil
10
+
11
+ end
12
+ 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.3
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Mihun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-23 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: []