kanwei-algorithms 0.2.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.
- data/History.txt +168 -0
- data/Manifest +41 -0
- data/README.markdown +92 -0
- data/Rakefile +31 -0
- data/algorithms.gemspec +32 -0
- data/benchmarks/deque.rb +17 -0
- data/benchmarks/sorts.rb +34 -0
- data/benchmarks/treemaps.rb +36 -0
- data/ext/containers/deque/deque.c +247 -0
- data/ext/containers/deque/extconf.rb +4 -0
- data/ext/containers/rbtree_map/extconf.rb +4 -0
- data/ext/containers/rbtree_map/rbtree.c +473 -0
- data/ext/containers/splaytree_map/extconf.rb +4 -0
- data/ext/containers/splaytree_map/splaytree.c +370 -0
- data/lib/algorithms/search.rb +84 -0
- data/lib/algorithms/sort.rb +238 -0
- data/lib/algorithms.rb +67 -0
- data/lib/containers/deque.rb +171 -0
- data/lib/containers/heap.rb +486 -0
- data/lib/containers/kd_tree.rb +87 -0
- data/lib/containers/priority_queue.rb +113 -0
- data/lib/containers/queue.rb +68 -0
- data/lib/containers/rb_tree_map.rb +398 -0
- data/lib/containers/splay_tree_map.rb +269 -0
- data/lib/containers/stack.rb +67 -0
- data/lib/containers/suffix_array.rb +68 -0
- data/lib/containers/trie.rb +182 -0
- data/spec/deque_gc_mark_spec.rb +18 -0
- data/spec/deque_spec.rb +108 -0
- data/spec/heap_spec.rb +126 -0
- data/spec/kd_tree_spec.rb +4 -0
- data/spec/priority_queue_spec.rb +75 -0
- data/spec/queue_spec.rb +61 -0
- data/spec/rb_tree_map_gc_mark_spec.rb +25 -0
- data/spec/rb_tree_map_spec.rb +123 -0
- data/spec/search_spec.rb +28 -0
- data/spec/sort_spec.rb +28 -0
- data/spec/splay_tree_map_spec.rb +102 -0
- data/spec/stack_spec.rb +60 -0
- data/spec/suffix_array_spec.rb +40 -0
- data/spec/trie_spec.rb +59 -0
- metadata +120 -0
data/spec/search_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
|
4
|
+
class String; include Algorithms::Search; end
|
5
|
+
|
6
|
+
describe "search algorithms" do
|
7
|
+
it "should binary search sorted arrays" do
|
8
|
+
n = 1000
|
9
|
+
@rand_array = Array.new(n) { rand(n) }.sort
|
10
|
+
|
11
|
+
Algorithms::Search.binary_search(@rand_array, @rand_array.first).should eql(@rand_array.first)
|
12
|
+
Algorithms::Search.binary_search(@rand_array, 999999).should be_nil
|
13
|
+
Algorithms::Search.binary_search(@rand_array, nil).should be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should use kmp_search to find substrings it has" do
|
17
|
+
string = "ABC ABCDAB ABCDABCDABDE"
|
18
|
+
Algorithms::Search.kmp_search(string, "ABCDABD").should eql(15)
|
19
|
+
Algorithms::Search.kmp_search(string, "ABCDEF").should be_nil
|
20
|
+
Algorithms::Search.kmp_search(string, nil).should be_nil
|
21
|
+
Algorithms::Search.kmp_search(string, "").should be_nil
|
22
|
+
Algorithms::Search.kmp_search(nil, "ABCD").should be_nil
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should let you include Search in String to enable instance methods" do
|
26
|
+
"ABC ABCDAB ABCDABCDABDE".kmp_search("ABCDABD").should eql(15)
|
27
|
+
end
|
28
|
+
end
|
data/spec/sort_spec.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
include Algorithms
|
4
|
+
|
5
|
+
describe "sort algorithms" do
|
6
|
+
before(:each) do
|
7
|
+
@sorts = %w(bubble_sort comb_sort selection_sort heapsort insertion_sort shell_sort quicksort mergesort)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should work for empty containers" do
|
11
|
+
empty_array = []
|
12
|
+
@sorts.each { |sort| Sort.send(sort, empty_array).should eql([]) }
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should work for a container of size 1" do
|
16
|
+
one_array = [1]
|
17
|
+
@sorts.each { |sort| Sort.send(sort, one_array).should eql(one_array) }
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should work for random arrays of numbers" do
|
21
|
+
n = 500
|
22
|
+
rand_array = Array.new(n) { rand(n) }
|
23
|
+
sorted_array = rand_array.sort
|
24
|
+
|
25
|
+
@sorts.each { |sort| Sort.send(sort, rand_array.dup).should eql(sorted_array) }
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
|
4
|
+
describe "empty splaytree", :shared => true do
|
5
|
+
it "should let you push stuff in" do
|
6
|
+
100.times { |x| @tree[x] = x }
|
7
|
+
@tree.size.should eql(100)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return 0 for size" do
|
11
|
+
@tree.size.should eql(0)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return nil for #min and #max" do
|
15
|
+
@tree.min.should be_nil
|
16
|
+
@tree.max.should be_nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should return nil for #delete" do
|
20
|
+
@tree.delete(:non_existing).should be_nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "non-empty splaytree", :shared => true do
|
25
|
+
before(:each) do
|
26
|
+
@num_items = 100
|
27
|
+
@random_array = []
|
28
|
+
@num_items.times { @random_array << rand(@num_items) }
|
29
|
+
@random_array.each { |x| @tree[x] = x }
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return correct size (uniqify items first)" do
|
33
|
+
@tree.size.should eql(@random_array.uniq.size)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should have correct height (worst case is when items are inserted in order, and height = num items inserted)" do
|
37
|
+
@tree.clear
|
38
|
+
10.times { |x| @tree[x] = x }
|
39
|
+
@tree.height.should eql(10)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return correct max and min keys" do
|
43
|
+
@tree.min[0].should eql(@random_array.min)
|
44
|
+
@tree.max[0].should eql(@random_array.max)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should not #has_key? keys it doesn't have" do
|
48
|
+
@tree.has_key?(10000).should be_false
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should #has_key? keys it does have" do
|
52
|
+
@tree.has_key?(@random_array[0]).should be_true
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should remove any key" do
|
56
|
+
random_key = @random_array[rand(@num_items)]
|
57
|
+
@tree.has_key?(random_key).should be_true
|
58
|
+
@tree.delete(random_key).should eql(random_key)
|
59
|
+
@tree.has_key?(random_key).should be_false
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should let you iterate with #each" do
|
63
|
+
counter = 0
|
64
|
+
sorted_array = @random_array.uniq.sort
|
65
|
+
@tree.each do |key, val|
|
66
|
+
key.should eql(sorted_array[counter])
|
67
|
+
counter += 1
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "empty splaytreemap" do
|
73
|
+
before(:each) do
|
74
|
+
@tree = Containers::RubySplayTreeMap.new
|
75
|
+
end
|
76
|
+
it_should_behave_like "empty splaytree"
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "full splaytreemap" do
|
80
|
+
before(:each) do
|
81
|
+
@tree = Containers::RubySplayTreeMap.new
|
82
|
+
end
|
83
|
+
it_should_behave_like "non-empty splaytree"
|
84
|
+
end
|
85
|
+
|
86
|
+
begin
|
87
|
+
Containers::CSplayTreeMap
|
88
|
+
describe "empty csplaytreemap" do
|
89
|
+
before(:each) do
|
90
|
+
@tree = Containers::CSplayTreeMap.new
|
91
|
+
end
|
92
|
+
it_should_behave_like "empty splaytree"
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "full csplaytreemap" do
|
96
|
+
before(:each) do
|
97
|
+
@tree = Containers::CSplayTreeMap.new
|
98
|
+
end
|
99
|
+
it_should_behave_like "non-empty splaytree"
|
100
|
+
end
|
101
|
+
rescue Exception
|
102
|
+
end
|
data/spec/stack_spec.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
|
4
|
+
describe "empty stack" do
|
5
|
+
before(:each) do
|
6
|
+
@stack = Containers::Stack.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return nil when sent #pop" do
|
10
|
+
@stack.pop.should be_nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a size of 1 when sent #push" do
|
14
|
+
@stack.push(1)
|
15
|
+
@stack.size.should eql(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should return nil when sent #next" do
|
19
|
+
@stack.next.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return empty?" do
|
23
|
+
@stack.empty?.should be_true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "non-empty stack" do
|
28
|
+
before(:each) do
|
29
|
+
@stack = Containers::Stack.new
|
30
|
+
@stack.push(10)
|
31
|
+
@stack.push("10")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return last pushed object" do
|
35
|
+
@stack.pop.should eql("10")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should return the size" do
|
39
|
+
@stack.size.should eql(2)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not return empty?" do
|
43
|
+
@stack.empty?.should be_false
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
it "should iterate in LIFO order" do
|
48
|
+
arr = []
|
49
|
+
@stack.each { |obj| arr << obj }
|
50
|
+
arr.should eql(["10", 10])
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should return nil after all pops" do
|
54
|
+
@stack.pop
|
55
|
+
@stack.pop
|
56
|
+
@stack.pop.should be_nil
|
57
|
+
@stack.next.should be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
|
4
|
+
describe "empty suffix array" do
|
5
|
+
it "should not initialize with empty string" do
|
6
|
+
lambda { Containers::SuffixArray.new("") }.should raise_error
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "non-empty suffix array" do
|
11
|
+
before(:each) do
|
12
|
+
@s_array = Containers::SuffixArray.new("abracadabra")
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should has_substring? each possible substring" do
|
16
|
+
@s_array.has_substring?("a").should be_true
|
17
|
+
@s_array.has_substring?("abra").should be_true
|
18
|
+
@s_array.has_substring?("abracadabra").should be_true
|
19
|
+
@s_array.has_substring?("acadabra").should be_true
|
20
|
+
@s_array.has_substring?("adabra").should be_true
|
21
|
+
@s_array.has_substring?("bra").should be_true
|
22
|
+
@s_array.has_substring?("bracadabra").should be_true
|
23
|
+
@s_array.has_substring?("cadabra").should be_true
|
24
|
+
@s_array.has_substring?("dabra").should be_true
|
25
|
+
@s_array.has_substring?("ra").should be_true
|
26
|
+
@s_array.has_substring?("racadabra").should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should not has_substring? substrings it does not have" do
|
30
|
+
@s_array.has_substring?("nope").should be_false
|
31
|
+
@s_array.has_substring?(nil).should be_false
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should work with numbers (calls to_s)" do
|
35
|
+
number = Containers::SuffixArray.new(123456789)
|
36
|
+
number[1].should be_true
|
37
|
+
number.has_substring?(12).should be_true
|
38
|
+
number.has_substring?(13).should be_false
|
39
|
+
end
|
40
|
+
end
|
data/spec/trie_spec.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
|
4
|
+
describe "empty trie" do
|
5
|
+
before(:each) do
|
6
|
+
@trie = Containers::Trie.new
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not get or has_key?" do
|
10
|
+
@trie.get("anything").should be_nil
|
11
|
+
@trie.has_key?("anything").should be_false
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should not have longest_prefix or match wildcards" do
|
15
|
+
@trie.wildcard("an*thing").should eql([])
|
16
|
+
@trie.longest_prefix("an*thing").should eql("")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "non-empty trie" do
|
21
|
+
before(:each) do
|
22
|
+
@trie = Containers::Trie.new
|
23
|
+
@trie.push("Hello", "World")
|
24
|
+
@trie.push("Hilly", "World")
|
25
|
+
@trie.push("Hello, brother", "World")
|
26
|
+
@trie.push("Hello, bob", "World")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should has_key? keys it has" do
|
30
|
+
@trie.has_key?("Hello").should be_true
|
31
|
+
@trie.has_key?("Hello, brother").should be_true
|
32
|
+
@trie.has_key?("Hello, bob").should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should not has_key? keys it doesn't have" do
|
36
|
+
@trie.has_key?("Nope").should be_false
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should get values" do
|
40
|
+
@trie.get("Hello").should eql("World")
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should overwrite values" do
|
44
|
+
@trie.push("Hello", "John")
|
45
|
+
@trie.get("Hello").should eql("John")
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return longest prefix" do
|
49
|
+
@trie.longest_prefix("Hello, brandon").should eql("Hello")
|
50
|
+
@trie.longest_prefix("Hel").should eql("")
|
51
|
+
@trie.longest_prefix("Hello").should eql("Hello")
|
52
|
+
@trie.longest_prefix("Hello, bob").should eql("Hello, bob")
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should match wildcards" do
|
56
|
+
@trie.wildcard("H*ll.").should eql(["Hello", "Hilly"])
|
57
|
+
@trie.wildcard("Hel").should eql([])
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kanwei-algorithms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kanwei Li
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-29 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: A library of algorithms and containers.
|
17
|
+
email: kanwei@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions:
|
21
|
+
- ext/containers/deque/extconf.rb
|
22
|
+
- ext/containers/rbtree_map/extconf.rb
|
23
|
+
- ext/containers/splaytree_map/extconf.rb
|
24
|
+
extra_rdoc_files:
|
25
|
+
- ext/containers/deque/deque.c
|
26
|
+
- ext/containers/deque/extconf.rb
|
27
|
+
- ext/containers/rbtree_map/extconf.rb
|
28
|
+
- ext/containers/rbtree_map/rbtree.c
|
29
|
+
- ext/containers/splaytree_map/extconf.rb
|
30
|
+
- ext/containers/splaytree_map/splaytree.c
|
31
|
+
- lib/algorithms/search.rb
|
32
|
+
- lib/algorithms/sort.rb
|
33
|
+
- lib/algorithms.rb
|
34
|
+
- lib/containers/deque.rb
|
35
|
+
- lib/containers/heap.rb
|
36
|
+
- lib/containers/kd_tree.rb
|
37
|
+
- lib/containers/priority_queue.rb
|
38
|
+
- lib/containers/queue.rb
|
39
|
+
- lib/containers/rb_tree_map.rb
|
40
|
+
- lib/containers/splay_tree_map.rb
|
41
|
+
- lib/containers/stack.rb
|
42
|
+
- lib/containers/suffix_array.rb
|
43
|
+
- lib/containers/trie.rb
|
44
|
+
- README.markdown
|
45
|
+
files:
|
46
|
+
- algorithms.gemspec
|
47
|
+
- benchmarks/deque.rb
|
48
|
+
- benchmarks/sorts.rb
|
49
|
+
- benchmarks/treemaps.rb
|
50
|
+
- ext/containers/deque/deque.c
|
51
|
+
- ext/containers/deque/extconf.rb
|
52
|
+
- ext/containers/rbtree_map/extconf.rb
|
53
|
+
- ext/containers/rbtree_map/rbtree.c
|
54
|
+
- ext/containers/splaytree_map/extconf.rb
|
55
|
+
- ext/containers/splaytree_map/splaytree.c
|
56
|
+
- History.txt
|
57
|
+
- lib/algorithms/search.rb
|
58
|
+
- lib/algorithms/sort.rb
|
59
|
+
- lib/algorithms.rb
|
60
|
+
- lib/containers/deque.rb
|
61
|
+
- lib/containers/heap.rb
|
62
|
+
- lib/containers/kd_tree.rb
|
63
|
+
- lib/containers/priority_queue.rb
|
64
|
+
- lib/containers/queue.rb
|
65
|
+
- lib/containers/rb_tree_map.rb
|
66
|
+
- lib/containers/splay_tree_map.rb
|
67
|
+
- lib/containers/stack.rb
|
68
|
+
- lib/containers/suffix_array.rb
|
69
|
+
- lib/containers/trie.rb
|
70
|
+
- Manifest
|
71
|
+
- Rakefile
|
72
|
+
- README.markdown
|
73
|
+
- spec/deque_gc_mark_spec.rb
|
74
|
+
- spec/deque_spec.rb
|
75
|
+
- spec/heap_spec.rb
|
76
|
+
- spec/kd_tree_spec.rb
|
77
|
+
- spec/priority_queue_spec.rb
|
78
|
+
- spec/queue_spec.rb
|
79
|
+
- spec/rb_tree_map_gc_mark_spec.rb
|
80
|
+
- spec/rb_tree_map_spec.rb
|
81
|
+
- spec/search_spec.rb
|
82
|
+
- spec/sort_spec.rb
|
83
|
+
- spec/splay_tree_map_spec.rb
|
84
|
+
- spec/stack_spec.rb
|
85
|
+
- spec/suffix_array_spec.rb
|
86
|
+
- spec/trie_spec.rb
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://rubyforge.org/projects/algorithms/
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options:
|
91
|
+
- --line-numbers
|
92
|
+
- --inline-source
|
93
|
+
- --title
|
94
|
+
- Algorithms
|
95
|
+
- --main
|
96
|
+
- README.markdown
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
- ext
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: "0"
|
105
|
+
version:
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: "1.2"
|
111
|
+
version:
|
112
|
+
requirements: []
|
113
|
+
|
114
|
+
rubyforge_project: algorithms
|
115
|
+
rubygems_version: 1.2.0
|
116
|
+
signing_key:
|
117
|
+
specification_version: 2
|
118
|
+
summary: A library of algorithms and containers.
|
119
|
+
test_files: []
|
120
|
+
|