stakach-algorithms 1.0.7 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/algorithms/version.rb +1 -1
- data/lib/containers/heap.rb +7 -3
- data/spec/deque_spec.rb +7 -7
- data/spec/rb_tree_map_spec.rb +6 -7
- data/spec/splay_tree_map_spec.rb +8 -8
- data/spec/string_spec.rb +6 -6
- metadata +1 -1
data/lib/algorithms/version.rb
CHANGED
data/lib/containers/heap.rb
CHANGED
@@ -9,7 +9,9 @@
|
|
9
9
|
|
10
10
|
This library implements a Fibonacci heap, which allows O(1) complexity for most methods.
|
11
11
|
=end
|
12
|
-
|
12
|
+
module Algorithms
|
13
|
+
module Containers
|
14
|
+
class Heap
|
13
15
|
include Enumerable
|
14
16
|
|
15
17
|
# call-seq:
|
@@ -418,7 +420,7 @@ class Containers::Heap
|
|
418
420
|
end
|
419
421
|
|
420
422
|
# A MaxHeap is a heap where the items are returned in descending order of key value.
|
421
|
-
class
|
423
|
+
class MaxHeap < Heap
|
422
424
|
|
423
425
|
# call-seq:
|
424
426
|
# MaxHeap.new(ary) -> new_heap
|
@@ -460,7 +462,7 @@ class Containers::MaxHeap < Containers::Heap
|
|
460
462
|
end
|
461
463
|
|
462
464
|
# A MinHeap is a heap where the items are returned in ascending order of key value.
|
463
|
-
class
|
465
|
+
class MinHeap < Heap
|
464
466
|
|
465
467
|
# call-seq:
|
466
468
|
# MinHeap.new(ary) -> new_heap
|
@@ -499,4 +501,6 @@ class Containers::MinHeap < Containers::Heap
|
|
499
501
|
def min!
|
500
502
|
self.pop
|
501
503
|
end
|
504
|
+
end
|
505
|
+
end
|
502
506
|
end
|
data/spec/deque_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
|
3
|
+
shared_examples_for "(empty deque)" do
|
4
4
|
it "should return nil when popping objects" do
|
5
5
|
@deque.pop_front.should be_nil
|
6
6
|
@deque.pop_back.should be_nil
|
@@ -30,7 +30,7 @@ shared_examples "(empty deque)" do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
shared_examples_for "(non-empty deque)" do
|
34
34
|
before(:each) do
|
35
35
|
@deque.push_back(10)
|
36
36
|
@deque.push_back("10")
|
@@ -76,30 +76,30 @@ end
|
|
76
76
|
|
77
77
|
describe "empty rubydeque" do
|
78
78
|
before(:each) do
|
79
|
-
@deque = Containers::RubyDeque.new
|
79
|
+
@deque = Algorithms::Containers::RubyDeque.new
|
80
80
|
end
|
81
81
|
it_should_behave_like "(empty deque)"
|
82
82
|
end
|
83
83
|
|
84
84
|
describe "full rubydeque" do
|
85
85
|
before(:each) do
|
86
|
-
@deque = Containers::RubyDeque.new
|
86
|
+
@deque = Algorithms::Containers::RubyDeque.new
|
87
87
|
end
|
88
88
|
it_should_behave_like "(non-empty deque)"
|
89
89
|
end
|
90
90
|
|
91
91
|
begin
|
92
|
-
Containers::CDeque
|
92
|
+
Algorithms::Containers::CDeque
|
93
93
|
describe "empty cdeque" do
|
94
94
|
before(:each) do
|
95
|
-
@deque = Containers::CDeque.new
|
95
|
+
@deque = Algorithms::Containers::CDeque.new
|
96
96
|
end
|
97
97
|
it_should_behave_like "(empty deque)"
|
98
98
|
end
|
99
99
|
|
100
100
|
describe "full cdeque" do
|
101
101
|
before(:each) do
|
102
|
-
@deque = Containers::CDeque.new
|
102
|
+
@deque = Algorithms::Containers::CDeque.new
|
103
103
|
end
|
104
104
|
it_should_behave_like "(non-empty deque)"
|
105
105
|
end
|
data/spec/rb_tree_map_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
-
|
3
|
+
shared_examples_for "empty rbtree" do
|
4
4
|
it "should let you push stuff in" do
|
5
5
|
100.times { |x| @tree[x] = x }
|
6
6
|
@tree.size.should eql(100)
|
@@ -30,7 +30,7 @@ shared_examples "empty rbtree" do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
shared_examples_for "non-empty rbtree" do
|
34
34
|
before(:each) do
|
35
35
|
@num_items = 1000
|
36
36
|
@random_array = Array.new(@num_items) { rand(@num_items) }
|
@@ -91,30 +91,29 @@ end
|
|
91
91
|
|
92
92
|
describe "empty rbtreemap" do
|
93
93
|
before(:each) do
|
94
|
-
@tree = Containers::RubyRBTreeMap.new
|
94
|
+
@tree = Algorithms::Containers::RubyRBTreeMap.new
|
95
95
|
end
|
96
96
|
it_should_behave_like "empty rbtree"
|
97
97
|
end
|
98
98
|
|
99
99
|
describe "full rbtreemap" do
|
100
100
|
before(:each) do
|
101
|
-
@tree = Containers::RubyRBTreeMap.new
|
101
|
+
@tree = Algorithms::Containers::RubyRBTreeMap.new
|
102
102
|
end
|
103
103
|
it_should_behave_like "non-empty rbtree"
|
104
104
|
end
|
105
105
|
|
106
106
|
begin
|
107
|
-
Containers::CRBTreeMap
|
108
107
|
describe "empty crbtreemap" do
|
109
108
|
before(:each) do
|
110
|
-
@tree = Containers::CRBTreeMap.new
|
109
|
+
@tree = Algorithms::Containers::CRBTreeMap.new
|
111
110
|
end
|
112
111
|
it_should_behave_like "empty rbtree"
|
113
112
|
end
|
114
113
|
|
115
114
|
describe "full crbtreemap" do
|
116
115
|
before(:each) do
|
117
|
-
@tree = Containers::CRBTreeMap.new
|
116
|
+
@tree = Algorithms::Containers::CRBTreeMap.new
|
118
117
|
end
|
119
118
|
it_should_behave_like "non-empty rbtree"
|
120
119
|
end
|
data/spec/splay_tree_map_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'helper'
|
2
|
-
|
3
|
-
|
2
|
+
|
3
|
+
shared_examples_for "empty splaytree" do
|
4
4
|
it "should let you push stuff in" do
|
5
5
|
100.times { |x| @tree[x] = x }
|
6
6
|
@tree.size.should eql(100)
|
@@ -24,7 +24,7 @@ shared_examples "empty splaytree" do
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
|
27
|
+
shared_examples_for "non-empty splaytree" do
|
28
28
|
before(:each) do
|
29
29
|
@num_items = 100
|
30
30
|
@random_array = []
|
@@ -74,30 +74,30 @@ end
|
|
74
74
|
|
75
75
|
describe "empty splaytreemap" do
|
76
76
|
before(:each) do
|
77
|
-
@tree = Containers::RubySplayTreeMap.new
|
77
|
+
@tree = Algorithms::Containers::RubySplayTreeMap.new
|
78
78
|
end
|
79
79
|
it_should_behave_like "empty splaytree"
|
80
80
|
end
|
81
81
|
|
82
82
|
describe "full splaytreemap" do
|
83
83
|
before(:each) do
|
84
|
-
@tree = Containers::RubySplayTreeMap.new
|
84
|
+
@tree = Algorithms::Containers::RubySplayTreeMap.new
|
85
85
|
end
|
86
86
|
it_should_behave_like "non-empty splaytree"
|
87
87
|
end
|
88
88
|
|
89
89
|
begin
|
90
|
-
Containers::CSplayTreeMap
|
90
|
+
Algorithms::Containers::CSplayTreeMap
|
91
91
|
describe "empty csplaytreemap" do
|
92
92
|
before(:each) do
|
93
|
-
@tree = Containers::CSplayTreeMap.new
|
93
|
+
@tree = Algorithms::Containers::CSplayTreeMap.new
|
94
94
|
end
|
95
95
|
it_should_behave_like "empty splaytree"
|
96
96
|
end
|
97
97
|
|
98
98
|
describe "full csplaytreemap" do
|
99
99
|
before(:each) do
|
100
|
-
@tree = Containers::CSplayTreeMap.new
|
100
|
+
@tree = Algorithms::Containers::CSplayTreeMap.new
|
101
101
|
end
|
102
102
|
it_should_behave_like "non-empty splaytree"
|
103
103
|
end
|
data/spec/string_spec.rb
CHANGED
@@ -2,11 +2,11 @@ require 'helper'
|
|
2
2
|
|
3
3
|
describe "string algorithms" do
|
4
4
|
it "should do levenshtein distance" do
|
5
|
-
Algorithms::
|
6
|
-
Algorithms::
|
7
|
-
Algorithms::
|
8
|
-
Algorithms::
|
9
|
-
Algorithms::
|
10
|
-
Algorithms::
|
5
|
+
Algorithms::String.levenshtein_dist("Hello", "Hel").should eql(2)
|
6
|
+
Algorithms::String.levenshtein_dist("Hello", "").should eql(5)
|
7
|
+
Algorithms::String.levenshtein_dist("", "Hello").should eql(5)
|
8
|
+
Algorithms::String.levenshtein_dist("Hello", "Hello").should eql(0)
|
9
|
+
Algorithms::String.levenshtein_dist("Hello", "ello").should eql(1)
|
10
|
+
Algorithms::String.levenshtein_dist("Hello", "Mello").should eql(1)
|
11
11
|
end
|
12
12
|
end
|