collection_utils 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
+ SHA1:
3
+ metadata.gz: ce5adb05105792945942d3c83bc675fb67a2bedf
4
+ data.tar.gz: 7ab710ea4fa7f278ee40c9f5dc6c4591167a8de5
5
+ SHA512:
6
+ metadata.gz: ec7d96ce17ca0816bab823ec8966d25b11bd361530be6dda756eec11ee88d236fc8cc63d4cc10b497b1eb10aa58773daedce1cab42063243b696094ea6bb8d61
7
+ data.tar.gz: f4b447cc6da79c419b1c3e6d83dbf9cc88b2b9d99f5c3656b8ac2de5b346361112e98df53762de605a75e7b571331b3c024e881a71709694d9ddd2514d3c7809
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.14.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in collection_utils.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 deeshugupta
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,38 @@
1
+ # CollectionUtils
2
+
3
+ CollectionUtils provide with basic collection templates like stack, queues and heaps e.t.c for easier development.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'collection_utils'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install collection_utils
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/deeshugupta/collection_utils. 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.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "collection_utils"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'collection_utils/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "collection_utils"
8
+ spec.version = CollectionUtils::VERSION
9
+ spec.authors = ["deeshugupta"]
10
+ spec.email = ["gupta.deeshu@gmail.com"]
11
+
12
+ spec.summary = ""
13
+ spec.description = "CollectionUtils provide with basic collection templates
14
+ like stack, queues and heaps e.t.c for easier development."
15
+ spec.homepage = "https://github.com/deeshugupta/collection_utils"
16
+ spec.license = "MIT"
17
+
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.14"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + '/collection_utils/*.rb'].each {|file| require file }
2
+ module CollectionUtils
3
+ # Your code goes here...
4
+ end
@@ -0,0 +1,5 @@
1
+ Dir[File.dirname(__FILE__) + '/heap_utils/*.rb'].each {|file| require file }
2
+ module CollectionUtils
3
+ module HeapUtils
4
+ end
5
+ end
@@ -0,0 +1,97 @@
1
+ module CollectionUtils
2
+ module HeapUtils
3
+ class Heap
4
+ private
5
+ attr_accessor :heap
6
+
7
+ public
8
+ #Constructors
9
+ def initialize(array = [])
10
+ @heap = array
11
+ end
12
+
13
+ #Public methods
14
+ def root
15
+ return @heap.first
16
+ end
17
+
18
+ def push(element)
19
+ @heap << element
20
+ end
21
+
22
+ def pop
23
+ element = @heap.pop
24
+ return element
25
+ end
26
+
27
+ def left_child(parent = 0)
28
+ left = (2*parent + 1)
29
+ return nil, nil if @heap[left].nil?
30
+ return @heap[left], left
31
+ end
32
+
33
+ def right_child(parent = 0)
34
+ right = (2*parent + 2)
35
+ return nil, nil if @heap[right].nil?
36
+ return @heap[right], right
37
+ end
38
+
39
+ def assign_left(parent =0, node)
40
+ left = (2*parent + 1)
41
+ @heap[left] = node
42
+ end
43
+
44
+ def assign_right(parent =0, node)
45
+ right = (2*parent + 2)
46
+ @heap[right] = node
47
+ end
48
+
49
+ def parent(child = 0)
50
+ par = child/2
51
+ return @heap[par], par
52
+ end
53
+
54
+ def size
55
+ return @heap.size
56
+ end
57
+
58
+ def is_empty?
59
+ return size == 0
60
+ end
61
+
62
+ def bfs
63
+ queue = CollectionUtils::Queue.new
64
+ queue.enqueue({element: @heap.first, index: 0})
65
+ while true do
66
+ node = queue.dequeue
67
+ break if node.nil?
68
+ left = left_child(node[:index])
69
+ right = right_child(node[:index])
70
+ yield(node[:element]) if block_given?
71
+ queue.enqueue({element: left.first, index: left.last}) if !left.first.nil?
72
+ queue.enqueue({element: right.first, index: right.last}) if !right.first.nil?
73
+ break if left.first.nil? && right.first
74
+ end
75
+
76
+ end
77
+
78
+ def dfs
79
+ stack = CollectionUtils::Stack.new
80
+ stack.push({element: @heap.first, index: 0})
81
+ while true do
82
+ node = stack.pop
83
+ break if node.nil?
84
+ left = left_child(node[:index])
85
+ right = right_child(node[:index])
86
+ yield(node[:element]) if block_given?
87
+ stack.push({element: left.first, index: left.last}) if !left.first.nil?
88
+ stack.push({element: right.first, index: right.last}) if !right.first.nil?
89
+ break if left.first.nil? && right.first
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ Tree = Heap
96
+ end
97
+ end
@@ -0,0 +1,81 @@
1
+ require_relative './heap'
2
+ module CollectionUtils
3
+ module HeapUtils
4
+ class MaxHeap < Heap
5
+
6
+ private
7
+ attr_accessor :heap
8
+
9
+ def exchange(element, comparor)
10
+ temp = comparor[:element]
11
+ comparor[:element] = element[:element]
12
+ element[:element] = temp
13
+ return element, comparor
14
+ end
15
+
16
+ def heapify(node)
17
+ left = left_child(node[:index]).first
18
+ right = right_child(node[:index]).first
19
+ largest = node
20
+ if !left.nil? && left[:element] >= node[:element]
21
+ largest = left
22
+ end
23
+
24
+ if !right.nil? && right[:element] >= largest[:element]
25
+ largest = right
26
+ end
27
+
28
+ if largest != node
29
+ exchange(largest, node)
30
+ heapify(largest)
31
+ end
32
+
33
+ end
34
+
35
+ def max
36
+ @heap.first
37
+ end
38
+
39
+ public
40
+ def initialize(array = [])
41
+ @heap = []
42
+ array.each_with_index do |element, index|
43
+ insert(element)
44
+ end
45
+ end
46
+
47
+ def insert(value)
48
+ value = {element: element, index: size}
49
+ @heap << value
50
+ i = @heap.size - 1
51
+ node, index = parent(i)
52
+ while i != 0 && @heap[i][:element] >= node[:element] do
53
+ exchange(@heap[i], node)
54
+ i = index
55
+ node, index = parent(i)
56
+ end
57
+ end
58
+
59
+ def get_max
60
+ return if is_empty?
61
+ return max[:element]
62
+ end
63
+
64
+ def extract_max
65
+ return if is_empty?
66
+ maximum = max
67
+ if @heap.size > 1
68
+ last_value = @heap.last
69
+ last_value[:index] = 0
70
+ @heap = @heap.slice(0..@heap.size-2)
71
+ @heap[0] = last_value
72
+ heapify(max)
73
+ else
74
+ @heap =[ ]
75
+ end
76
+
77
+ return maximum[:element]
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,81 @@
1
+ require_relative './heap'
2
+ module CollectionUtils
3
+ module HeapUtils
4
+ class MinHeap < Heap
5
+
6
+ private
7
+ attr_accessor :heap
8
+
9
+ def exchange(element, comparor)
10
+ temp = comparor[:element]
11
+ comparor[:element] = element[:element]
12
+ element[:element] = temp
13
+ return element, comparor
14
+ end
15
+
16
+ def heapify(node)
17
+ left = left_child(node[:index]).first
18
+ right = right_child(node[:index]).first
19
+ smallest = node
20
+ if !left.nil? && left[:element] < node[:element]
21
+ smallest = left
22
+ end
23
+
24
+ if !right.nil? && right[:element] < smallest[:element]
25
+ smallest = right
26
+ end
27
+
28
+ if smallest != node
29
+ exchange(smallest, node)
30
+ heapify(smallest)
31
+ end
32
+
33
+ end
34
+
35
+ def min
36
+ @heap.first
37
+ end
38
+
39
+ public
40
+ def initialize(array = [])
41
+ @heap = []
42
+ array.each_with_index do |element, index|
43
+ insert(element)
44
+ end
45
+ end
46
+
47
+ def insert(element)
48
+ value = {element: element, index: size}
49
+ @heap << value
50
+ i = size - 1
51
+ node, index = parent(i)
52
+ while i != 0 && @heap[i][:element] < node[:element] do
53
+ exchange(@heap[i], node)
54
+ i = index
55
+ node, index = parent(i)
56
+ end
57
+ end
58
+
59
+ def get_min
60
+ return if is_empty?
61
+ return min[:element]
62
+ end
63
+
64
+ def extract_min
65
+ return if is_empty?
66
+ minimum = min
67
+ if @heap.size > 1
68
+ last_value = @heap.last
69
+ last_value[:index] = 0
70
+ @heap = @heap.slice(0..@heap.size-2)
71
+ @heap[0] = last_value
72
+ heapify(min)
73
+ else
74
+ @heap =[ ]
75
+ end
76
+
77
+ return minimum[:element]
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,45 @@
1
+ module CollectionUtils
2
+ class Queue
3
+ private
4
+ attr_accessor :queue
5
+
6
+ public
7
+ #Constructors
8
+
9
+ def initialize(array=[])
10
+ @queue = []
11
+ array.each do |element|
12
+ enqueue(element)
13
+ end
14
+ end
15
+
16
+ #Public Methods
17
+
18
+ def enqueue(element)
19
+ @queue << element
20
+ end
21
+
22
+ def dequeue
23
+ element = @queue.first
24
+ @queue = @queue.slice(1..-1)
25
+ return element
26
+ end
27
+
28
+ def front
29
+ return @queue.first
30
+ end
31
+
32
+ def rear
33
+ return @queue.last
34
+ end
35
+
36
+ def is_empty?
37
+ return @queue.size == 0
38
+ end
39
+
40
+ def size
41
+ return @queue.size
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ module CollectionUtils
2
+ class Stack
3
+ private
4
+ attr_accessor :stack
5
+
6
+ public
7
+ #Constructors
8
+ def initialize(array=[])
9
+ @stack = []
10
+ array.each do |element|
11
+ @stack << element
12
+ end
13
+ end
14
+
15
+ #Public methods
16
+ def pop()
17
+ return @stack.pop
18
+ end
19
+
20
+ def push(element)
21
+ @stack << element
22
+ end
23
+
24
+ def is_empty?
25
+ return @stack.size == 0
26
+ end
27
+
28
+ def peek
29
+ return @stack.last
30
+ end
31
+
32
+ def size
33
+ return @stack.size
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module CollectionUtils
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collection_utils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - deeshugupta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-18 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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: |-
56
+ CollectionUtils provide with basic collection templates
57
+ like stack, queues and heaps e.t.c for easier development.
58
+ email:
59
+ - gupta.deeshu@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - .rspec
66
+ - .travis.yml
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - collection_utils.gemspec
74
+ - lib/collection_utils.rb
75
+ - lib/collection_utils/heap_utils.rb
76
+ - lib/collection_utils/heap_utils/heap.rb
77
+ - lib/collection_utils/heap_utils/max_heap.rb
78
+ - lib/collection_utils/heap_utils/min_heap.rb
79
+ - lib/collection_utils/queue.rb
80
+ - lib/collection_utils/stack.rb
81
+ - lib/collection_utils/version.rb
82
+ homepage: https://github.com/deeshugupta/collection_utils
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.4.8
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: ''
106
+ test_files: []