stakach-algorithms 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +97 -97
- data/Rakefile +27 -27
- data/ext/algorithms/string/extconf.rb +4 -4
- data/ext/algorithms/string/string.c +70 -70
- data/ext/containers/bst/bst.c +249 -249
- data/ext/containers/bst/extconf.rb +4 -4
- data/ext/containers/deque/deque.c +248 -248
- data/ext/containers/deque/extconf.rb +4 -4
- data/ext/containers/rbtree_map/extconf.rb +4 -4
- data/ext/containers/rbtree_map/rbtree.c +500 -500
- data/ext/containers/splaytree_map/extconf.rb +4 -4
- data/ext/containers/splaytree_map/splaytree.c +421 -421
- data/lib/algorithms/search.rb +85 -85
- data/lib/algorithms/sort.rb +242 -242
- data/lib/algorithms/string.rb +10 -10
- data/lib/algorithms/version.rb +1 -1
- data/lib/algorithms.rb +69 -69
- data/lib/containers/deque.rb +176 -176
- data/lib/containers/heap.rb +506 -506
- data/lib/containers/kd_tree.rb +112 -112
- data/lib/containers/priority_queue.rb +116 -116
- data/lib/containers/queue.rb +71 -71
- data/lib/containers/rb_tree_map.rb +402 -402
- data/lib/containers/splay_tree_map.rb +273 -273
- data/lib/containers/stack.rb +70 -70
- data/lib/containers/suffix_array.rb +71 -71
- data/lib/containers/trie.rb +187 -187
- metadata +3 -3
data/lib/algorithms/string.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
This module implements string algorithms. Documentation is provided for each algorithm.
|
3
|
-
|
4
|
-
=end
|
5
|
-
|
6
|
-
|
7
|
-
begin
|
8
|
-
require 'CString' unless RUBY_PLATFORM =~ /java/
|
9
|
-
rescue
|
10
|
-
end
|
1
|
+
=begin rdoc
|
2
|
+
This module implements string algorithms. Documentation is provided for each algorithm.
|
3
|
+
|
4
|
+
=end
|
5
|
+
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'CString' unless RUBY_PLATFORM =~ /java/
|
9
|
+
rescue
|
10
|
+
end
|
data/lib/algorithms/version.rb
CHANGED
data/lib/algorithms.rb
CHANGED
@@ -1,69 +1,69 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
The 'Algorithms and Containers' library is an effort to provide a set of commonly used
|
3
|
-
algorithms and containers to Ruby programmers.
|
4
|
-
|
5
|
-
This is a Google Summer of Code 2008 project
|
6
|
-
|
7
|
-
Written by Kanwei Li, mentored by Austin Ziegler
|
8
|
-
|
9
|
-
To avoid typing Algorithms::Containers::xxx to initialize containers, include the Algorithms namespace and Containers module.
|
10
|
-
|
11
|
-
require 'algorithms'
|
12
|
-
include Algorithms
|
13
|
-
include Containers
|
14
|
-
|
15
|
-
tree = RBTreeMap.new
|
16
|
-
|
17
|
-
instead of:
|
18
|
-
|
19
|
-
require 'algorithms'
|
20
|
-
|
21
|
-
tree = Algorithms::Containers::RBTreeMap.new
|
22
|
-
|
23
|
-
Done so far:
|
24
|
-
* Heaps - Algorithms::Containers::Heap, Algorithms::Containers::MaxHeap, Algorithms::Containers::MinHeap
|
25
|
-
* Priority Queue - Algorithms::Containers::PriorityQueue
|
26
|
-
* Stack - Algorithms::Containers::Stack
|
27
|
-
* Queue - Algorithms::Containers::Queue
|
28
|
-
* Deque - Algorithms::Containers::Deque, Algorithms::Containers::CDeque (C extension), Algorithms::Containers::RubyDeque
|
29
|
-
* Red-Black Trees - Algorithms::Containers::RBTreeMap, Algorithms::Containers::CRBTreeMap (C extension), Algorithms::Containers::RubyRBTreeMap
|
30
|
-
* Splay Trees - Algorithms::Containers::SplayTreeMap
|
31
|
-
* Tries - Algorithms::Containers::Trie
|
32
|
-
* Suffix Array - Algorithms::Containers::SuffixArray
|
33
|
-
* kd Tree - Algorithms::Containers::KDTree
|
34
|
-
|
35
|
-
* Search algorithms
|
36
|
-
- Binary Search - Algorithms::Algorithms::Search.binary_search
|
37
|
-
- Knuth-Morris-Pratt - Algorithms::Algorithms::Search.kmp_search
|
38
|
-
* Sort algorithms
|
39
|
-
- Bubble sort - Algorithms::Algorithms::Sort.bubble_sort
|
40
|
-
- Comb sort - Algorithms::Algorithms::Sort.comb_sort
|
41
|
-
- Selection sort - Algorithms::Algorithms::Sort.selection_sort
|
42
|
-
- Heapsort - Algorithms::Algorithms::Sort.heapsort
|
43
|
-
- Insertion sort - Algorithms::Algorithms::Sort.insertion_sort
|
44
|
-
- Shell sort - Algorithms::Algorithms::Sort.shell_sort
|
45
|
-
- Quicksort - Algorithms::Algorithms::Sort.quicksort
|
46
|
-
- Mergesort - Algorithms::Algorithms::Sort.mergesort
|
47
|
-
* String algorithms
|
48
|
-
- Levenshtein distance - Algorithms::Algorithms::String.levenshtein_dist
|
49
|
-
=end
|
50
|
-
|
51
|
-
module Algorithms
|
52
|
-
module Algorithms; end
|
53
|
-
module Containers; end
|
54
|
-
end
|
55
|
-
|
56
|
-
require 'algorithms/search'
|
57
|
-
require 'algorithms/sort'
|
58
|
-
require 'algorithms/string'
|
59
|
-
require 'containers/heap'
|
60
|
-
require 'containers/stack'
|
61
|
-
require 'containers/deque'
|
62
|
-
require 'containers/queue'
|
63
|
-
require 'containers/priority_queue'
|
64
|
-
require 'containers/rb_tree_map'
|
65
|
-
require 'containers/splay_tree_map'
|
66
|
-
require 'containers/suffix_array'
|
67
|
-
require 'containers/trie'
|
68
|
-
require 'containers/kd_tree'
|
69
|
-
|
1
|
+
=begin rdoc
|
2
|
+
The 'Algorithms and Containers' library is an effort to provide a set of commonly used
|
3
|
+
algorithms and containers to Ruby programmers.
|
4
|
+
|
5
|
+
This is a Google Summer of Code 2008 project
|
6
|
+
|
7
|
+
Written by Kanwei Li, mentored by Austin Ziegler
|
8
|
+
|
9
|
+
To avoid typing Algorithms::Containers::xxx to initialize containers, include the Algorithms namespace and Containers module.
|
10
|
+
|
11
|
+
require 'algorithms'
|
12
|
+
include Algorithms
|
13
|
+
include Containers
|
14
|
+
|
15
|
+
tree = RBTreeMap.new
|
16
|
+
|
17
|
+
instead of:
|
18
|
+
|
19
|
+
require 'algorithms'
|
20
|
+
|
21
|
+
tree = Algorithms::Containers::RBTreeMap.new
|
22
|
+
|
23
|
+
Done so far:
|
24
|
+
* Heaps - Algorithms::Containers::Heap, Algorithms::Containers::MaxHeap, Algorithms::Containers::MinHeap
|
25
|
+
* Priority Queue - Algorithms::Containers::PriorityQueue
|
26
|
+
* Stack - Algorithms::Containers::Stack
|
27
|
+
* Queue - Algorithms::Containers::Queue
|
28
|
+
* Deque - Algorithms::Containers::Deque, Algorithms::Containers::CDeque (C extension), Algorithms::Containers::RubyDeque
|
29
|
+
* Red-Black Trees - Algorithms::Containers::RBTreeMap, Algorithms::Containers::CRBTreeMap (C extension), Algorithms::Containers::RubyRBTreeMap
|
30
|
+
* Splay Trees - Algorithms::Containers::SplayTreeMap
|
31
|
+
* Tries - Algorithms::Containers::Trie
|
32
|
+
* Suffix Array - Algorithms::Containers::SuffixArray
|
33
|
+
* kd Tree - Algorithms::Containers::KDTree
|
34
|
+
|
35
|
+
* Search algorithms
|
36
|
+
- Binary Search - Algorithms::Algorithms::Search.binary_search
|
37
|
+
- Knuth-Morris-Pratt - Algorithms::Algorithms::Search.kmp_search
|
38
|
+
* Sort algorithms
|
39
|
+
- Bubble sort - Algorithms::Algorithms::Sort.bubble_sort
|
40
|
+
- Comb sort - Algorithms::Algorithms::Sort.comb_sort
|
41
|
+
- Selection sort - Algorithms::Algorithms::Sort.selection_sort
|
42
|
+
- Heapsort - Algorithms::Algorithms::Sort.heapsort
|
43
|
+
- Insertion sort - Algorithms::Algorithms::Sort.insertion_sort
|
44
|
+
- Shell sort - Algorithms::Algorithms::Sort.shell_sort
|
45
|
+
- Quicksort - Algorithms::Algorithms::Sort.quicksort
|
46
|
+
- Mergesort - Algorithms::Algorithms::Sort.mergesort
|
47
|
+
* String algorithms
|
48
|
+
- Levenshtein distance - Algorithms::Algorithms::String.levenshtein_dist
|
49
|
+
=end
|
50
|
+
|
51
|
+
module Algorithms
|
52
|
+
module Algorithms; end
|
53
|
+
module Containers; end
|
54
|
+
end
|
55
|
+
|
56
|
+
require 'algorithms/search'
|
57
|
+
require 'algorithms/sort'
|
58
|
+
require 'algorithms/string'
|
59
|
+
require 'containers/heap'
|
60
|
+
require 'containers/stack'
|
61
|
+
require 'containers/deque'
|
62
|
+
require 'containers/queue'
|
63
|
+
require 'containers/priority_queue'
|
64
|
+
require 'containers/rb_tree_map'
|
65
|
+
require 'containers/splay_tree_map'
|
66
|
+
require 'containers/suffix_array'
|
67
|
+
require 'containers/trie'
|
68
|
+
require 'containers/kd_tree'
|
69
|
+
|
data/lib/containers/deque.rb
CHANGED
@@ -1,176 +1,176 @@
|
|
1
|
-
=begin rdoc
|
2
|
-
A Deque is a container that allows items to be added and removed from both the front and back,
|
3
|
-
acting as a combination of a Stack and Queue.
|
4
|
-
|
5
|
-
This implementation uses a doubly-linked list, guaranteeing O(1) complexity for all operations.
|
6
|
-
=end
|
7
|
-
module Algorithms
|
8
|
-
module Containers
|
9
|
-
class RubyDeque
|
10
|
-
include Enumerable
|
11
|
-
|
12
|
-
Node = Struct.new(:left, :right, :obj)
|
13
|
-
|
14
|
-
# Create a new Deque. Takes an optional array argument to initialize the Deque.
|
15
|
-
#
|
16
|
-
# d = Containers::Deque.new([1, 2, 3])
|
17
|
-
# d.front #=> 1
|
18
|
-
# d.back #=> 3
|
19
|
-
def initialize(ary=[])
|
20
|
-
@front = nil
|
21
|
-
@back = nil
|
22
|
-
@size = 0
|
23
|
-
ary.to_a.each { |obj| push_back(obj) }
|
24
|
-
end
|
25
|
-
|
26
|
-
# Returns true if the Deque is empty, false otherwise.
|
27
|
-
def empty?
|
28
|
-
@size == 0
|
29
|
-
end
|
30
|
-
|
31
|
-
# Removes all the objects in the Deque.
|
32
|
-
def clear
|
33
|
-
@front = @back = nil
|
34
|
-
@size = 0
|
35
|
-
end
|
36
|
-
|
37
|
-
# Return the number of items in the Deque.
|
38
|
-
#
|
39
|
-
# d = Containers::Deque.new([1, 2, 3])
|
40
|
-
# d.size #=> 3
|
41
|
-
def size
|
42
|
-
@size
|
43
|
-
end
|
44
|
-
alias_method :length, :size
|
45
|
-
|
46
|
-
# Returns the object at the front of the Deque but does not remove it.
|
47
|
-
#
|
48
|
-
# d = Containers::Deque.new
|
49
|
-
# d.push_front(1)
|
50
|
-
# d.push_front(2)
|
51
|
-
# d.front #=> 2
|
52
|
-
def front
|
53
|
-
@front && @front.obj
|
54
|
-
end
|
55
|
-
|
56
|
-
# Returns the object at the back of the Deque but does not remove it.
|
57
|
-
#
|
58
|
-
# d = Containers::Deque.new
|
59
|
-
# d.push_front(1)
|
60
|
-
# d.push_front(2)
|
61
|
-
# d.back #=> 1
|
62
|
-
def back
|
63
|
-
@back && @back.obj
|
64
|
-
end
|
65
|
-
|
66
|
-
# Adds an object at the front of the Deque.
|
67
|
-
#
|
68
|
-
# d = Containers::Deque.new([1, 2, 3])
|
69
|
-
# d.push_front(0)
|
70
|
-
# d.pop_front #=> 0
|
71
|
-
def push_front(obj)
|
72
|
-
node = Node.new(nil, nil, obj)
|
73
|
-
if @front
|
74
|
-
node.right = @front
|
75
|
-
@front.left = node
|
76
|
-
@front = node
|
77
|
-
else
|
78
|
-
@front = @back = node
|
79
|
-
end
|
80
|
-
@size += 1
|
81
|
-
obj
|
82
|
-
end
|
83
|
-
|
84
|
-
# Adds an object at the back of the Deque.
|
85
|
-
#
|
86
|
-
# d = Containers::Deque.new([1, 2, 3])
|
87
|
-
# d.push_back(4)
|
88
|
-
# d.pop_back #=> 4
|
89
|
-
def push_back(obj)
|
90
|
-
node = Node.new(nil, nil, obj)
|
91
|
-
if @back
|
92
|
-
node.left = @back
|
93
|
-
@back.right = node
|
94
|
-
@back = node
|
95
|
-
else
|
96
|
-
@front = @back = node
|
97
|
-
end
|
98
|
-
@size += 1
|
99
|
-
obj
|
100
|
-
end
|
101
|
-
|
102
|
-
# Returns the object at the front of the Deque and removes it.
|
103
|
-
#
|
104
|
-
# d = Containers::Deque.new
|
105
|
-
# d.push_front(1)
|
106
|
-
# d.push_front(2)
|
107
|
-
# d.pop_front #=> 2
|
108
|
-
# d.size #=> 1
|
109
|
-
def pop_front
|
110
|
-
return nil unless @front
|
111
|
-
node = @front
|
112
|
-
if @size == 1
|
113
|
-
clear
|
114
|
-
return node.obj
|
115
|
-
else
|
116
|
-
@front.right.left = nil
|
117
|
-
@front = @front.right
|
118
|
-
end
|
119
|
-
@size -= 1
|
120
|
-
node.obj
|
121
|
-
end
|
122
|
-
|
123
|
-
# Returns the object at the back of the Deque and removes it.
|
124
|
-
#
|
125
|
-
# d = Containers::Deque.new
|
126
|
-
# d.push_front(1)
|
127
|
-
# d.push_front(2)
|
128
|
-
# d.pop_back #=> 1
|
129
|
-
# d.size #=> 1
|
130
|
-
def pop_back
|
131
|
-
return nil unless @back
|
132
|
-
node = @back
|
133
|
-
if @size == 1
|
134
|
-
clear
|
135
|
-
return node.obj
|
136
|
-
else
|
137
|
-
@back.left.right = nil
|
138
|
-
@back = @back.left
|
139
|
-
end
|
140
|
-
@size -= 1
|
141
|
-
node.obj
|
142
|
-
end
|
143
|
-
|
144
|
-
# Iterate over the Deque in FIFO order.
|
145
|
-
def each_forward
|
146
|
-
return unless @front
|
147
|
-
node = @front
|
148
|
-
while node
|
149
|
-
yield node.obj
|
150
|
-
node = node.right
|
151
|
-
end
|
152
|
-
end
|
153
|
-
alias_method :each, :each_forward
|
154
|
-
|
155
|
-
# Iterate over the Deque in LIFO order.
|
156
|
-
def each_backward
|
157
|
-
return unless @back
|
158
|
-
node = @back
|
159
|
-
while node
|
160
|
-
yield node.obj
|
161
|
-
node = node.left
|
162
|
-
end
|
163
|
-
end
|
164
|
-
alias_method :reverse_each, :each_backward
|
165
|
-
|
166
|
-
end
|
167
|
-
end
|
168
|
-
|
169
|
-
begin
|
170
|
-
require 'CDeque'
|
171
|
-
Containers::Deque = Containers::CDeque
|
172
|
-
rescue
|
173
|
-
Containers::Deque = Containers::RubyDeque
|
174
|
-
end
|
175
|
-
|
176
|
-
end
|
1
|
+
=begin rdoc
|
2
|
+
A Deque is a container that allows items to be added and removed from both the front and back,
|
3
|
+
acting as a combination of a Stack and Queue.
|
4
|
+
|
5
|
+
This implementation uses a doubly-linked list, guaranteeing O(1) complexity for all operations.
|
6
|
+
=end
|
7
|
+
module Algorithms
|
8
|
+
module Containers
|
9
|
+
class RubyDeque
|
10
|
+
include Enumerable
|
11
|
+
|
12
|
+
Node = Struct.new(:left, :right, :obj)
|
13
|
+
|
14
|
+
# Create a new Deque. Takes an optional array argument to initialize the Deque.
|
15
|
+
#
|
16
|
+
# d = Containers::Deque.new([1, 2, 3])
|
17
|
+
# d.front #=> 1
|
18
|
+
# d.back #=> 3
|
19
|
+
def initialize(ary=[])
|
20
|
+
@front = nil
|
21
|
+
@back = nil
|
22
|
+
@size = 0
|
23
|
+
ary.to_a.each { |obj| push_back(obj) }
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns true if the Deque is empty, false otherwise.
|
27
|
+
def empty?
|
28
|
+
@size == 0
|
29
|
+
end
|
30
|
+
|
31
|
+
# Removes all the objects in the Deque.
|
32
|
+
def clear
|
33
|
+
@front = @back = nil
|
34
|
+
@size = 0
|
35
|
+
end
|
36
|
+
|
37
|
+
# Return the number of items in the Deque.
|
38
|
+
#
|
39
|
+
# d = Containers::Deque.new([1, 2, 3])
|
40
|
+
# d.size #=> 3
|
41
|
+
def size
|
42
|
+
@size
|
43
|
+
end
|
44
|
+
alias_method :length, :size
|
45
|
+
|
46
|
+
# Returns the object at the front of the Deque but does not remove it.
|
47
|
+
#
|
48
|
+
# d = Containers::Deque.new
|
49
|
+
# d.push_front(1)
|
50
|
+
# d.push_front(2)
|
51
|
+
# d.front #=> 2
|
52
|
+
def front
|
53
|
+
@front && @front.obj
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the object at the back of the Deque but does not remove it.
|
57
|
+
#
|
58
|
+
# d = Containers::Deque.new
|
59
|
+
# d.push_front(1)
|
60
|
+
# d.push_front(2)
|
61
|
+
# d.back #=> 1
|
62
|
+
def back
|
63
|
+
@back && @back.obj
|
64
|
+
end
|
65
|
+
|
66
|
+
# Adds an object at the front of the Deque.
|
67
|
+
#
|
68
|
+
# d = Containers::Deque.new([1, 2, 3])
|
69
|
+
# d.push_front(0)
|
70
|
+
# d.pop_front #=> 0
|
71
|
+
def push_front(obj)
|
72
|
+
node = Node.new(nil, nil, obj)
|
73
|
+
if @front
|
74
|
+
node.right = @front
|
75
|
+
@front.left = node
|
76
|
+
@front = node
|
77
|
+
else
|
78
|
+
@front = @back = node
|
79
|
+
end
|
80
|
+
@size += 1
|
81
|
+
obj
|
82
|
+
end
|
83
|
+
|
84
|
+
# Adds an object at the back of the Deque.
|
85
|
+
#
|
86
|
+
# d = Containers::Deque.new([1, 2, 3])
|
87
|
+
# d.push_back(4)
|
88
|
+
# d.pop_back #=> 4
|
89
|
+
def push_back(obj)
|
90
|
+
node = Node.new(nil, nil, obj)
|
91
|
+
if @back
|
92
|
+
node.left = @back
|
93
|
+
@back.right = node
|
94
|
+
@back = node
|
95
|
+
else
|
96
|
+
@front = @back = node
|
97
|
+
end
|
98
|
+
@size += 1
|
99
|
+
obj
|
100
|
+
end
|
101
|
+
|
102
|
+
# Returns the object at the front of the Deque and removes it.
|
103
|
+
#
|
104
|
+
# d = Containers::Deque.new
|
105
|
+
# d.push_front(1)
|
106
|
+
# d.push_front(2)
|
107
|
+
# d.pop_front #=> 2
|
108
|
+
# d.size #=> 1
|
109
|
+
def pop_front
|
110
|
+
return nil unless @front
|
111
|
+
node = @front
|
112
|
+
if @size == 1
|
113
|
+
clear
|
114
|
+
return node.obj
|
115
|
+
else
|
116
|
+
@front.right.left = nil
|
117
|
+
@front = @front.right
|
118
|
+
end
|
119
|
+
@size -= 1
|
120
|
+
node.obj
|
121
|
+
end
|
122
|
+
|
123
|
+
# Returns the object at the back of the Deque and removes it.
|
124
|
+
#
|
125
|
+
# d = Containers::Deque.new
|
126
|
+
# d.push_front(1)
|
127
|
+
# d.push_front(2)
|
128
|
+
# d.pop_back #=> 1
|
129
|
+
# d.size #=> 1
|
130
|
+
def pop_back
|
131
|
+
return nil unless @back
|
132
|
+
node = @back
|
133
|
+
if @size == 1
|
134
|
+
clear
|
135
|
+
return node.obj
|
136
|
+
else
|
137
|
+
@back.left.right = nil
|
138
|
+
@back = @back.left
|
139
|
+
end
|
140
|
+
@size -= 1
|
141
|
+
node.obj
|
142
|
+
end
|
143
|
+
|
144
|
+
# Iterate over the Deque in FIFO order.
|
145
|
+
def each_forward
|
146
|
+
return unless @front
|
147
|
+
node = @front
|
148
|
+
while node
|
149
|
+
yield node.obj
|
150
|
+
node = node.right
|
151
|
+
end
|
152
|
+
end
|
153
|
+
alias_method :each, :each_forward
|
154
|
+
|
155
|
+
# Iterate over the Deque in LIFO order.
|
156
|
+
def each_backward
|
157
|
+
return unless @back
|
158
|
+
node = @back
|
159
|
+
while node
|
160
|
+
yield node.obj
|
161
|
+
node = node.left
|
162
|
+
end
|
163
|
+
end
|
164
|
+
alias_method :reverse_each, :each_backward
|
165
|
+
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
begin
|
170
|
+
require 'CDeque'
|
171
|
+
Containers::Deque = Containers::CDeque
|
172
|
+
rescue # C Version could not be found, try ruby version
|
173
|
+
Containers::Deque = Containers::RubyDeque
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|