algorithms64 0.6.2

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.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.markdown +193 -0
  3. data/Gemfile +9 -0
  4. data/Manifest +51 -0
  5. data/README.markdown +66 -0
  6. data/Rakefile +22 -0
  7. data/algorithms.gemspec +23 -0
  8. data/benchmarks/deque.rb +17 -0
  9. data/benchmarks/sorts.rb +34 -0
  10. data/benchmarks/treemaps.rb +51 -0
  11. data/ext/algorithms/string/extconf.rb +4 -0
  12. data/ext/algorithms/string/string.c +68 -0
  13. data/ext/containers/bst/bst.c +247 -0
  14. data/ext/containers/bst/extconf.rb +4 -0
  15. data/ext/containers/deque/deque.c +247 -0
  16. data/ext/containers/deque/extconf.rb +4 -0
  17. data/ext/containers/rbtree_map/extconf.rb +4 -0
  18. data/ext/containers/rbtree_map/rbtree.c +498 -0
  19. data/ext/containers/splaytree_map/extconf.rb +4 -0
  20. data/ext/containers/splaytree_map/splaytree.c +419 -0
  21. data/lib/algorithms.rb +66 -0
  22. data/lib/algorithms/search.rb +84 -0
  23. data/lib/algorithms/sort.rb +368 -0
  24. data/lib/algorithms/string.rb +9 -0
  25. data/lib/containers/deque.rb +171 -0
  26. data/lib/containers/heap.rb +502 -0
  27. data/lib/containers/kd_tree.rb +110 -0
  28. data/lib/containers/priority_queue.rb +113 -0
  29. data/lib/containers/queue.rb +68 -0
  30. data/lib/containers/rb_tree_map.rb +398 -0
  31. data/lib/containers/splay_tree_map.rb +269 -0
  32. data/lib/containers/stack.rb +67 -0
  33. data/lib/containers/suffix_array.rb +68 -0
  34. data/lib/containers/trie.rb +182 -0
  35. data/spec/bst_gc_mark_spec.rb +25 -0
  36. data/spec/bst_spec.rb +25 -0
  37. data/spec/deque_gc_mark_spec.rb +18 -0
  38. data/spec/deque_spec.rb +108 -0
  39. data/spec/heap_spec.rb +131 -0
  40. data/spec/kd_expected_out.txt +10000 -0
  41. data/spec/kd_test_in.txt +10000 -0
  42. data/spec/kd_tree_spec.rb +34 -0
  43. data/spec/map_gc_mark_spec.rb +29 -0
  44. data/spec/priority_queue_spec.rb +75 -0
  45. data/spec/queue_spec.rb +61 -0
  46. data/spec/rb_tree_map_spec.rb +123 -0
  47. data/spec/search_spec.rb +28 -0
  48. data/spec/sort_spec.rb +29 -0
  49. data/spec/splay_tree_map_spec.rb +106 -0
  50. data/spec/stack_spec.rb +60 -0
  51. data/spec/string_spec.rb +15 -0
  52. data/spec/suffix_array_spec.rb +40 -0
  53. data/spec/trie_spec.rb +59 -0
  54. metadata +110 -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
+ expect(@stack.pop).to be_nil
11
+ end
12
+
13
+ it "should return a size of 1 when sent #push" do
14
+ @stack.push(1)
15
+ expect(@stack.size).to eql(1)
16
+ end
17
+
18
+ it "should return nil when sent #next" do
19
+ expect(@stack.next).to be_nil
20
+ end
21
+
22
+ it "should return empty?" do
23
+ expect(@stack.empty?).to 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
+ expect(@stack.pop).to eql("10")
36
+ end
37
+
38
+ it "should return the size" do
39
+ expect(@stack.size).to eql(2)
40
+ end
41
+
42
+ it "should not return empty?" do
43
+ expect(@stack.empty?).to be false
44
+ end
45
+
46
+
47
+ it "should iterate in LIFO order" do
48
+ arr = []
49
+ @stack.each { |obj| arr << obj }
50
+ expect(arr).to eql(["10", 10])
51
+ end
52
+
53
+ it "should return nil after all pops" do
54
+ @stack.pop
55
+ @stack.pop
56
+ expect(@stack.pop).to be_nil
57
+ expect(@stack.next).to be_nil
58
+ end
59
+
60
+ end
@@ -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
@@ -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
+ expect { Containers::SuffixArray.new("") }.to 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
+ 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
+ end
28
+
29
+ it "should not has_substring? substrings it does not have" do
30
+ expect(@s_array.has_substring?("nope")).to be false
31
+ expect(@s_array.has_substring?(nil)).to be false
32
+ end
33
+
34
+ it "should work with numbers (calls to_s)" do
35
+ number = Containers::SuffixArray.new(123456789)
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
+ end
40
+ end
@@ -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
+ expect(@trie.get("anything")).to be_nil
11
+ expect(@trie.has_key?("anything")).to be false
12
+ end
13
+
14
+ it "should not have longest_prefix or match wildcards" do
15
+ expect(@trie.wildcard("an*thing")).to eql([])
16
+ expect(@trie.longest_prefix("an*thing")).to 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
+ 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
+ end
34
+
35
+ it "should not has_key? keys it doesn't have" do
36
+ expect(@trie.has_key?("Nope")).to be false
37
+ end
38
+
39
+ it "should get values" do
40
+ expect(@trie.get("Hello")).to eql("World")
41
+ end
42
+
43
+ it "should overwrite values" do
44
+ @trie.push("Hello", "John")
45
+ expect(@trie.get("Hello")).to eql("John")
46
+ end
47
+
48
+ it "should return longest prefix" do
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
+ end
54
+
55
+ it "should match wildcards" do
56
+ expect(@trie.wildcard("H*ll.")).to eql(["Hello", "Hilly"])
57
+ expect(@trie.wildcard("Hel")).to eql([])
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: algorithms64
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.2
5
+ platform: ruby
6
+ authors:
7
+ - Kanwei Li
8
+ - Muhammad Mufid Afif
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2018-12-06 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Heap, Priority Queue, Deque, Stack, Queue, Red-Black Trees, Splay Trees,
15
+ sorting algorithms, and more
16
+ email: kanwei@gmail.com
17
+ executables: []
18
+ extensions:
19
+ - ext/algorithms/string/extconf.rb
20
+ - ext/containers/bst/extconf.rb
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
+ files:
26
+ - CHANGELOG.markdown
27
+ - Gemfile
28
+ - Manifest
29
+ - README.markdown
30
+ - Rakefile
31
+ - algorithms.gemspec
32
+ - benchmarks/deque.rb
33
+ - benchmarks/sorts.rb
34
+ - benchmarks/treemaps.rb
35
+ - ext/algorithms/string/extconf.rb
36
+ - ext/algorithms/string/string.c
37
+ - ext/containers/bst/bst.c
38
+ - ext/containers/bst/extconf.rb
39
+ - ext/containers/deque/deque.c
40
+ - ext/containers/deque/extconf.rb
41
+ - ext/containers/rbtree_map/extconf.rb
42
+ - ext/containers/rbtree_map/rbtree.c
43
+ - ext/containers/splaytree_map/extconf.rb
44
+ - ext/containers/splaytree_map/splaytree.c
45
+ - lib/algorithms.rb
46
+ - lib/algorithms/search.rb
47
+ - lib/algorithms/sort.rb
48
+ - lib/algorithms/string.rb
49
+ - lib/containers/deque.rb
50
+ - lib/containers/heap.rb
51
+ - lib/containers/kd_tree.rb
52
+ - lib/containers/priority_queue.rb
53
+ - lib/containers/queue.rb
54
+ - lib/containers/rb_tree_map.rb
55
+ - lib/containers/splay_tree_map.rb
56
+ - lib/containers/stack.rb
57
+ - lib/containers/suffix_array.rb
58
+ - lib/containers/trie.rb
59
+ - spec/bst_gc_mark_spec.rb
60
+ - spec/bst_spec.rb
61
+ - spec/deque_gc_mark_spec.rb
62
+ - spec/deque_spec.rb
63
+ - spec/heap_spec.rb
64
+ - spec/kd_expected_out.txt
65
+ - spec/kd_test_in.txt
66
+ - spec/kd_tree_spec.rb
67
+ - spec/map_gc_mark_spec.rb
68
+ - spec/priority_queue_spec.rb
69
+ - spec/queue_spec.rb
70
+ - spec/rb_tree_map_spec.rb
71
+ - spec/search_spec.rb
72
+ - spec/sort_spec.rb
73
+ - spec/splay_tree_map_spec.rb
74
+ - spec/stack_spec.rb
75
+ - spec/string_spec.rb
76
+ - spec/suffix_array_spec.rb
77
+ - spec/trie_spec.rb
78
+ homepage: https://github.com/kanwei/algorithms
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options:
84
+ - "--line-numbers"
85
+ - "--inline-source"
86
+ - "--title"
87
+ - Algorithms
88
+ - "--main"
89
+ - README.markdown
90
+ require_paths:
91
+ - lib
92
+ - ext
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project: algorithms
105
+ rubygems_version: 2.7.7
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Useful algorithms and data structures for Ruby. Optional C extensions. Optimized
109
+ 64-bit integer.
110
+ test_files: []