stakach-algorithms 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README.markdown +99 -97
  2. data/Rakefile +15 -27
  3. data/ext/algorithms/string/extconf.rb +4 -4
  4. data/ext/algorithms/string/string.c +68 -70
  5. data/ext/containers/bst/bst.c +249 -249
  6. data/ext/containers/bst/extconf.rb +4 -4
  7. data/ext/containers/deque/deque.c +248 -248
  8. data/ext/containers/deque/extconf.rb +4 -4
  9. data/ext/containers/rbtree_map/extconf.rb +4 -4
  10. data/ext/containers/rbtree_map/rbtree.c +500 -500
  11. data/ext/containers/splaytree_map/extconf.rb +4 -4
  12. data/ext/containers/splaytree_map/splaytree.c +421 -421
  13. data/lib/algorithms.rb +69 -69
  14. data/lib/algorithms/search.rb +85 -85
  15. data/lib/algorithms/sort.rb +242 -242
  16. data/lib/algorithms/string.rb +10 -10
  17. data/lib/algorithms/version.rb +3 -3
  18. data/lib/containers/deque.rb +176 -176
  19. data/lib/containers/heap.rb +501 -506
  20. data/lib/containers/kd_tree.rb +112 -112
  21. data/lib/containers/priority_queue.rb +116 -116
  22. data/lib/containers/queue.rb +71 -71
  23. data/lib/containers/rb_tree_map.rb +402 -402
  24. data/lib/containers/splay_tree_map.rb +273 -273
  25. data/lib/containers/stack.rb +70 -70
  26. data/lib/containers/suffix_array.rb +71 -71
  27. data/lib/containers/trie.rb +187 -187
  28. data/spec/bst_gc_mark_spec.rb +25 -0
  29. data/spec/bst_spec.rb +25 -0
  30. data/spec/deque_gc_mark_spec.rb +17 -0
  31. data/spec/deque_spec.rb +107 -0
  32. data/spec/heap_spec.rb +130 -0
  33. data/spec/helper.rb +4 -0
  34. data/spec/kd_expected_out.txt +10000 -0
  35. data/spec/kd_test_in.txt +10000 -0
  36. data/spec/kd_tree_spec.rb +33 -0
  37. data/spec/map_gc_mark_spec.rb +28 -0
  38. data/spec/priority_queue_spec.rb +74 -0
  39. data/spec/queue_spec.rb +60 -0
  40. data/spec/rb_tree_map_spec.rb +122 -0
  41. data/spec/search_spec.rb +27 -0
  42. data/spec/sort_spec.rb +26 -0
  43. data/spec/splay_tree_map_spec.rb +105 -0
  44. data/spec/stack_spec.rb +59 -0
  45. data/spec/string_spec.rb +12 -0
  46. data/spec/suffix_array_spec.rb +39 -0
  47. data/spec/trie_spec.rb +58 -0
  48. metadata +60 -4
@@ -0,0 +1,59 @@
1
+ require 'helper'
2
+
3
+ describe "empty stack" do
4
+ before(:each) do
5
+ @stack = Algorithms::Containers::Stack.new
6
+ end
7
+
8
+ it "should return nil when sent #pop" do
9
+ @stack.pop.should be_nil
10
+ end
11
+
12
+ it "should return a size of 1 when sent #push" do
13
+ @stack.push(1)
14
+ @stack.size.should eql(1)
15
+ end
16
+
17
+ it "should return nil when sent #next" do
18
+ @stack.next.should be_nil
19
+ end
20
+
21
+ it "should return empty?" do
22
+ @stack.empty?.should be_true
23
+ end
24
+ end
25
+
26
+ describe "non-empty stack" do
27
+ before(:each) do
28
+ @stack = Algorithms::Containers::Stack.new
29
+ @stack.push(10)
30
+ @stack.push("10")
31
+ end
32
+
33
+ it "should return last pushed object" do
34
+ @stack.pop.should eql("10")
35
+ end
36
+
37
+ it "should return the size" do
38
+ @stack.size.should eql(2)
39
+ end
40
+
41
+ it "should not return empty?" do
42
+ @stack.empty?.should be_false
43
+ end
44
+
45
+
46
+ it "should iterate in LIFO order" do
47
+ arr = []
48
+ @stack.each { |obj| arr << obj }
49
+ arr.should eql(["10", 10])
50
+ end
51
+
52
+ it "should return nil after all pops" do
53
+ @stack.pop
54
+ @stack.pop
55
+ @stack.pop.should be_nil
56
+ @stack.next.should be_nil
57
+ end
58
+
59
+ end
@@ -0,0 +1,12 @@
1
+ require 'helper'
2
+
3
+ describe "string algorithms" do
4
+ it "should do levenshtein distance" do
5
+ Algorithms::Algorithms::String.levenshtein_dist("Hello", "Hel").should eql(2)
6
+ Algorithms::Algorithms::String.levenshtein_dist("Hello", "").should eql(5)
7
+ Algorithms::Algorithms::String.levenshtein_dist("", "Hello").should eql(5)
8
+ Algorithms::Algorithms::String.levenshtein_dist("Hello", "Hello").should eql(0)
9
+ Algorithms::Algorithms::String.levenshtein_dist("Hello", "ello").should eql(1)
10
+ Algorithms::Algorithms::String.levenshtein_dist("Hello", "Mello").should eql(1)
11
+ end
12
+ end
@@ -0,0 +1,39 @@
1
+ require 'helper'
2
+
3
+ describe "empty suffix array" do
4
+ it "should not initialize with empty string" do
5
+ lambda { Algorithms::Containers::SuffixArray.new("") }.should raise_error
6
+ end
7
+ end
8
+
9
+ describe "non-empty suffix array" do
10
+ before(:each) do
11
+ @s_array = Algorithms::Containers::SuffixArray.new("abracadabra")
12
+ end
13
+
14
+ it "should has_substring? each possible substring" do
15
+ @s_array.has_substring?("a").should be_true
16
+ @s_array.has_substring?("abra").should be_true
17
+ @s_array.has_substring?("abracadabra").should be_true
18
+ @s_array.has_substring?("acadabra").should be_true
19
+ @s_array.has_substring?("adabra").should be_true
20
+ @s_array.has_substring?("bra").should be_true
21
+ @s_array.has_substring?("bracadabra").should be_true
22
+ @s_array.has_substring?("cadabra").should be_true
23
+ @s_array.has_substring?("dabra").should be_true
24
+ @s_array.has_substring?("ra").should be_true
25
+ @s_array.has_substring?("racadabra").should be_true
26
+ end
27
+
28
+ it "should not has_substring? substrings it does not have" do
29
+ @s_array.has_substring?("nope").should be_false
30
+ @s_array.has_substring?(nil).should be_false
31
+ end
32
+
33
+ it "should work with numbers (calls to_s)" do
34
+ number = Algorithms::Containers::SuffixArray.new(123456789)
35
+ number[1].should be_true
36
+ number.has_substring?(12).should be_true
37
+ number.has_substring?(13).should be_false
38
+ end
39
+ end
data/spec/trie_spec.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'helper'
2
+
3
+ describe "empty trie" do
4
+ before(:each) do
5
+ @trie = Algorithms::Containers::Trie.new
6
+ end
7
+
8
+ it "should not get or has_key?" do
9
+ @trie.get("anything").should be_nil
10
+ @trie.has_key?("anything").should be_false
11
+ end
12
+
13
+ it "should not have longest_prefix or match wildcards" do
14
+ @trie.wildcard("an*thing").should eql([])
15
+ @trie.longest_prefix("an*thing").should eql("")
16
+ end
17
+ end
18
+
19
+ describe "non-empty trie" do
20
+ before(:each) do
21
+ @trie = Algorithms::Containers::Trie.new
22
+ @trie.push("Hello", "World")
23
+ @trie.push("Hilly", "World")
24
+ @trie.push("Hello, brother", "World")
25
+ @trie.push("Hello, bob", "World")
26
+ end
27
+
28
+ it "should has_key? keys it has" do
29
+ @trie.has_key?("Hello").should be_true
30
+ @trie.has_key?("Hello, brother").should be_true
31
+ @trie.has_key?("Hello, bob").should be_true
32
+ end
33
+
34
+ it "should not has_key? keys it doesn't have" do
35
+ @trie.has_key?("Nope").should be_false
36
+ end
37
+
38
+ it "should get values" do
39
+ @trie.get("Hello").should eql("World")
40
+ end
41
+
42
+ it "should overwrite values" do
43
+ @trie.push("Hello", "John")
44
+ @trie.get("Hello").should eql("John")
45
+ end
46
+
47
+ it "should return longest prefix" do
48
+ @trie.longest_prefix("Hello, brandon").should eql("Hello")
49
+ @trie.longest_prefix("Hel").should eql("")
50
+ @trie.longest_prefix("Hello").should eql("Hello")
51
+ @trie.longest_prefix("Hello, bob").should eql("Hello, bob")
52
+ end
53
+
54
+ it "should match wildcards" do
55
+ @trie.wildcard("H*ll.").should eql(["Hello", "Hilly"])
56
+ @trie.wildcard("Hel").should eql([])
57
+ end
58
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stakach-algorithms
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,8 +10,24 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-27 00:00:00.000000000 Z
14
- dependencies: []
13
+ date: 2012-11-12 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
15
31
  description: A library of algorithms and containers.
16
32
  email:
17
33
  - kanwei@gmail.com
@@ -76,6 +92,26 @@ files:
76
92
  - ext/containers/rbtree_map/rbtree.c
77
93
  - ext/containers/splaytree_map/extconf.rb
78
94
  - ext/containers/splaytree_map/splaytree.c
95
+ - spec/bst_gc_mark_spec.rb
96
+ - spec/bst_spec.rb
97
+ - spec/deque_gc_mark_spec.rb
98
+ - spec/deque_spec.rb
99
+ - spec/heap_spec.rb
100
+ - spec/helper.rb
101
+ - spec/kd_expected_out.txt
102
+ - spec/kd_test_in.txt
103
+ - spec/kd_tree_spec.rb
104
+ - spec/map_gc_mark_spec.rb
105
+ - spec/priority_queue_spec.rb
106
+ - spec/queue_spec.rb
107
+ - spec/rb_tree_map_spec.rb
108
+ - spec/search_spec.rb
109
+ - spec/sort_spec.rb
110
+ - spec/splay_tree_map_spec.rb
111
+ - spec/stack_spec.rb
112
+ - spec/string_spec.rb
113
+ - spec/suffix_array_spec.rb
114
+ - spec/trie_spec.rb
79
115
  homepage: https://github.com/stakach/algorithms
80
116
  licenses: []
81
117
  post_install_message:
@@ -107,4 +143,24 @@ rubygems_version: 1.8.24
107
143
  signing_key:
108
144
  specification_version: 3
109
145
  summary: A library of algorithms and containers.
110
- test_files: []
146
+ test_files:
147
+ - spec/bst_gc_mark_spec.rb
148
+ - spec/bst_spec.rb
149
+ - spec/deque_gc_mark_spec.rb
150
+ - spec/deque_spec.rb
151
+ - spec/heap_spec.rb
152
+ - spec/helper.rb
153
+ - spec/kd_expected_out.txt
154
+ - spec/kd_test_in.txt
155
+ - spec/kd_tree_spec.rb
156
+ - spec/map_gc_mark_spec.rb
157
+ - spec/priority_queue_spec.rb
158
+ - spec/queue_spec.rb
159
+ - spec/rb_tree_map_spec.rb
160
+ - spec/search_spec.rb
161
+ - spec/sort_spec.rb
162
+ - spec/splay_tree_map_spec.rb
163
+ - spec/stack_spec.rb
164
+ - spec/string_spec.rb
165
+ - spec/suffix_array_spec.rb
166
+ - spec/trie_spec.rb