algorithms 0.5.0 → 1.1.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/CHANGELOG.markdown +8 -0
- data/Gemfile +9 -0
- data/README.markdown +40 -67
- data/Rakefile +20 -11
- data/algorithms.gemspec +12 -26
- data/ext/algorithms/string/string.c +2 -2
- data/ext/containers/bst/bst.c +1 -1
- data/ext/containers/deque/deque.c +2 -2
- data/ext/containers/rbtree_map/rbtree.c +6 -6
- data/ext/containers/splaytree_map/splaytree.c +4 -4
- data/lib/algorithms/sort.rb +130 -0
- data/lib/algorithms/string.rb +1 -0
- data/lib/algorithms.rb +1 -0
- data/lib/containers/heap.rb +1 -9
- data/lib/containers/priority_queue.rb +0 -1
- data/lib/containers/rb_tree_map.rb +2 -2
- data/lib/containers/suffix_array.rb +3 -3
- data/lib/containers/trie.rb +1 -1
- data/spec/deque_gc_mark_spec.rb +1 -1
- data/spec/deque_spec.rb +18 -18
- data/spec/heap_spec.rb +211 -25
- data/spec/kd_tree_spec.rb +2 -2
- data/spec/map_gc_mark_spec.rb +23 -21
- data/spec/priority_queue_spec.rb +20 -20
- data/spec/queue_spec.rb +10 -10
- data/spec/rb_tree_map_spec.rb +23 -23
- data/spec/search_spec.rb +9 -9
- data/spec/sort_spec.rb +6 -5
- data/spec/splay_tree_map_spec.rb +16 -16
- data/spec/stack_spec.rb +10 -10
- data/spec/string_spec.rb +10 -8
- data/spec/suffix_array_spec.rb +17 -17
- data/spec/trie_spec.rb +16 -16
- metadata +21 -72
data/spec/rb_tree_map_spec.rb
CHANGED
|
@@ -4,30 +4,30 @@ require 'algorithms'
|
|
|
4
4
|
shared_examples "empty rbtree" do
|
|
5
5
|
it "should let you push stuff in" do
|
|
6
6
|
100.times { |x| @tree[x] = x }
|
|
7
|
-
@tree.size.
|
|
7
|
+
expect(@tree.size).to eql(100)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
it "should be empty?" do
|
|
11
|
-
@tree.empty
|
|
11
|
+
expect(@tree.empty?).to be true
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it "should return 0 for height" do
|
|
15
|
-
@tree.height.
|
|
15
|
+
expect(@tree.height).to eql(0)
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
it "should return 0 for size" do
|
|
19
|
-
@tree.size.
|
|
19
|
+
expect(@tree.size).to eql(0)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "should return nil for max and min" do
|
|
23
|
-
@tree.min.
|
|
24
|
-
@tree.max.
|
|
25
|
-
@tree.min_key.
|
|
26
|
-
@tree.max_key.
|
|
23
|
+
expect(@tree.min).to be_nil
|
|
24
|
+
expect(@tree.max).to be_nil
|
|
25
|
+
expect(@tree.min_key).to be_nil
|
|
26
|
+
expect(@tree.max_key).to be_nil
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
it "should not delete" do
|
|
30
|
-
@tree.delete(:non_existing).
|
|
30
|
+
expect(@tree.delete(:non_existing)).to be_nil
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
@@ -39,52 +39,52 @@ shared_examples "non-empty rbtree" do
|
|
|
39
39
|
end
|
|
40
40
|
|
|
41
41
|
it "should return correct size (uniqify items first)" do
|
|
42
|
-
@tree.
|
|
43
|
-
@tree.size.
|
|
42
|
+
expect(@tree).not_to be_empty
|
|
43
|
+
expect(@tree.size).to eql(@random_array.uniq.size)
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
it "should return correct max and min" do
|
|
47
|
-
@tree.min_key.
|
|
48
|
-
@tree.max_key.
|
|
49
|
-
@tree.min[0].
|
|
50
|
-
@tree.max[0].
|
|
47
|
+
expect(@tree.min_key).to eql(@random_array.min)
|
|
48
|
+
expect(@tree.max_key).to eql(@random_array.max)
|
|
49
|
+
expect(@tree.min[0]).to eql(@random_array.min)
|
|
50
|
+
expect(@tree.max[0]).to eql(@random_array.max)
|
|
51
51
|
end
|
|
52
52
|
|
|
53
53
|
it "should not #has_key? keys it doesn't have" do
|
|
54
|
-
@tree.has_key?(100000).
|
|
54
|
+
expect(@tree.has_key?(100000)).to be false
|
|
55
55
|
end
|
|
56
56
|
|
|
57
57
|
it "should #has_key? keys it does have" do
|
|
58
|
-
@tree.has_key?(@random_array[0]).
|
|
58
|
+
expect(@tree.has_key?(@random_array[0])).to be true
|
|
59
59
|
end
|
|
60
60
|
|
|
61
61
|
it "should remove all keys" do
|
|
62
62
|
ordered = []
|
|
63
63
|
@random_array.uniq.each do |key|
|
|
64
|
-
@tree.has_key?(key).
|
|
64
|
+
expect(@tree.has_key?(key)).to eql(true)
|
|
65
65
|
ordered << @tree.delete(key)
|
|
66
|
-
@tree.has_key?(key).
|
|
66
|
+
expect(@tree.has_key?(key)).to eql(false)
|
|
67
67
|
end
|
|
68
|
-
ordered.
|
|
68
|
+
expect(ordered).to eql(@random_array.uniq)
|
|
69
69
|
end
|
|
70
70
|
|
|
71
71
|
it "should delete_min keys correctly" do
|
|
72
72
|
ascending = []
|
|
73
73
|
ascending << @tree.delete_min until @tree.empty?
|
|
74
|
-
ascending.
|
|
74
|
+
expect(ascending).to eql(@random_array.uniq.sort)
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
it "should delete_max keys correctly" do
|
|
78
78
|
descending = []
|
|
79
79
|
descending << @tree.delete_max until @tree.empty?
|
|
80
|
-
descending.
|
|
80
|
+
expect(descending).to eql(@random_array.uniq.sort.reverse)
|
|
81
81
|
end
|
|
82
82
|
|
|
83
83
|
it "should let you iterate with #each" do
|
|
84
84
|
counter = 0
|
|
85
85
|
sorted_array = @random_array.uniq.sort
|
|
86
86
|
@tree.each do |key, val|
|
|
87
|
-
key.
|
|
87
|
+
expect(key).to eql(sorted_array[counter])
|
|
88
88
|
counter += 1
|
|
89
89
|
end
|
|
90
90
|
end
|
data/spec/search_spec.rb
CHANGED
|
@@ -8,21 +8,21 @@ describe "search algorithms" do
|
|
|
8
8
|
n = 1000
|
|
9
9
|
@rand_array = Array.new(n) { rand(n) }.sort
|
|
10
10
|
|
|
11
|
-
Algorithms::Search.binary_search(@rand_array, @rand_array.first).
|
|
12
|
-
Algorithms::Search.binary_search(@rand_array, 999999).
|
|
13
|
-
Algorithms::Search.binary_search(@rand_array, nil).
|
|
11
|
+
expect(Algorithms::Search.binary_search(@rand_array, @rand_array.first)).to eql(@rand_array.first)
|
|
12
|
+
expect(Algorithms::Search.binary_search(@rand_array, 999999)).to be_nil
|
|
13
|
+
expect(Algorithms::Search.binary_search(@rand_array, nil)).to be_nil
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it "should use kmp_search to find substrings it has" do
|
|
17
17
|
string = "ABC ABCDAB ABCDABCDABDE"
|
|
18
|
-
Algorithms::Search.kmp_search(string, "ABCDABD").
|
|
19
|
-
Algorithms::Search.kmp_search(string, "ABCDEF").
|
|
20
|
-
Algorithms::Search.kmp_search(string, nil).
|
|
21
|
-
Algorithms::Search.kmp_search(string, "").
|
|
22
|
-
Algorithms::Search.kmp_search(nil, "ABCD").
|
|
18
|
+
expect(Algorithms::Search.kmp_search(string, "ABCDABD")).to eql(15)
|
|
19
|
+
expect(Algorithms::Search.kmp_search(string, "ABCDEF")).to be_nil
|
|
20
|
+
expect(Algorithms::Search.kmp_search(string, nil)).to be_nil
|
|
21
|
+
expect(Algorithms::Search.kmp_search(string, "")).to be_nil
|
|
22
|
+
expect(Algorithms::Search.kmp_search(nil, "ABCD")).to be_nil
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
it "should let you include Search in String to enable instance methods" do
|
|
26
|
-
"ABC ABCDAB ABCDABCDABDE".kmp_search("ABCDABD").
|
|
26
|
+
expect("ABC ABCDAB ABCDABCDABDE".kmp_search("ABCDABD")).to eql(15)
|
|
27
27
|
end
|
|
28
28
|
end
|
data/spec/sort_spec.rb
CHANGED
|
@@ -4,17 +4,18 @@ include Algorithms
|
|
|
4
4
|
|
|
5
5
|
describe "sort algorithms" do
|
|
6
6
|
before(:each) do
|
|
7
|
-
@sorts = %w(bubble_sort comb_sort selection_sort heapsort insertion_sort
|
|
7
|
+
@sorts = %w(bubble_sort comb_sort selection_sort heapsort insertion_sort
|
|
8
|
+
shell_sort quicksort mergesort dualpivotquicksort)
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
it "should work for empty containers" do
|
|
11
12
|
empty_array = []
|
|
12
|
-
@sorts.each { |sort| Sort.send(sort, empty_array).
|
|
13
|
+
@sorts.each { |sort| expect(Sort.send(sort, empty_array)).to eql([]) }
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
it "should work for a container of size 1" do
|
|
16
17
|
one_array = [1]
|
|
17
|
-
@sorts.each { |sort| Sort.send(sort, one_array).
|
|
18
|
+
@sorts.each { |sort| expect(Sort.send(sort, one_array)).to eql([1]) }
|
|
18
19
|
end
|
|
19
20
|
|
|
20
21
|
it "should work for random arrays of numbers" do
|
|
@@ -22,7 +23,7 @@ describe "sort algorithms" do
|
|
|
22
23
|
rand_array = Array.new(n) { rand(n) }
|
|
23
24
|
sorted_array = rand_array.sort
|
|
24
25
|
|
|
25
|
-
@sorts.each { |sort| Sort.send(sort, rand_array.dup).
|
|
26
|
+
@sorts.each { |sort| expect(Sort.send(sort, rand_array.dup)).to eql(sorted_array) }
|
|
26
27
|
end
|
|
27
28
|
|
|
28
|
-
end
|
|
29
|
+
end
|
data/spec/splay_tree_map_spec.rb
CHANGED
|
@@ -4,24 +4,24 @@ require 'algorithms'
|
|
|
4
4
|
shared_examples "empty splaytree" do
|
|
5
5
|
it "should let you push stuff in" do
|
|
6
6
|
100.times { |x| @tree[x] = x }
|
|
7
|
-
@tree.size.
|
|
7
|
+
expect(@tree.size).to eql(100)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
it "should return 0 for size" do
|
|
11
|
-
@tree.size.
|
|
11
|
+
expect(@tree.size).to eql(0)
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
it "should return nil for #min and #max" do
|
|
15
|
-
@tree.min.
|
|
16
|
-
@tree.max.
|
|
15
|
+
expect(@tree.min).to be_nil
|
|
16
|
+
expect(@tree.max).to be_nil
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
it "should return nil for #delete" do
|
|
20
|
-
@tree.delete(:non_existing).
|
|
20
|
+
expect(@tree.delete(:non_existing)).to be_nil
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
it "should return nil for #get" do
|
|
24
|
-
@tree[4235].
|
|
24
|
+
expect(@tree[4235]).to be_nil
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
@@ -34,40 +34,40 @@ shared_examples "non-empty splaytree" do
|
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
it "should return correct size (uniqify items first)" do
|
|
37
|
-
@tree.size.
|
|
37
|
+
expect(@tree.size).to eql(@random_array.uniq.size)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
it "should have correct height (worst case is when items are inserted in order, and height = num items inserted)" do
|
|
41
41
|
@tree.clear
|
|
42
42
|
10.times { |x| @tree[x] = x }
|
|
43
|
-
@tree.height.
|
|
43
|
+
expect(@tree.height).to eql(10)
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
it "should return correct max and min keys" do
|
|
47
|
-
@tree.min[0].
|
|
48
|
-
@tree.max[0].
|
|
47
|
+
expect(@tree.min[0]).to eql(@random_array.min)
|
|
48
|
+
expect(@tree.max[0]).to eql(@random_array.max)
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
it "should not #has_key? keys it doesn't have" do
|
|
52
|
-
@tree.has_key?(10000).
|
|
52
|
+
expect(@tree.has_key?(10000)).to be false
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
it "should #has_key? keys it does have" do
|
|
56
|
-
@tree.has_key?(@random_array[0]).
|
|
56
|
+
expect(@tree.has_key?(@random_array[0])).to be true
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
it "should remove any key" do
|
|
60
60
|
random_key = @random_array[rand(@num_items)]
|
|
61
|
-
@tree.has_key?(random_key).
|
|
62
|
-
@tree.delete(random_key).
|
|
63
|
-
@tree.has_key?(random_key).
|
|
61
|
+
expect(@tree.has_key?(random_key)).to be true
|
|
62
|
+
expect(@tree.delete(random_key)).to eql(random_key)
|
|
63
|
+
expect(@tree.has_key?(random_key)).to be false
|
|
64
64
|
end
|
|
65
65
|
|
|
66
66
|
it "should let you iterate with #each" do
|
|
67
67
|
counter = 0
|
|
68
68
|
sorted_array = @random_array.uniq.sort
|
|
69
69
|
@tree.each do |key, val|
|
|
70
|
-
key.
|
|
70
|
+
expect(key).to eql(sorted_array[counter])
|
|
71
71
|
counter += 1
|
|
72
72
|
end
|
|
73
73
|
end
|
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
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
$: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
|
|
2
2
|
require 'algorithms'
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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
|
|
12
14
|
end
|
|
13
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,39 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: algorithms
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
5
|
-
prerelease:
|
|
4
|
+
version: 1.1.0
|
|
6
5
|
platform: ruby
|
|
7
6
|
authors:
|
|
8
7
|
- Kanwei Li
|
|
9
|
-
autorequire:
|
|
10
8
|
bindir: bin
|
|
11
9
|
cert_chain: []
|
|
12
|
-
date:
|
|
13
|
-
dependencies:
|
|
14
|
-
-
|
|
15
|
-
|
|
16
|
-
requirement: &70163289605640 !ruby/object:Gem::Requirement
|
|
17
|
-
none: false
|
|
18
|
-
requirements:
|
|
19
|
-
- - ! '>='
|
|
20
|
-
- !ruby/object:Gem::Version
|
|
21
|
-
version: '0'
|
|
22
|
-
type: :development
|
|
23
|
-
prerelease: false
|
|
24
|
-
version_requirements: *70163289605640
|
|
25
|
-
- !ruby/object:Gem::Dependency
|
|
26
|
-
name: echoe
|
|
27
|
-
requirement: &70163289605060 !ruby/object:Gem::Requirement
|
|
28
|
-
none: false
|
|
29
|
-
requirements:
|
|
30
|
-
- - ! '>='
|
|
31
|
-
- !ruby/object:Gem::Version
|
|
32
|
-
version: '0'
|
|
33
|
-
type: :development
|
|
34
|
-
prerelease: false
|
|
35
|
-
version_requirements: *70163289605060
|
|
36
|
-
description: A library of algorithms and data structures (containers).
|
|
10
|
+
date: 2023-11-11 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Heap, Priority Queue, Deque, Stack, Queue, Red-Black Trees, Splay Trees,
|
|
13
|
+
sorting algorithms, and more
|
|
37
14
|
email: kanwei@gmail.com
|
|
38
15
|
executables: []
|
|
39
16
|
extensions:
|
|
@@ -42,35 +19,10 @@ extensions:
|
|
|
42
19
|
- ext/containers/deque/extconf.rb
|
|
43
20
|
- ext/containers/rbtree_map/extconf.rb
|
|
44
21
|
- ext/containers/splaytree_map/extconf.rb
|
|
45
|
-
extra_rdoc_files:
|
|
46
|
-
- CHANGELOG.markdown
|
|
47
|
-
- README.markdown
|
|
48
|
-
- ext/algorithms/string/extconf.rb
|
|
49
|
-
- ext/algorithms/string/string.c
|
|
50
|
-
- ext/containers/bst/bst.c
|
|
51
|
-
- ext/containers/bst/extconf.rb
|
|
52
|
-
- ext/containers/deque/deque.c
|
|
53
|
-
- ext/containers/deque/extconf.rb
|
|
54
|
-
- ext/containers/rbtree_map/extconf.rb
|
|
55
|
-
- ext/containers/rbtree_map/rbtree.c
|
|
56
|
-
- ext/containers/splaytree_map/extconf.rb
|
|
57
|
-
- ext/containers/splaytree_map/splaytree.c
|
|
58
|
-
- lib/algorithms.rb
|
|
59
|
-
- lib/algorithms/search.rb
|
|
60
|
-
- lib/algorithms/sort.rb
|
|
61
|
-
- lib/algorithms/string.rb
|
|
62
|
-
- lib/containers/deque.rb
|
|
63
|
-
- lib/containers/heap.rb
|
|
64
|
-
- lib/containers/kd_tree.rb
|
|
65
|
-
- lib/containers/priority_queue.rb
|
|
66
|
-
- lib/containers/queue.rb
|
|
67
|
-
- lib/containers/rb_tree_map.rb
|
|
68
|
-
- lib/containers/splay_tree_map.rb
|
|
69
|
-
- lib/containers/stack.rb
|
|
70
|
-
- lib/containers/suffix_array.rb
|
|
71
|
-
- lib/containers/trie.rb
|
|
22
|
+
extra_rdoc_files: []
|
|
72
23
|
files:
|
|
73
24
|
- CHANGELOG.markdown
|
|
25
|
+
- Gemfile
|
|
74
26
|
- Manifest
|
|
75
27
|
- README.markdown
|
|
76
28
|
- Rakefile
|
|
@@ -121,35 +73,32 @@ files:
|
|
|
121
73
|
- spec/string_spec.rb
|
|
122
74
|
- spec/suffix_array_spec.rb
|
|
123
75
|
- spec/trie_spec.rb
|
|
124
|
-
homepage: https://
|
|
125
|
-
licenses:
|
|
126
|
-
|
|
76
|
+
homepage: https://github.com/kanwei/algorithms
|
|
77
|
+
licenses:
|
|
78
|
+
- MIT
|
|
79
|
+
metadata: {}
|
|
127
80
|
rdoc_options:
|
|
128
|
-
- --line-numbers
|
|
129
|
-
- --inline-source
|
|
130
|
-
- --title
|
|
81
|
+
- "--line-numbers"
|
|
82
|
+
- "--inline-source"
|
|
83
|
+
- "--title"
|
|
131
84
|
- Algorithms
|
|
132
|
-
- --main
|
|
85
|
+
- "--main"
|
|
133
86
|
- README.markdown
|
|
134
87
|
require_paths:
|
|
135
88
|
- lib
|
|
136
89
|
- ext
|
|
137
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
|
-
none: false
|
|
139
91
|
requirements:
|
|
140
|
-
- -
|
|
92
|
+
- - ">="
|
|
141
93
|
- !ruby/object:Gem::Version
|
|
142
94
|
version: '0'
|
|
143
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
144
|
-
none: false
|
|
145
96
|
requirements:
|
|
146
|
-
- -
|
|
97
|
+
- - ">="
|
|
147
98
|
- !ruby/object:Gem::Version
|
|
148
|
-
version: '
|
|
99
|
+
version: '0'
|
|
149
100
|
requirements: []
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
specification_version: 3
|
|
154
|
-
summary: A library of algorithms and data structures (containers).
|
|
101
|
+
rubygems_version: 3.6.9
|
|
102
|
+
specification_version: 4
|
|
103
|
+
summary: Useful algorithms and data structures for Ruby. Optional C extensions.
|
|
155
104
|
test_files: []
|