algorithms-aunderwo 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +9 -0
  2. data/History.txt +176 -0
  3. data/Manifest +47 -0
  4. data/README.markdown +93 -0
  5. data/Rakefile +31 -0
  6. data/VERSION +1 -0
  7. data/algorithms-aunderwo.gemspec +110 -0
  8. data/algorithms.gemspec +32 -0
  9. data/benchmarks/deque.rb +17 -0
  10. data/benchmarks/sorts.rb +34 -0
  11. data/benchmarks/treemaps.rb +51 -0
  12. data/ext/algorithms/string/extconf.rb +4 -0
  13. data/ext/algorithms/string/string.c +68 -0
  14. data/ext/containers/bst/bst.c +247 -0
  15. data/ext/containers/bst/extconf.rb +4 -0
  16. data/ext/containers/deque/deque.c +247 -0
  17. data/ext/containers/deque/extconf.rb +4 -0
  18. data/ext/containers/rbtree_map/extconf.rb +4 -0
  19. data/ext/containers/rbtree_map/rbtree.c +498 -0
  20. data/ext/containers/splaytree_map/extconf.rb +4 -0
  21. data/ext/containers/splaytree_map/splaytree.c +419 -0
  22. data/lib/algorithms.rb +65 -0
  23. data/lib/algorithms/search.rb +84 -0
  24. data/lib/algorithms/sort.rb +238 -0
  25. data/lib/algorithms/string.rb +8 -0
  26. data/lib/containers/deque.rb +171 -0
  27. data/lib/containers/heap.rb +486 -0
  28. data/lib/containers/kd_tree.rb +110 -0
  29. data/lib/containers/priority_queue.rb +113 -0
  30. data/lib/containers/queue.rb +68 -0
  31. data/lib/containers/rb_tree_map.rb +398 -0
  32. data/lib/containers/splay_tree_map.rb +269 -0
  33. data/lib/containers/stack.rb +67 -0
  34. data/lib/containers/suffix_array.rb +68 -0
  35. data/lib/containers/trie.rb +182 -0
  36. data/spec/bst_gc_mark_spec.rb +25 -0
  37. data/spec/bst_spec.rb +25 -0
  38. data/spec/deque_gc_mark_spec.rb +18 -0
  39. data/spec/deque_spec.rb +108 -0
  40. data/spec/heap_spec.rb +126 -0
  41. data/spec/kd_expected_out.txt +10000 -0
  42. data/spec/kd_test_in.txt +10000 -0
  43. data/spec/kd_tree_spec.rb +34 -0
  44. data/spec/map_gc_mark_spec.rb +27 -0
  45. data/spec/priority_queue_spec.rb +75 -0
  46. data/spec/queue_spec.rb +61 -0
  47. data/spec/rb_tree_map_spec.rb +123 -0
  48. data/spec/search_spec.rb +28 -0
  49. data/spec/sort_spec.rb +28 -0
  50. data/spec/splay_tree_map_spec.rb +106 -0
  51. data/spec/stack_spec.rb +60 -0
  52. data/spec/string_spec.rb +13 -0
  53. data/spec/suffix_array_spec.rb +40 -0
  54. data/spec/trie_spec.rb +59 -0
  55. metadata +138 -0
@@ -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,13 @@
1
+ $: << File.join(File.expand_path(File.dirname(__FILE__)), '..', 'lib')
2
+ require 'algorithms'
3
+
4
+ describe "string algorithms" do
5
+ it "should do levenshtein distance" do
6
+ Algorithms::String.levenshtein_dist("Hello", "Hel").should eql(2)
7
+ Algorithms::String.levenshtein_dist("Hello", "").should eql(5)
8
+ Algorithms::String.levenshtein_dist("", "Hello").should eql(5)
9
+ Algorithms::String.levenshtein_dist("Hello", "Hello").should eql(0)
10
+ Algorithms::String.levenshtein_dist("Hello", "ello").should eql(1)
11
+ Algorithms::String.levenshtein_dist("Hello", "Mello").should eql(1)
12
+ end
13
+ 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,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: algorithms-aunderwo
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
10
+ platform: ruby
11
+ authors:
12
+ - Kanwei Li
13
+ - Anthony Underwood
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-07-29 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A modfication of the algorithms gem to include the latest String function
23
+ email: algorithms@ants.otherinbox.co, kanwei@gmail.com
24
+ executables: []
25
+
26
+ extensions:
27
+ - ext/algorithms/string/extconf.rb
28
+ - ext/containers/splaytree_map/extconf.rb
29
+ - ext/containers/bst/extconf.rb
30
+ - ext/containers/rbtree_map/extconf.rb
31
+ - ext/containers/deque/extconf.rb
32
+ extra_rdoc_files:
33
+ - README.markdown
34
+ files:
35
+ - .gitignore
36
+ - History.txt
37
+ - Manifest
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - algorithms-aunderwo.gemspec
42
+ - algorithms.gemspec
43
+ - benchmarks/deque.rb
44
+ - benchmarks/sorts.rb
45
+ - benchmarks/treemaps.rb
46
+ - ext/algorithms/string/extconf.rb
47
+ - ext/algorithms/string/string.c
48
+ - ext/containers/bst/bst.c
49
+ - ext/containers/bst/extconf.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
+ - lib/algorithms.rb
57
+ - lib/algorithms/search.rb
58
+ - lib/algorithms/sort.rb
59
+ - lib/algorithms/string.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
+ - spec/bst_gc_mark_spec.rb
71
+ - spec/bst_spec.rb
72
+ - spec/deque_gc_mark_spec.rb
73
+ - spec/deque_spec.rb
74
+ - spec/heap_spec.rb
75
+ - spec/kd_expected_out.txt
76
+ - spec/kd_test_in.txt
77
+ - spec/kd_tree_spec.rb
78
+ - spec/map_gc_mark_spec.rb
79
+ - spec/priority_queue_spec.rb
80
+ - spec/queue_spec.rb
81
+ - spec/rb_tree_map_spec.rb
82
+ - spec/search_spec.rb
83
+ - spec/sort_spec.rb
84
+ - spec/splay_tree_map_spec.rb
85
+ - spec/stack_spec.rb
86
+ - spec/string_spec.rb
87
+ - spec/suffix_array_spec.rb
88
+ - spec/trie_spec.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/aunderwo/algorithms
91
+ licenses: []
92
+
93
+ post_install_message:
94
+ rdoc_options:
95
+ - --charset=UTF-8
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.3.7
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: A library of algorithms and containers.
121
+ test_files:
122
+ - spec/rb_tree_map_spec.rb
123
+ - spec/kd_tree_spec.rb
124
+ - spec/suffix_array_spec.rb
125
+ - spec/trie_spec.rb
126
+ - spec/map_gc_mark_spec.rb
127
+ - spec/heap_spec.rb
128
+ - spec/stack_spec.rb
129
+ - spec/priority_queue_spec.rb
130
+ - spec/deque_gc_mark_spec.rb
131
+ - spec/bst_spec.rb
132
+ - spec/string_spec.rb
133
+ - spec/queue_spec.rb
134
+ - spec/deque_spec.rb
135
+ - spec/sort_spec.rb
136
+ - spec/bst_gc_mark_spec.rb
137
+ - spec/splay_tree_map_spec.rb
138
+ - spec/search_spec.rb