ruby_collections 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0462986d07093cf4ecca10470680ebbafcda8d9
4
- data.tar.gz: f722323cd43f7b4d5f4abb4301dfd2fb9ae57fd5
3
+ metadata.gz: 34ab0c58c7947f07aa41aedecd63bb6cb93891da
4
+ data.tar.gz: bc4baeb4df6f7b1bcaeb0932791c449dbfa48382
5
5
  SHA512:
6
- metadata.gz: 5a78827b01c0fd4fc5fd2908d5f668d35f341236dce3fbd5018ce652c332fe5da1976fa47d4c730e81fadd50109b1a787a8f1f87a6e9e04acd4bc9d2a83d300a
7
- data.tar.gz: 96b622b4028421d8624f3536e9a2d36d6540f6b0de02a5f8b7ee387d7330e895508dcb5d2ded53ff82b058b6c41ebf430651ff2c670870e1bfc30667476d945a
6
+ metadata.gz: f019fb61c75a57552a9442e1ef97f735f9329c02c5fb5e5e5b6390ae8aa3aac772a29be1908b7deef5b46237fe80554e8de026a45c842b673c79912651f34b24
7
+ data.tar.gz: c9b8b9192ef07cc1f53b2d39cd0db21d108d7d479041a4831665789f7dbf9c4c64847cd7314d9c6d30ed5f64d990366e39858fa01666621f31398e41fa8a2935
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # RubyCollections
2
2
 
3
- Welcome to ruby_collections gem. This gem will provide you with an easy access to common data structures to be used in ruby language.
3
+ Welcome to ruby_collections gem. This gem will provide you with an easy access to common data structures to be used in Ruby Language.
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,7 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
- Supported Data Structures: Max Heap, Min Heap, Stack, LinkedList, Deque
23
+ Supported Data Structures: Max Heap, Min Heap, Stack, LinkedList, Deque, UnionFind
24
24
 
25
25
  ### RubyCollections::MaxHeap
26
26
 
@@ -156,6 +156,30 @@ list.remove_last # list: [10, 2]
156
156
 
157
157
  ```
158
158
 
159
+ ### RubyCollections::UnionFind
160
+
161
+ ```ruby
162
+
163
+ # initialization takes an array argument. each element should have a to_s method defined which should return uniq val
164
+
165
+ uf = RubyCollections::UnionFind.new (1..8).to_a
166
+
167
+ uf.union(1,3) # joins 1 and 3 and returns the leader
168
+
169
+ uf.union(5,6) # joins 5 and 6 and returns the leader
170
+
171
+ uf.union(2,4) # joins 2 and 4 and returns the leader
172
+
173
+ uf.union(1,7) # joins 1 and 7 and returns the leader
174
+
175
+ uf.find(7) # leader of the cluster containing 7
176
+
177
+ uf.to_a # => [[1, 3, 7], [2, 4], [5, 6], [8]] i.e. returns array of all clusters
178
+
179
+ uf.cluster(1) # => [1, 3, 7] i.e. returns all elements in same cluster as of 1
180
+
181
+ ```
182
+
159
183
  ## Development
160
184
 
161
185
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,4 +2,5 @@ require "ruby_collections/max_heap"
2
2
  require "ruby_collections/min_heap"
3
3
  require "ruby_collections/stack"
4
4
  require "ruby_collections/linked_list"
5
- require "ruby_collections/deque"
5
+ require "ruby_collections/deque"
6
+ require "ruby_collections/union_find"
@@ -2,6 +2,9 @@ require 'securerandom'
2
2
  module RubyCollections
3
3
  class Deque
4
4
 
5
+ #TODO: implement iterator
6
+ #TODO: implement to_a
7
+
5
8
  def initialize
6
9
  @hash = {size: 0, front: {data: nil, next: :rear, prev: nil},
7
10
  rear: {data: nil, next: nil, prev: :front}}
@@ -2,6 +2,9 @@ module RubyCollections
2
2
  class LinkedList
3
3
  attr_accessor :size, :top
4
4
 
5
+ #TODO: implement iterator
6
+ #TODO: implement to_a
7
+
5
8
  def initialize
6
9
  @size = 0
7
10
  @top = nil
@@ -1,6 +1,9 @@
1
1
  module RubyCollections
2
2
  class Stack
3
3
 
4
+ #TODO: implement iterator
5
+ #TODO: implement to_a
6
+
4
7
  def initialize(arr = [])
5
8
  @arr = arr
6
9
  end
@@ -0,0 +1,62 @@
1
+ module RubyCollections
2
+ class UnionFind
3
+ def initialize(arr)
4
+ @leadership_hash = {}; @count = {}; @orig_val = {}
5
+ arr.each {|elem| set_hash(elem, elem); set_size(elem, 0); @orig_val[elem.to_s] = elem}
6
+ end
7
+
8
+ def find(elem)
9
+ elem = get_hash(elem) while get_hash(elem) != elem
10
+ return elem
11
+ end
12
+
13
+ def union(elem1, elem2)
14
+ leader1 = find(elem1)
15
+ leader2 = find(elem2)
16
+ unless leader1 == leader2
17
+ size(leader1) > size(leader2) ? set_hash(leader2, leader1) : set_hash(leader1, leader2)
18
+ end
19
+ end
20
+
21
+ def to_a
22
+ hash.keys.each do |key|
23
+ leader = find(key)
24
+ uf_hash[leader.to_s] = uf_hash.fetch(leader.to_s, []) << @orig_val[key]
25
+ end
26
+ uf_hash.values
27
+ end
28
+
29
+ def cluster(elem)
30
+ leader = find(elem)
31
+ cluster = []
32
+ hash.keys.each {|key| cluster << @orig_val[key] if find(key) == leader}
33
+ cluster
34
+ end
35
+
36
+ private
37
+
38
+ def set_hash(key, value)
39
+ @leadership_hash[key.to_s] = value
40
+ end
41
+
42
+ def get_hash(key)
43
+ @leadership_hash[key.to_s]
44
+ end
45
+
46
+ def hash
47
+ @leadership_hash
48
+ end
49
+
50
+ def size(elem)
51
+ @count[elem.to_s]
52
+ end
53
+
54
+ def set_size(elem, val)
55
+ @count[elem.to_s] = val
56
+ end
57
+
58
+ def uf_hash
59
+ @uf_hash ||= {}
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module RubyCollections
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_collections
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vikash Vikram
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-04-10 00:00:00.000000000 Z
11
+ date: 2015-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -63,6 +63,7 @@ files:
63
63
  - lib/ruby_collections/max_heap.rb
64
64
  - lib/ruby_collections/min_heap.rb
65
65
  - lib/ruby_collections/stack.rb
66
+ - lib/ruby_collections/union_find.rb
66
67
  - lib/ruby_collections/version.rb
67
68
  - ruby_collections.gemspec
68
69
  homepage: https://github.com/vikashvikram/ruby_collections