adt_utilit 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/adt_utilit/linked_list.rb +21 -20
- data/lib/adt_utilit/queue.rb +37 -0
- data/lib/adt_utilit/stack.rb +105 -0
- data/lib/adt_utilit/stack_queue.rb +121 -0
- data/lib/adt_utilit.rb +1 -0
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e8ee1f39f314d0f7ca04f595bd4b474d361aa2f
|
4
|
+
data.tar.gz: 9e26d7ba45ad54ddb0bff7b5f4860e146957a415
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e13664fee3e3cfcf5b2587d966accf1f2c7ab520d921109a161fae1f57098a2c57b1389c6a35994866713f10955890497afab20353b547f15030dcadc7a47a3f
|
7
|
+
data.tar.gz: cf2dfcd78512d7de2110ab393c3acb621a5d97b97deb12baa38d27c57af897c8e26bf3da5e9a8ad1f8290369855ef25ca8e86ea635e9641784ef74d39f798880
|
@@ -1,14 +1,13 @@
|
|
1
1
|
require_relative 'linked_list_node.rb'
|
2
|
-
require "byebug"
|
3
2
|
class LinkedList
|
4
3
|
include Enumerable
|
5
4
|
attr_reader :length, :head, :tail
|
6
5
|
|
7
|
-
def initialize(value)
|
8
|
-
if value.
|
6
|
+
def initialize(value = nil)
|
7
|
+
if value.nil?
|
8
|
+
nodes = []
|
9
|
+
elsif value.is_a?(Array)
|
9
10
|
nodes = initialize_with_array(value)
|
10
|
-
elsif value.is_a?(Hash)
|
11
|
-
nodes = initialize_with_hash(value)
|
12
11
|
else
|
13
12
|
nodes = [LinkedListNode.new(value)]
|
14
13
|
end
|
@@ -52,8 +51,14 @@ class LinkedList
|
|
52
51
|
else
|
53
52
|
node = LinkedListNode.new(value)
|
54
53
|
end
|
55
|
-
|
56
|
-
@
|
54
|
+
|
55
|
+
if @length == 0
|
56
|
+
@head = @tail = node
|
57
|
+
else
|
58
|
+
@tail.connect(node)
|
59
|
+
@tail = node
|
60
|
+
end
|
61
|
+
|
57
62
|
@length +=1
|
58
63
|
self
|
59
64
|
end
|
@@ -64,8 +69,14 @@ class LinkedList
|
|
64
69
|
else
|
65
70
|
node = LinkedListNode.new(value)
|
66
71
|
end
|
67
|
-
|
68
|
-
@
|
72
|
+
|
73
|
+
if @length == 0
|
74
|
+
@head = @tail = node
|
75
|
+
else
|
76
|
+
node.connect(@head)
|
77
|
+
@head = node
|
78
|
+
end
|
79
|
+
|
69
80
|
@length +=1
|
70
81
|
self
|
71
82
|
end
|
@@ -84,7 +95,7 @@ class LinkedList
|
|
84
95
|
shifted_node
|
85
96
|
end
|
86
97
|
|
87
|
-
def
|
98
|
+
def find_node(value)
|
88
99
|
self.each do |node|
|
89
100
|
return node if node.value == value
|
90
101
|
end
|
@@ -148,14 +159,4 @@ class LinkedList
|
|
148
159
|
nodes
|
149
160
|
end
|
150
161
|
|
151
|
-
def initialize_with_hash(hash)
|
152
|
-
nodes = []
|
153
|
-
hash.map do |key, value|
|
154
|
-
node = LinkedListNode.new({key => value})
|
155
|
-
nodes[-1].connect(node) unless nodes[-1].nil?
|
156
|
-
nodes << node
|
157
|
-
end
|
158
|
-
nodes
|
159
|
-
end
|
160
|
-
|
161
162
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Queue
|
2
|
+
include Enumerable
|
3
|
+
def initialize(value = nil)
|
4
|
+
if value.nil?
|
5
|
+
@queue = []
|
6
|
+
elsif value.is_a?(Array)
|
7
|
+
@queue = value
|
8
|
+
else
|
9
|
+
@queue = [value]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def each(&prc)
|
14
|
+
@queue.each(&prc)
|
15
|
+
end
|
16
|
+
|
17
|
+
def length
|
18
|
+
@queue.length
|
19
|
+
end
|
20
|
+
|
21
|
+
def enqueue(value)
|
22
|
+
@queue.push(value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def dequeue
|
26
|
+
@queue.shift
|
27
|
+
end
|
28
|
+
|
29
|
+
def peek
|
30
|
+
@queue.first
|
31
|
+
end
|
32
|
+
|
33
|
+
def last
|
34
|
+
@queue.last
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
class Stack
|
2
|
+
include Enumerable
|
3
|
+
def initialize(value = nil)
|
4
|
+
@stack = []
|
5
|
+
if value.nil?
|
6
|
+
return
|
7
|
+
elsif value.is_a?(Array)
|
8
|
+
value.each do |el|
|
9
|
+
push(el)
|
10
|
+
end
|
11
|
+
else
|
12
|
+
push(value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.new_with_metadata(metadata_name, &prc)
|
17
|
+
stack = self.new
|
18
|
+
stack.instance_variable_set(:@metadata, {max: "#{metadata_name}_max", min: "#{metadata_name}_min", proc: prc})
|
19
|
+
|
20
|
+
define_method("#{metadata_name}_max"){
|
21
|
+
raise "such metadata has not been set for this instance" if @metadata.nil?
|
22
|
+
return nil if @stack.empty?
|
23
|
+
@stack.last[@metadata[:max]]
|
24
|
+
}
|
25
|
+
define_method("#{metadata_name}_min"){
|
26
|
+
raise "such metadata has not been set for this instance" if @metadata.nil?
|
27
|
+
return nil if @stack.empty?
|
28
|
+
@stack.last[@metadata[:min]]
|
29
|
+
}
|
30
|
+
stack
|
31
|
+
end
|
32
|
+
|
33
|
+
def peek
|
34
|
+
return nil if @stack.empty?
|
35
|
+
@stack.last[:value]
|
36
|
+
end
|
37
|
+
|
38
|
+
def min
|
39
|
+
return nil if @stack.empty?
|
40
|
+
@stack.last[:min]
|
41
|
+
end
|
42
|
+
|
43
|
+
def max
|
44
|
+
return nil if @stack.empty?
|
45
|
+
@stack.last[:max]
|
46
|
+
end
|
47
|
+
|
48
|
+
def length
|
49
|
+
@stack.length
|
50
|
+
end
|
51
|
+
|
52
|
+
def empty?
|
53
|
+
@stack.length == 0
|
54
|
+
end
|
55
|
+
|
56
|
+
def push(value)
|
57
|
+
stack_el = {value: value}
|
58
|
+
if @stack.empty?
|
59
|
+
stack_el[:max] = stack_el[:min] = stack_el[:value]
|
60
|
+
unless @metadata.nil?
|
61
|
+
stack_el[@metadata[:max]] = stack_el[@metadata[:min]] = stack_el[:value]
|
62
|
+
end
|
63
|
+
else
|
64
|
+
stack_el[:max] = stack_el[:value] >= max ? stack_el[:value] : max
|
65
|
+
stack_el[:min] = stack_el[:value] <= min ? stack_el[:value] : min
|
66
|
+
if @metadata
|
67
|
+
proc = @metadata[:proc]
|
68
|
+
|
69
|
+
if proc.call(stack_el[:value]) >= proc.call(self.send("#{@metadata[:max]}"))
|
70
|
+
stack_el[@metadata[:max]] = stack_el[:value]
|
71
|
+
else
|
72
|
+
stack_el[@metadata[:max]] = self.send("#{@metadata[:max]}")
|
73
|
+
end
|
74
|
+
|
75
|
+
if proc.call(stack_el[:value]) <= proc.call(self.send("#{@metadata[:min]}"))
|
76
|
+
stack_el[@metadata[:min]] = stack_el[:value]
|
77
|
+
else
|
78
|
+
stack_el[@metadata[:min]] = self.send("#{@metadata[:min]}")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
@stack << stack_el
|
83
|
+
self
|
84
|
+
end
|
85
|
+
|
86
|
+
def pop
|
87
|
+
return nil if @stack.empty?
|
88
|
+
top_el = @stack.pop
|
89
|
+
top_el[:value]
|
90
|
+
end
|
91
|
+
|
92
|
+
def each(&prc)
|
93
|
+
@stack.each do |el|
|
94
|
+
prc.call(el[:value])
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def each_from_top(&prc)
|
99
|
+
(@stack.length - 1).downto(0) do |i|
|
100
|
+
prc.call(@stack[i][:value])
|
101
|
+
end
|
102
|
+
self
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require_relative "queue"
|
2
|
+
require_relative "stack"
|
3
|
+
class StackQueue
|
4
|
+
include Enumerable
|
5
|
+
def initialize(value = nil)
|
6
|
+
@stack_a = Stack.new
|
7
|
+
@stack_b = Stack.new
|
8
|
+
if !value.nil?
|
9
|
+
if value.is_a?(Array)
|
10
|
+
value.each { |el| @stack_a.push(el) }
|
11
|
+
else
|
12
|
+
@stack_a.push(value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def peek
|
18
|
+
slinky if @stack_b.empty?
|
19
|
+
@stack_b.peek
|
20
|
+
end
|
21
|
+
|
22
|
+
def last
|
23
|
+
if @stack_a.empty?
|
24
|
+
@stack_b_bottom
|
25
|
+
else
|
26
|
+
@stack_a.peek
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def enqueue(value)
|
31
|
+
@stack_a.push(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def dequeue
|
35
|
+
return nil if self.empty?
|
36
|
+
slinky if @stack_b.empty?
|
37
|
+
@stack_b.pop
|
38
|
+
end
|
39
|
+
|
40
|
+
def min
|
41
|
+
return nil if self.empty?
|
42
|
+
return @stack_a.min if @stack_b.min.nil?
|
43
|
+
return @stack_b.min if @stack_a.min.nil?
|
44
|
+
[@stack_a.min, @stack_b.min].min
|
45
|
+
end
|
46
|
+
|
47
|
+
def max
|
48
|
+
return nil if self.empty?
|
49
|
+
return @stack_a.max if @stack_b.max.nil?
|
50
|
+
return @stack_b.max if @stack_a.max.nil?
|
51
|
+
[@stack_a.max, @stack_b.max].max
|
52
|
+
end
|
53
|
+
|
54
|
+
def length
|
55
|
+
@stack_a.length + @stack_b.length
|
56
|
+
end
|
57
|
+
|
58
|
+
def empty?
|
59
|
+
@stack_a.empty? && @stack_b.empty?
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.new_with_metadata(metadata_name, &prc)
|
63
|
+
stack_queue = self.new
|
64
|
+
stack_queue.instance_variable_set(:@metadata, {
|
65
|
+
max: "#{metadata_name}_max",
|
66
|
+
min: "#{metadata_name}_min",
|
67
|
+
proc: prc
|
68
|
+
})
|
69
|
+
stack_queue.instance_variable_set(:@stack_a, Stack.new_with_metadata(metadata_name, &prc))
|
70
|
+
stack_queue.instance_variable_set(:@stack_b, Stack.new_with_metadata(metadata_name, &prc))
|
71
|
+
|
72
|
+
define_method("#{metadata_name}_max") {
|
73
|
+
raise "such metadata has not been set for this instance" if @metadata.nil?
|
74
|
+
return nil if self.empty?
|
75
|
+
prc = @metadata[:proc]
|
76
|
+
stack_a_max = @stack_a.send(@metadata[:max])
|
77
|
+
stack_b_max = @stack_b.send(@metadata[:max])
|
78
|
+
|
79
|
+
return stack_b_max if stack_a_max.nil?
|
80
|
+
return stack_a_max if stack_b_max.nil?
|
81
|
+
if prc.call(stack_a_max) < prc.call(stack_b_max)
|
82
|
+
return stack_b_max
|
83
|
+
else
|
84
|
+
return stack_a_max
|
85
|
+
end
|
86
|
+
}
|
87
|
+
|
88
|
+
define_method("#{metadata_name}_min"){
|
89
|
+
raise "such metadata has not been set for this instance" if @metadata.nil?
|
90
|
+
return nil if self.empty?
|
91
|
+
prc = @metadata[:proc]
|
92
|
+
stack_a_min = @stack_a.send(@metadata[:min])
|
93
|
+
stack_b_min = @stack_b.send(@metadata[:min])
|
94
|
+
|
95
|
+
return stack_b_min if stack_a_min.nil?
|
96
|
+
return stack_a_min if stack_b_min.nil?
|
97
|
+
if prc.call(stack_a_min) < prc.call(stack_b_min)
|
98
|
+
return stack_b_min
|
99
|
+
else
|
100
|
+
return stack_a_min
|
101
|
+
end
|
102
|
+
}
|
103
|
+
|
104
|
+
return stack_queue
|
105
|
+
end
|
106
|
+
|
107
|
+
def each(&prc)
|
108
|
+
@stack_b.each_from_top(&prc)
|
109
|
+
@stack_a.each(&prc)
|
110
|
+
self
|
111
|
+
end
|
112
|
+
|
113
|
+
private
|
114
|
+
def slinky
|
115
|
+
if @stack_b.empty?
|
116
|
+
@stack_b_bottom = @stack_a.peek
|
117
|
+
@stack_b.push(@stack_a.pop) until @stack_a.empty?
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
data/lib/adt_utilit.rb
CHANGED
metadata
CHANGED
@@ -1,16 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adt_utilit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hirosvk
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-07-
|
11
|
+
date: 2016-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: "<in development> AdtUtilit is a Ruby gem that provides a set of useful
|
14
|
+
data type classes such as Linked List, Queue, Stack, StackQueue, and Polytree. Many
|
15
|
+
intuitive methods are available for each class. See documentations for the details."
|
14
16
|
email: kajino.hiroyuki@gmail.com
|
15
17
|
executables: []
|
16
18
|
extensions: []
|
@@ -19,6 +21,9 @@ files:
|
|
19
21
|
- lib/adt_utilit.rb
|
20
22
|
- lib/adt_utilit/linked_list.rb
|
21
23
|
- lib/adt_utilit/linked_list_node.rb
|
24
|
+
- lib/adt_utilit/queue.rb
|
25
|
+
- lib/adt_utilit/stack.rb
|
26
|
+
- lib/adt_utilit/stack_queue.rb
|
22
27
|
homepage: http://rubygems.org/gems/adt_utilit
|
23
28
|
licenses:
|
24
29
|
- MIT
|