algorithms 0.2.0 → 1.0.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 +7 -0
- data/{History.txt → CHANGELOG.markdown} +25 -0
- data/Gemfile +9 -0
- data/Manifest +16 -6
- data/README.markdown +40 -66
- data/Rakefile +16 -25
- data/algorithms.gemspec +14 -24
- data/benchmarks/treemaps.rb +25 -10
- data/ext/algorithms/string/extconf.rb +4 -0
- data/ext/algorithms/string/string.c +68 -0
- data/ext/containers/bst/bst.c +247 -0
- data/ext/containers/bst/extconf.rb +4 -0
- data/ext/containers/deque/deque.c +3 -3
- data/ext/containers/rbtree_map/rbtree.c +43 -18
- data/ext/containers/splaytree_map/splaytree.c +154 -105
- data/lib/algorithms.rb +5 -6
- data/lib/algorithms/sort.rb +130 -0
- data/lib/algorithms/string.rb +9 -0
- data/lib/containers/heap.rb +16 -0
- data/lib/containers/kd_tree.rb +40 -17
- data/lib/containers/trie.rb +1 -1
- data/spec/bst_gc_mark_spec.rb +25 -0
- data/spec/bst_spec.rb +25 -0
- data/spec/deque_gc_mark_spec.rb +1 -1
- data/spec/deque_spec.rb +20 -20
- data/spec/heap_spec.rb +28 -23
- data/spec/kd_expected_out.txt +10000 -0
- data/spec/kd_test_in.txt +10000 -0
- data/spec/kd_tree_spec.rb +31 -1
- data/spec/{rb_tree_map_gc_mark_spec.rb → map_gc_mark_spec.rb} +10 -6
- data/spec/priority_queue_spec.rb +20 -20
- data/spec/queue_spec.rb +10 -10
- data/spec/rb_tree_map_spec.rb +25 -25
- data/spec/search_spec.rb +9 -9
- data/spec/sort_spec.rb +6 -5
- data/spec/splay_tree_map_spec.rb +21 -17
- data/spec/stack_spec.rb +10 -10
- data/spec/string_spec.rb +15 -0
- data/spec/suffix_array_spec.rb +17 -17
- data/spec/trie_spec.rb +16 -16
- metadata +49 -62
data/spec/stack_spec.rb
CHANGED
@@ -7,20 +7,20 @@ describe "empty stack" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should return nil when sent #pop" do
|
10
|
-
@stack.pop.
|
10
|
+
expect(@stack.pop).to be_nil
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should return a size of 1 when sent #push" do
|
14
14
|
@stack.push(1)
|
15
|
-
@stack.size.
|
15
|
+
expect(@stack.size).to eql(1)
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should return nil when sent #next" do
|
19
|
-
@stack.next.
|
19
|
+
expect(@stack.next).to be_nil
|
20
20
|
end
|
21
21
|
|
22
22
|
it "should return empty?" do
|
23
|
-
@stack.empty
|
23
|
+
expect(@stack.empty?).to be true
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -32,29 +32,29 @@ describe "non-empty stack" do
|
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should return last pushed object" do
|
35
|
-
@stack.pop.
|
35
|
+
expect(@stack.pop).to eql("10")
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should return the size" do
|
39
|
-
@stack.size.
|
39
|
+
expect(@stack.size).to eql(2)
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should not return empty?" do
|
43
|
-
@stack.empty
|
43
|
+
expect(@stack.empty?).to be false
|
44
44
|
end
|
45
45
|
|
46
46
|
|
47
47
|
it "should iterate in LIFO order" do
|
48
48
|
arr = []
|
49
49
|
@stack.each { |obj| arr << obj }
|
50
|
-
arr.
|
50
|
+
expect(arr).to eql(["10", 10])
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should return nil after all pops" do
|
54
54
|
@stack.pop
|
55
55
|
@stack.pop
|
56
|
-
@stack.pop.
|
57
|
-
@stack.next.
|
56
|
+
expect(@stack.pop).to be_nil
|
57
|
+
expect(@stack.next).to be_nil
|
58
58
|
end
|
59
59
|
|
60
60
|
end
|
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
2
|
+
require 'algorithms'
|
3
|
+
|
4
|
+
if defined? Algorithms::String
|
5
|
+
describe "string algorithms" do
|
6
|
+
it "should do levenshtein distance" do
|
7
|
+
expect(Algorithms::String.levenshtein_dist("Hello", "Hel")).to eql(2)
|
8
|
+
expect(Algorithms::String.levenshtein_dist("Hello", "")).to eql(5)
|
9
|
+
expect(Algorithms::String.levenshtein_dist("", "Hello")).to eql(5)
|
10
|
+
expect(Algorithms::String.levenshtein_dist("Hello", "Hello")).to eql(0)
|
11
|
+
expect(Algorithms::String.levenshtein_dist("Hello", "ello")).to eql(1)
|
12
|
+
expect(Algorithms::String.levenshtein_dist("Hello", "Mello")).to eql(1)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/suffix_array_spec.rb
CHANGED
@@ -3,7 +3,7 @@ require 'algorithms'
|
|
3
3
|
|
4
4
|
describe "empty suffix array" do
|
5
5
|
it "should not initialize with empty string" do
|
6
|
-
|
6
|
+
expect { Containers::SuffixArray.new("") }.to raise_error(ArgumentError)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -13,28 +13,28 @@ describe "non-empty suffix array" do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should has_substring? each possible substring" do
|
16
|
-
@s_array.has_substring?("a").
|
17
|
-
@s_array.has_substring?("abra").
|
18
|
-
@s_array.has_substring?("abracadabra").
|
19
|
-
@s_array.has_substring?("acadabra").
|
20
|
-
@s_array.has_substring?("adabra").
|
21
|
-
@s_array.has_substring?("bra").
|
22
|
-
@s_array.has_substring?("bracadabra").
|
23
|
-
@s_array.has_substring?("cadabra").
|
24
|
-
@s_array.has_substring?("dabra").
|
25
|
-
@s_array.has_substring?("ra").
|
26
|
-
@s_array.has_substring?("racadabra").
|
16
|
+
expect(@s_array.has_substring?("a")).to be true
|
17
|
+
expect(@s_array.has_substring?("abra")).to be true
|
18
|
+
expect(@s_array.has_substring?("abracadabra")).to be true
|
19
|
+
expect(@s_array.has_substring?("acadabra")).to be true
|
20
|
+
expect(@s_array.has_substring?("adabra")).to be true
|
21
|
+
expect(@s_array.has_substring?("bra")).to be true
|
22
|
+
expect(@s_array.has_substring?("bracadabra")).to be true
|
23
|
+
expect(@s_array.has_substring?("cadabra")).to be true
|
24
|
+
expect(@s_array.has_substring?("dabra")).to be true
|
25
|
+
expect(@s_array.has_substring?("ra")).to be true
|
26
|
+
expect(@s_array.has_substring?("racadabra")).to be true
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should not has_substring? substrings it does not have" do
|
30
|
-
@s_array.has_substring?("nope").
|
31
|
-
@s_array.has_substring?(nil).
|
30
|
+
expect(@s_array.has_substring?("nope")).to be false
|
31
|
+
expect(@s_array.has_substring?(nil)).to be false
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should work with numbers (calls to_s)" do
|
35
35
|
number = Containers::SuffixArray.new(123456789)
|
36
|
-
number[1].
|
37
|
-
number.has_substring?(12).
|
38
|
-
number.has_substring?(13).
|
36
|
+
expect(number[1]).to be true
|
37
|
+
expect(number.has_substring?(12)).to be true
|
38
|
+
expect(number.has_substring?(13)).to be false
|
39
39
|
end
|
40
40
|
end
|
data/spec/trie_spec.rb
CHANGED
@@ -7,13 +7,13 @@ describe "empty trie" do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "should not get or has_key?" do
|
10
|
-
@trie.get("anything").
|
11
|
-
@trie.has_key?("anything").
|
10
|
+
expect(@trie.get("anything")).to be_nil
|
11
|
+
expect(@trie.has_key?("anything")).to be false
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should not have longest_prefix or match wildcards" do
|
15
|
-
@trie.wildcard("an*thing").
|
16
|
-
@trie.longest_prefix("an*thing").
|
15
|
+
expect(@trie.wildcard("an*thing")).to eql([])
|
16
|
+
expect(@trie.longest_prefix("an*thing")).to eql("")
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -27,33 +27,33 @@ describe "non-empty trie" do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should has_key? keys it has" do
|
30
|
-
@trie.has_key?("Hello").
|
31
|
-
@trie.has_key?("Hello, brother").
|
32
|
-
@trie.has_key?("Hello, bob").
|
30
|
+
expect(@trie.has_key?("Hello")).to be true
|
31
|
+
expect(@trie.has_key?("Hello, brother")).to be true
|
32
|
+
expect(@trie.has_key?("Hello, bob")).to be true
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should not has_key? keys it doesn't have" do
|
36
|
-
@trie.has_key?("Nope").
|
36
|
+
expect(@trie.has_key?("Nope")).to be false
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should get values" do
|
40
|
-
@trie.get("Hello").
|
40
|
+
expect(@trie.get("Hello")).to eql("World")
|
41
41
|
end
|
42
42
|
|
43
43
|
it "should overwrite values" do
|
44
44
|
@trie.push("Hello", "John")
|
45
|
-
@trie.get("Hello").
|
45
|
+
expect(@trie.get("Hello")).to eql("John")
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should return longest prefix" do
|
49
|
-
@trie.longest_prefix("Hello, brandon").
|
50
|
-
@trie.longest_prefix("Hel").
|
51
|
-
@trie.longest_prefix("Hello").
|
52
|
-
@trie.longest_prefix("Hello, bob").
|
49
|
+
expect(@trie.longest_prefix("Hello, brandon")).to eql("Hello")
|
50
|
+
expect(@trie.longest_prefix("Hel")).to eql("")
|
51
|
+
expect(@trie.longest_prefix("Hello")).to eql("Hello")
|
52
|
+
expect(@trie.longest_prefix("Hello, bob")).to eql("Hello, bob")
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should match wildcards" do
|
56
|
-
@trie.wildcard("H*ll.").
|
57
|
-
@trie.wildcard("Hel").
|
56
|
+
expect(@trie.wildcard("H*ll.")).to eql(["Hello", "Hilly"])
|
57
|
+
expect(@trie.wildcard("Hel")).to eql([])
|
58
58
|
end
|
59
59
|
end
|
metadata
CHANGED
@@ -1,62 +1,50 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: algorithms
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Kanwei Li
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
date: 2009-03-29 00:00:00 -04:00
|
13
|
-
default_executable:
|
11
|
+
date: 2021-04-04 00:00:00.000000000 Z
|
14
12
|
dependencies: []
|
15
|
-
|
16
|
-
|
13
|
+
description: Heap, Priority Queue, Deque, Stack, Queue, Red-Black Trees, Splay Trees,
|
14
|
+
sorting algorithms, and more
|
17
15
|
email: kanwei@gmail.com
|
18
16
|
executables: []
|
19
|
-
|
20
|
-
|
17
|
+
extensions:
|
18
|
+
- ext/algorithms/string/extconf.rb
|
19
|
+
- ext/containers/bst/extconf.rb
|
21
20
|
- ext/containers/deque/extconf.rb
|
22
21
|
- ext/containers/rbtree_map/extconf.rb
|
23
22
|
- ext/containers/splaytree_map/extconf.rb
|
24
|
-
extra_rdoc_files:
|
25
|
-
|
26
|
-
-
|
27
|
-
-
|
28
|
-
-
|
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
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- CHANGELOG.markdown
|
26
|
+
- Gemfile
|
27
|
+
- Manifest
|
44
28
|
- README.markdown
|
45
|
-
|
29
|
+
- Rakefile
|
46
30
|
- algorithms.gemspec
|
47
31
|
- benchmarks/deque.rb
|
48
32
|
- benchmarks/sorts.rb
|
49
33
|
- benchmarks/treemaps.rb
|
34
|
+
- ext/algorithms/string/extconf.rb
|
35
|
+
- ext/algorithms/string/string.c
|
36
|
+
- ext/containers/bst/bst.c
|
37
|
+
- ext/containers/bst/extconf.rb
|
50
38
|
- ext/containers/deque/deque.c
|
51
39
|
- ext/containers/deque/extconf.rb
|
52
40
|
- ext/containers/rbtree_map/extconf.rb
|
53
41
|
- ext/containers/rbtree_map/rbtree.c
|
54
42
|
- ext/containers/splaytree_map/extconf.rb
|
55
43
|
- ext/containers/splaytree_map/splaytree.c
|
56
|
-
-
|
44
|
+
- lib/algorithms.rb
|
57
45
|
- lib/algorithms/search.rb
|
58
46
|
- lib/algorithms/sort.rb
|
59
|
-
- lib/algorithms.rb
|
47
|
+
- lib/algorithms/string.rb
|
60
48
|
- lib/containers/deque.rb
|
61
49
|
- lib/containers/heap.rb
|
62
50
|
- lib/containers/kd_tree.rb
|
@@ -67,54 +55,53 @@ files:
|
|
67
55
|
- lib/containers/stack.rb
|
68
56
|
- lib/containers/suffix_array.rb
|
69
57
|
- lib/containers/trie.rb
|
70
|
-
-
|
71
|
-
-
|
72
|
-
- README.markdown
|
58
|
+
- spec/bst_gc_mark_spec.rb
|
59
|
+
- spec/bst_spec.rb
|
73
60
|
- spec/deque_gc_mark_spec.rb
|
74
61
|
- spec/deque_spec.rb
|
75
62
|
- spec/heap_spec.rb
|
63
|
+
- spec/kd_expected_out.txt
|
64
|
+
- spec/kd_test_in.txt
|
76
65
|
- spec/kd_tree_spec.rb
|
66
|
+
- spec/map_gc_mark_spec.rb
|
77
67
|
- spec/priority_queue_spec.rb
|
78
68
|
- spec/queue_spec.rb
|
79
|
-
- spec/rb_tree_map_gc_mark_spec.rb
|
80
69
|
- spec/rb_tree_map_spec.rb
|
81
70
|
- spec/search_spec.rb
|
82
71
|
- spec/sort_spec.rb
|
83
72
|
- spec/splay_tree_map_spec.rb
|
84
73
|
- spec/stack_spec.rb
|
74
|
+
- spec/string_spec.rb
|
85
75
|
- spec/suffix_array_spec.rb
|
86
76
|
- spec/trie_spec.rb
|
87
|
-
|
88
|
-
|
77
|
+
homepage: https://github.com/kanwei/algorithms
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
89
81
|
post_install_message:
|
90
|
-
rdoc_options:
|
91
|
-
- --line-numbers
|
92
|
-
- --inline-source
|
93
|
-
- --title
|
82
|
+
rdoc_options:
|
83
|
+
- "--line-numbers"
|
84
|
+
- "--inline-source"
|
85
|
+
- "--title"
|
94
86
|
- Algorithms
|
95
|
-
- --main
|
87
|
+
- "--main"
|
96
88
|
- README.markdown
|
97
|
-
require_paths:
|
89
|
+
require_paths:
|
98
90
|
- lib
|
99
91
|
- ext
|
100
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
-
requirements:
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
102
94
|
- - ">="
|
103
|
-
- !ruby/object:Gem::Version
|
104
|
-
version:
|
105
|
-
|
106
|
-
|
107
|
-
requirements:
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
108
99
|
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version:
|
111
|
-
version:
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
112
102
|
requirements: []
|
113
|
-
|
114
|
-
rubyforge_project: algorithms
|
115
|
-
rubygems_version: 1.3.1
|
103
|
+
rubygems_version: 3.1.2
|
116
104
|
signing_key:
|
117
|
-
specification_version:
|
118
|
-
summary:
|
105
|
+
specification_version: 4
|
106
|
+
summary: Useful algorithms and data structures for Ruby. Optional C extensions.
|
119
107
|
test_files: []
|
120
|
-
|