heapify 0.1.0

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: 51a7513b7e04e069205261a19182ffef3bfa703acd31a30c32a7341ecd4b935b
4
+ data.tar.gz: 64b1ddace129544cca479eba4b856081efcf788dd93afabb156e2f9a0c3eaaea
5
+ SHA512:
6
+ metadata.gz: c14ea46042ae6eb1de1d4bd98632ff8ed543c6a21f3f008f54c63d32ba64d2bccdfb69561a3d8361fdf7266238453945731c36d7e287e554a82a6e8d1b1da59d
7
+ data.tar.gz: 1f6c21864c04be6f3bf03f2d8ce3a9fa4c1ec00563338c7565f98a7ad26efd22190f4a55a020d95c6e4537a737cfa6c0b5153cd904bdd2bf90a61006bc7c125a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-24
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at garrison.jensen@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Garrison Jensen
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,76 @@
1
+ # Heapify
2
+
3
+ Inspired by Python's heapq module, Heapify is a Ruby library that provides methods to transform an array into a min heap. A min heap is a binary tree where the parent node is less than or equal to its children.
4
+
5
+ The API below differs from textbook heap algorithms in two aspects: (a) We use zero-based indexing. This makes the relationship between the index for a node and the indexes for its children slightly less obvious, but is more suitable since Ruby uses zero-based indexing. (b) Our pop method returns the smallest item, not the largest (called a “min heap” in textbooks; a “max heap” is more common in texts because of its suitability for in-place sorting).
6
+
7
+ These two make it possible to view the heap as a regular Ruby list without surprises: heap[0] is the smallest item, and heap.sort maintains the heap invariant!
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'heapify'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```bash
26
+ gem install heapify
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ Heapify extends the Array class with the following methods:
32
+
33
+ ### `heapify`
34
+
35
+ Transforms an array into a heap in-place. It has a time complexity of O(n) and a space complexity of O(1).
36
+
37
+ ```ruby
38
+ array = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
39
+ heap = array.heapify
40
+ # heap => [1, 2, 3, 4, 7, 9, 10, 14, 8, 16]
41
+ ```
42
+
43
+ ### `heap_push(val)`
44
+
45
+ This method adds an element to the heap. It has a time complexity of O(log n) and a space complexity of O(1).
46
+
47
+ ```ruby
48
+ array = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
49
+ array.heapify
50
+ array.heap_push(5)
51
+ # array => [1, 2, 3, 4, 5, 9, 10, 14, 8, 7, 16]
52
+ ```
53
+
54
+ ### `heap_pop`
55
+
56
+ This method removes the smallest element from the heap and returns it. It has a time complexity of O(log n) and a space complexity of O(1).
57
+
58
+ ```ruby
59
+ array = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
60
+ array.heapify
61
+ array.heap_pop
62
+ # => 1
63
+ # array => [2, 4, 3, 7, 16, 9, 10, 14, 8]
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/garrisonj/heapify. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
69
+
70
+ ## License
71
+
72
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
73
+
74
+ ## Code of Conduct
75
+
76
+ Everyone interacting in the Heapify project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/garrisonj/heapify/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Heapify
4
+ VERSION = "0.1.0"
5
+ end
data/lib/heapify.rb ADDED
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "heapify/version"
4
+
5
+ module Heapify
6
+ # Provides a heapify method for arrays. It also provides methods for adding and removing elements from the heap.
7
+ module ArrayExtension
8
+ # Transforms an array into a heap, in-place, in linear time.
9
+ #
10
+ # @return [Array] The heapified array.
11
+ def heapify
12
+ (size / 2).downto(0) do |i|
13
+ heapify_down(i)
14
+ end
15
+ self
16
+ end
17
+
18
+ # Add an element to the heap.
19
+ #
20
+ # O(log n) time complexity.
21
+ #
22
+ # @param val [Object] The value to be added to the heap.
23
+ # @return [void]
24
+ def heap_push(val)
25
+ self << val
26
+ heapify_up(size - 1)
27
+ end
28
+
29
+ # Remove the minimum element from the heap.
30
+ #
31
+ # O(log n) time complexity.
32
+ #
33
+ # @return [Object, nil] The minimum element from the heap, or nil if the heap is empty.
34
+ def heap_pop
35
+ return if empty?
36
+
37
+ swap(0, size - 1)
38
+ min = pop
39
+ heapify_down(0)
40
+ min
41
+ end
42
+
43
+ private
44
+
45
+ def heapify_down(index)
46
+ left = 2 * index + 1
47
+ right = 2 * index + 2
48
+ smallest = index
49
+
50
+ smallest = left if left < size && self[left] < self[smallest]
51
+ smallest = right if right < size && self[right] < self[smallest]
52
+
53
+ return if smallest == index
54
+
55
+ swap(index, smallest)
56
+ heapify_down(smallest)
57
+ end
58
+
59
+ def heapify_up(index)
60
+ parent = (index - 1) / 2
61
+
62
+ return if index.zero? || self[parent] <= self[index]
63
+
64
+ swap(index, parent)
65
+ heapify_up(parent)
66
+ end
67
+
68
+ def swap(index1, index2)
69
+ self[index1], self[index2] = self[index2], self[index1]
70
+ end
71
+ end
72
+ end
73
+
74
+ Array.include Heapify::ArrayExtension
data/sig/heapify.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Heapify
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heapify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Garrison Jensen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Heapify is a gem that provides a heapify method for arrays. It also provides
14
+ methods for adding and removing elements from the heap.
15
+ email:
16
+ - garrison.jensen@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - ".rubocop.yml"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/heapify.rb
29
+ - lib/heapify/version.rb
30
+ - sig/heapify.rbs
31
+ homepage: https://www.github.com/garrisonJ/heapify
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ allowed_push_host: https://rubygems.org
36
+ homepage_uri: https://www.github.com/garrisonJ/heapify
37
+ source_code_uri: https://www.github.com/garrisonJ/heapify
38
+ changelog_uri: https://www.github.com/garrisonJ/heapify/CHANGELOG.md
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.4.22
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Heapify is a gem that provides a heapify method for arrays.
58
+ test_files: []