ruby_structures 2.3.0 → 2.4.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 +5 -5
- data/lib/data_structures/heap.rb +4 -4
- data/lib/data_structures/priority_queue.rb +74 -0
- data/lib/ruby_structures.rb +1 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8a9f58e9e0fb04f0a297238643bc9ea12408a8b81e7ee3f962ecb34b30e4c410
|
4
|
+
data.tar.gz: cdcd58cb09e29f012ded1af3a8dd1cb839343cc7c39f5810ec0b5d4c42c32ffa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 02c69ae62556ae04a058e5bc8d03e2db59cfb46e72b62717db46b1e7a2e44230f72e22dd05e8c4870dd1f946e71c0adf0605933bdcbe60f44bedc9ce58cdd2d8
|
7
|
+
data.tar.gz: fac9c9fcc1c6da0ffc1d7d43f1cfe1d6bd2b25c28d61a38707c6eea3a03b5279b327443b47a7d3f462b1410e1f8ce9d4fa719d01c0d91d28379584f6496f586d
|
data/lib/data_structures/heap.rb
CHANGED
@@ -11,6 +11,10 @@ class Heap
|
|
11
11
|
heap
|
12
12
|
end
|
13
13
|
|
14
|
+
def initialize
|
15
|
+
@store = []
|
16
|
+
end
|
17
|
+
|
14
18
|
def to_s
|
15
19
|
"Heap: head=#{self.peek || 'nil'}, length=#{self.length}"
|
16
20
|
end
|
@@ -27,10 +31,6 @@ class Heap
|
|
27
31
|
@store.length
|
28
32
|
end
|
29
33
|
|
30
|
-
def initialize
|
31
|
-
@store = []
|
32
|
-
end
|
33
|
-
|
34
34
|
def peek
|
35
35
|
return nil if @store.empty?
|
36
36
|
@store.first
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require_relative 'heap'
|
2
|
+
|
3
|
+
class PriorityQueue < Heap
|
4
|
+
def self.from_array(array)
|
5
|
+
nodes = array.map { |arr| PriorityQueueNode.new(arr.first, arr.last) }
|
6
|
+
heap = super(nodes)
|
7
|
+
from_heap(heap)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.from_hash(hash)
|
11
|
+
nodes = hash.map { |k, v| PriorityQueueNode.new(k, v) }
|
12
|
+
heap = self.superclass.from_array(nodes)
|
13
|
+
from_heap(heap)
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
"Priority Queue: head=#{self.peek || 'nil'}, length=#{self.length}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def inspect
|
21
|
+
"Priority Queue: head=#{self.peek || 'nil'}, length=#{self.length}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def insert(data, priority)
|
25
|
+
node = PriorityQueueNode.new(data, priority)
|
26
|
+
super(node)
|
27
|
+
end
|
28
|
+
|
29
|
+
def find(data)
|
30
|
+
return nil if @store.empty?
|
31
|
+
@store.each { |node| return node if node.send(:data) == data }
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def include?(data)
|
36
|
+
return nil if @store.empty?
|
37
|
+
@store.each { |node| return true if node.send(:data) == data }
|
38
|
+
false
|
39
|
+
end
|
40
|
+
|
41
|
+
def merge(other_queue)
|
42
|
+
raise ArgumentError.new("May only merge with a Priority Queue. You passed a #{other_queue.class}.") unless other_queue.is_a?(PriorityQueue)
|
43
|
+
array = self.send(:store) + other_queue.send(:store)
|
44
|
+
heap = self.class.superclass.from_array(array)
|
45
|
+
self.class.send(:from_heap, heap)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def self.from_heap(heap)
|
51
|
+
queue = PriorityQueue.new
|
52
|
+
queue.instance_variable_set(:@store, heap.send(:store))
|
53
|
+
queue
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
class PriorityQueueNode
|
58
|
+
def initialize(data, priority)
|
59
|
+
@data = data
|
60
|
+
@priority = priority
|
61
|
+
end
|
62
|
+
|
63
|
+
def method_missing(method, other_node)
|
64
|
+
if [:<, :<=, :>, :>=].include?(method)
|
65
|
+
@priority.send(method, other_node.send(:priority))
|
66
|
+
else
|
67
|
+
super
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
attr_reader :data, :priority
|
74
|
+
end
|
data/lib/ruby_structures.rb
CHANGED
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_structures
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Numeroff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Ruby implementations of a Stack, Queue, Linked List, Binary Tree, LRU
|
14
|
-
Cache and
|
14
|
+
Cache, Heap and Priority Queue. More to come!
|
15
15
|
email: jnumeroff@hotmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
@@ -21,6 +21,7 @@ files:
|
|
21
21
|
- lib/data_structures/heap.rb
|
22
22
|
- lib/data_structures/linked_list.rb
|
23
23
|
- lib/data_structures/lru_cache.rb
|
24
|
+
- lib/data_structures/priority_queue.rb
|
24
25
|
- lib/data_structures/queue.rb
|
25
26
|
- lib/data_structures/stack.rb
|
26
27
|
- lib/ruby_structures.rb
|
@@ -44,7 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
45
|
version: '0'
|
45
46
|
requirements: []
|
46
47
|
rubyforge_project:
|
47
|
-
rubygems_version: 2.
|
48
|
+
rubygems_version: 2.7.3
|
48
49
|
signing_key:
|
49
50
|
specification_version: 4
|
50
51
|
summary: Ruby Data Structures
|