ruby-heap 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 +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/Heap.gemspec +27 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/Heap/binary_heap/binary_heap.rb +85 -0
- data/lib/Heap/version.rb +3 -0
- data/lib/Heap.rb +5 -0
- data/ruby-heap-0.0.5.gem +0 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b49b7d675703a399ead164dfd7cd837975253c61
|
4
|
+
data.tar.gz: ac4a978badfa483cadb5445bdf308283af462ee7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 604bab2bb181daf0cbc411c5fae7788ab5424bd517497f1f6bb1b80bea7473eafe72f0ddfa4d0dc56cbc6ea7feecb73eedede1a12e4c0b6f488ca6e3057ee7bd
|
7
|
+
data.tar.gz: 0e725440379858b4f3dbe03c0686b36c800775ab3ad5a33726edff9aa6ef30fb316c1f4aaacf11c649a5dca78a06c9cf07f84143801dece3a2064e1a8eb98b9e
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Heap.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'Heap/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'ruby-heap'
|
9
|
+
spec.version = Heap::VERSION
|
10
|
+
spec.authors = ['Alexandr Sysoev']
|
11
|
+
spec.email = ['sanchous.ok@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Binary or multiple heap'
|
14
|
+
spec.homepage = 'https://github.com/pups3s/ruby-heap'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
27
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Alexandr Sysoev
|
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,62 @@
|
|
1
|
+
# Heap (ruby heapsort)
|
2
|
+
|
3
|
+
Gem is using for making Heaps (binary only for now).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ruby-heap'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install ruby-heap
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
#### Binary Heap with min root
|
24
|
+
```ruby
|
25
|
+
require 'Heap'
|
26
|
+
|
27
|
+
# Initialize
|
28
|
+
b_heap = Heap::BinaryHeap::MinHeap.new([2, 3, 1, -1])
|
29
|
+
|
30
|
+
# Return elements in Heap (read access only)
|
31
|
+
b_heap.elements # [-1, 1, 3, 2]
|
32
|
+
|
33
|
+
# Return sorted array (heap-sort) without
|
34
|
+
# changing elements in heap
|
35
|
+
b_heap.sort # [-1, 1, 2, 3]
|
36
|
+
|
37
|
+
# Count of elements in Heap
|
38
|
+
b_heap.count # 4
|
39
|
+
|
40
|
+
# Return min element without removing from Heap
|
41
|
+
b_heap.extract_min # -1
|
42
|
+
|
43
|
+
# Return min element and remove it from Heap
|
44
|
+
b_heap.extract_min! # -1
|
45
|
+
b_heap.count # 3
|
46
|
+
b_heap.elements # [1, 2, 3]
|
47
|
+
```
|
48
|
+
|
49
|
+
## Development
|
50
|
+
|
51
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
52
|
+
|
53
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
Bug reports and pull requests are welcome on GitHub at [Project page](https://github.com/pups3s/ruby-heap). This project is intended to be a safe, welcoming space for collaboration.
|
58
|
+
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "Heap"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module Heap
|
2
|
+
module BinaryHeap
|
3
|
+
class MinHeap
|
4
|
+
attr_reader :elements
|
5
|
+
|
6
|
+
def initialize(elements = [])
|
7
|
+
@elements = []
|
8
|
+
add(elements.pop) until elements.empty?
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(element)
|
12
|
+
@elements.push element
|
13
|
+
swim_up(count)
|
14
|
+
end
|
15
|
+
|
16
|
+
def count
|
17
|
+
@elements.length
|
18
|
+
end
|
19
|
+
|
20
|
+
def extract_min
|
21
|
+
@elements[0]
|
22
|
+
end
|
23
|
+
|
24
|
+
def extract_min!
|
25
|
+
swap(1, count)
|
26
|
+
el = @elements.pop
|
27
|
+
swim_down(1)
|
28
|
+
el
|
29
|
+
end
|
30
|
+
|
31
|
+
def sort
|
32
|
+
el_temp = @elements.clone
|
33
|
+
result = []
|
34
|
+
result.push extract_min! while count > 0
|
35
|
+
@elements = el_temp
|
36
|
+
result
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def swap(index1, index2)
|
42
|
+
temp = @elements[index1 - 1]
|
43
|
+
@elements[index1 - 1] = @elements[index2 - 1]
|
44
|
+
@elements[index2 - 1] = temp
|
45
|
+
end
|
46
|
+
|
47
|
+
def swim_up(index)
|
48
|
+
return if index == 1
|
49
|
+
parent_index = (index / 2).floor
|
50
|
+
return if @elements[parent_index - 1] <= @elements[index - 1]
|
51
|
+
swap(parent_index, index)
|
52
|
+
swim_up parent_index
|
53
|
+
end
|
54
|
+
|
55
|
+
def swim_down(index)
|
56
|
+
child1_index = 2 * index
|
57
|
+
child2_index = 2 * index + 1
|
58
|
+
return if @elements[child1_index - 1].nil? && @elements[child2_index - 1].nil?
|
59
|
+
if @elements[child2_index - 1].nil?
|
60
|
+
return if @elements[child1_index - 1] >= @elements[index - 1]
|
61
|
+
swap(index, child1_index)
|
62
|
+
swim_down(child1_index)
|
63
|
+
else
|
64
|
+
if @elements[child2_index - 1] >= @elements[index - 1] && @elements[child1_index - 1] >= @elements[index - 1]
|
65
|
+
return
|
66
|
+
elsif @elements[child2_index - 1] < @elements[index - 1] && @elements[child1_index - 1] < @elements[index - 1]
|
67
|
+
if @elements[child2_index - 1] < @elements[child1_index - 1]
|
68
|
+
swap(child2_index, index)
|
69
|
+
swim_down(child2_index)
|
70
|
+
else
|
71
|
+
swap(child1_index, index)
|
72
|
+
swim_down(child1_index)
|
73
|
+
end
|
74
|
+
elsif @elements[child2_index - 1] < @elements[index - 1]
|
75
|
+
swap(child2_index, index)
|
76
|
+
swim_down(child2_index)
|
77
|
+
elsif @elements[child1_index - 1] < @elements[index - 1]
|
78
|
+
swap(child1_index, index)
|
79
|
+
swim_down(child1_index)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/Heap/version.rb
ADDED
data/lib/Heap.rb
ADDED
data/ruby-heap-0.0.5.gem
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-heap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexandr Sysoev
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- sanchous.ok@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- Heap.gemspec
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/Heap.rb
|
72
|
+
- lib/Heap/binary_heap/binary_heap.rb
|
73
|
+
- lib/Heap/version.rb
|
74
|
+
- ruby-heap-0.0.5.gem
|
75
|
+
homepage: https://github.com/pups3s/ruby-heap
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.6.12
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Binary or multiple heap
|
99
|
+
test_files: []
|