algorithms 0.6.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/CHANGELOG.markdown +193 -0
  2. data/Gemfile +9 -0
  3. data/Manifest +51 -0
  4. data/README.markdown +84 -0
  5. data/Rakefile +18 -0
  6. data/algorithms.gemspec +23 -0
  7. data/benchmarks/deque.rb +17 -0
  8. data/benchmarks/sorts.rb +34 -0
  9. data/benchmarks/treemaps.rb +51 -0
  10. data/ext/algorithms/string/extconf.rb +4 -0
  11. data/ext/algorithms/string/string.c +68 -0
  12. data/ext/containers/bst/bst.c +247 -0
  13. data/ext/containers/bst/extconf.rb +4 -0
  14. data/ext/containers/deque/deque.c +247 -0
  15. data/ext/containers/deque/extconf.rb +4 -0
  16. data/ext/containers/rbtree_map/extconf.rb +4 -0
  17. data/ext/containers/rbtree_map/rbtree.c +498 -0
  18. data/ext/containers/splaytree_map/extconf.rb +4 -0
  19. data/ext/containers/splaytree_map/splaytree.c +419 -0
  20. data/lib/algorithms.rb +65 -0
  21. data/lib/algorithms/search.rb +84 -0
  22. data/lib/algorithms/sort.rb +238 -0
  23. data/lib/algorithms/string.rb +9 -0
  24. data/lib/containers/deque.rb +171 -0
  25. data/lib/containers/heap.rb +502 -0
  26. data/lib/containers/kd_tree.rb +110 -0
  27. data/lib/containers/priority_queue.rb +113 -0
  28. data/lib/containers/queue.rb +68 -0
  29. data/lib/containers/rb_tree_map.rb +398 -0
  30. data/lib/containers/splay_tree_map.rb +269 -0
  31. data/lib/containers/stack.rb +67 -0
  32. data/lib/containers/suffix_array.rb +68 -0
  33. data/lib/containers/trie.rb +182 -0
  34. data/spec/bst_gc_mark_spec.rb +25 -0
  35. data/spec/bst_spec.rb +25 -0
  36. data/spec/deque_gc_mark_spec.rb +18 -0
  37. data/spec/deque_spec.rb +108 -0
  38. data/spec/heap_spec.rb +131 -0
  39. data/spec/kd_expected_out.txt +10000 -0
  40. data/spec/kd_test_in.txt +10000 -0
  41. data/spec/kd_tree_spec.rb +34 -0
  42. data/spec/map_gc_mark_spec.rb +29 -0
  43. data/spec/priority_queue_spec.rb +75 -0
  44. data/spec/queue_spec.rb +61 -0
  45. data/spec/rb_tree_map_spec.rb +123 -0
  46. data/spec/search_spec.rb +28 -0
  47. data/spec/sort_spec.rb +28 -0
  48. data/spec/splay_tree_map_spec.rb +106 -0
  49. data/spec/stack_spec.rb +60 -0
  50. data/spec/string_spec.rb +15 -0
  51. data/spec/suffix_array_spec.rb +40 -0
  52. data/spec/trie_spec.rb +59 -0
  53. metadata +106 -0
@@ -0,0 +1,193 @@
1
+ === January 22, 2013
2
+
3
+ * JRuby support
4
+
5
+ === April 15, 2012
6
+
7
+ * Use long instead of int for string methods
8
+ * Use VALUE instead of int for comparison vars
9
+ * Now compiles without warnings (OS X 10.7)
10
+
11
+ === April 19, 2012
12
+
13
+ * Pulled in fix for ruby 1.9 compilation error (sorry!!)
14
+ * Fix specs that broke with new rspec
15
+ * Migration to rubygems.org
16
+ * 0.4.0 release
17
+
18
+ === Jan 3, 2009
19
+
20
+ * Levenshtein distance in C
21
+
22
+ === April 3, 2009
23
+
24
+ * Finished C refactorization of SplayTree
25
+
26
+ === March 28, 2009
27
+
28
+ * Implemented SplayTree in C
29
+ * Made recursively_free_nodes methods static to fix a SEGFAULT
30
+ * Improved CBst
31
+ * Moved to Markdown for README
32
+ * 0.2.0 release
33
+
34
+ === January 19, 2009
35
+
36
+ * kd-tree for points in multi-dimensional space
37
+
38
+ === November 25, 2008
39
+
40
+ * Checked in gnufied's C BST
41
+
42
+ === November 13, 2008
43
+
44
+ * Removed #each for Hash and Priority Queue (Feature)
45
+
46
+ === September 15, 2008
47
+
48
+ * Added comb sort
49
+ * Benchmark work on sorting algorithms
50
+
51
+ === September 1, 2008
52
+
53
+ * Switched to Hanna rdoc template
54
+ * RBTree#isred now private
55
+
56
+ === August 20, 2008
57
+
58
+ * Implemented Knuth-Morris-Pratt substring algorithm
59
+
60
+ === August 15, 2008
61
+
62
+ * Updated README to reflect progress
63
+
64
+ === August 10, 2008
65
+
66
+ * Implemented mergesort, insertion_sort, shell_sort, quicksort
67
+
68
+ === August 8, 2008
69
+
70
+ * Implemented bubble_sort, selection_sort, heapsort
71
+
72
+ === August 5, 2008
73
+
74
+ * Started Algorithms portion
75
+ * Implemented Search#binary_search
76
+
77
+ === July 20, 2008
78
+
79
+ * Iterate over trees iteratively instead of recursively
80
+ * Implemented Deque in C
81
+
82
+ === July 15, 2008
83
+
84
+ * Refactored namespaces, thank you Austin Ziegler!
85
+
86
+ === July 14, 2008
87
+
88
+ * Use alias_method instead of alias
89
+ * Found and fixed RBTree#delete bug (finally!)
90
+ * Refactored Trie, SuffixArray, SplayTreeMap
91
+
92
+ === July 13, 2008
93
+
94
+ * Refactored Deque
95
+ * Implemented Deque#reverse_each (like Array's)
96
+
97
+ === July 12, 2008
98
+
99
+ * Reformatted some specs to be more idiomatic (Thank you Federico Builes)
100
+
101
+ === July 10, 2008
102
+
103
+ * Added algorithm complexity information for all Containers
104
+ * Implemented Trie for string representation
105
+ * Implmented SuffixArray for fast substring search
106
+ * Fixed memory leak in CRBTree
107
+ * Updated Manifest and algorithms.rb to match progress
108
+
109
+ === July 9, 2008
110
+
111
+ * Implemented Deque
112
+ * Stack and Queue now use Deque
113
+ * Fixed issues with CRBTree's #empty? and delete methods
114
+
115
+ === July 8, 2008
116
+
117
+ * Can now iterate over a heap
118
+ * Renamed #contains_key -> has_key? since it's more idiomatic
119
+ * Implented #change_key and #delete for Heap
120
+ * Priority Queue is now implemented with the new Fibonacci Heap
121
+ * Removed old Priority Queue code as a result
122
+ * Heap: fixed #delete bug not checking if item exists, #has_key? bug
123
+ for not returning boolean
124
+ * Heap: value field is now optional and defaults to the key if not specified
125
+ * More refactoring of RBTreeMap (both Ruby and C)
126
+
127
+ === July 7, 2008
128
+
129
+ * Heap is now implemented with a Fibonacci Heap, not a binomial heap
130
+
131
+ === July 4, 2008
132
+
133
+ * Implemented SplayTreeMap
134
+ * Heap now uses kind_of? to check for other heaps when doing #merge
135
+ * Renamed some Heap methods for consistency with the rest of the library
136
+ * RBTreeMap#contains? -> contains_key?
137
+ * Refactored RBTreeMap to be more object-oriented
138
+ * More documentation for RBTreeMap
139
+
140
+ === July 3, 2008
141
+
142
+ * Added documentation for Stack and Queue
143
+
144
+ === June 24, 2008
145
+
146
+ * Imported Brian Amberg's priority queue implementation
147
+ * Now uses Echoe to build gem
148
+ * Gem builds for the first time
149
+
150
+ === June 18, 2008
151
+
152
+ * Can now enumerate over RBTreeMap
153
+
154
+ === June 17, 2008
155
+
156
+ * RBTreemap#delete now returns deleted value
157
+ * Added delete method to C implementation
158
+
159
+ === June 16, 2008
160
+
161
+ * Implemented delete methods for RBTreeMap
162
+
163
+ === June 14, 2008
164
+
165
+ * Renamed the data structures module to "Containers"
166
+ * Removed dependence on stdbool.h
167
+ * Renamed RBTree to RBTreeMap
168
+
169
+ === June 13, 2008
170
+
171
+ * Implemented Sedgewick's Left Leaning Red Black Tree in C!
172
+
173
+ === June 12, 2008
174
+
175
+ * Implemented Sedgewick's Left Leaning Red Black Tree
176
+
177
+ === June 10, 2008
178
+
179
+ * Implemented merge! for other heaps and heap initialization from an array
180
+ * Implemented Queue
181
+
182
+ === June 9, 2008
183
+
184
+ * Finished binomial heap implementation
185
+
186
+ === June 8, 2008
187
+
188
+ * Added Stack
189
+ * Working on heap
190
+
191
+ === April 20
192
+
193
+ * Accepted to Google Summer of Code!
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "rspec"
7
+ gem "rake-compiler"
8
+ end
9
+
@@ -0,0 +1,51 @@
1
+ CHANGELOG.markdown
2
+ Manifest
3
+ README.markdown
4
+ Rakefile
5
+ algorithms.gemspec
6
+ benchmarks/deque.rb
7
+ benchmarks/sorts.rb
8
+ benchmarks/treemaps.rb
9
+ ext/algorithms/string/extconf.rb
10
+ ext/algorithms/string/string.c
11
+ ext/containers/bst/bst.c
12
+ ext/containers/bst/extconf.rb
13
+ ext/containers/deque/deque.c
14
+ ext/containers/deque/extconf.rb
15
+ ext/containers/rbtree_map/extconf.rb
16
+ ext/containers/rbtree_map/rbtree.c
17
+ ext/containers/splaytree_map/extconf.rb
18
+ ext/containers/splaytree_map/splaytree.c
19
+ lib/algorithms.rb
20
+ lib/algorithms/search.rb
21
+ lib/algorithms/sort.rb
22
+ lib/algorithms/string.rb
23
+ lib/containers/deque.rb
24
+ lib/containers/heap.rb
25
+ lib/containers/kd_tree.rb
26
+ lib/containers/priority_queue.rb
27
+ lib/containers/queue.rb
28
+ lib/containers/rb_tree_map.rb
29
+ lib/containers/splay_tree_map.rb
30
+ lib/containers/stack.rb
31
+ lib/containers/suffix_array.rb
32
+ lib/containers/trie.rb
33
+ spec/bst_gc_mark_spec.rb
34
+ spec/bst_spec.rb
35
+ spec/deque_gc_mark_spec.rb
36
+ spec/deque_spec.rb
37
+ spec/heap_spec.rb
38
+ spec/kd_expected_out.txt
39
+ spec/kd_test_in.txt
40
+ spec/kd_tree_spec.rb
41
+ spec/map_gc_mark_spec.rb
42
+ spec/priority_queue_spec.rb
43
+ spec/queue_spec.rb
44
+ spec/rb_tree_map_spec.rb
45
+ spec/search_spec.rb
46
+ spec/sort_spec.rb
47
+ spec/splay_tree_map_spec.rb
48
+ spec/stack_spec.rb
49
+ spec/string_spec.rb
50
+ spec/suffix_array_spec.rb
51
+ spec/trie_spec.rb
@@ -0,0 +1,84 @@
1
+ # algorithms [![Build Status](https://travis-ci.org/kanwei/algorithms.png)](https://travis-ci.org/kanwei/algorithms)
2
+
3
+ ## DESCRIPTION:
4
+
5
+ Started as a [Google Summer of Code 2008](http://code.google.com/soc/2008/ruby/about.html) project
6
+
7
+ Written by [Kanwei Li](http://kanwei.com/), mentored by Austin Ziegler
8
+
9
+ ### Original Proposal: ###
10
+
11
+ Using the right data structure or algorithm for the situation is an important
12
+ aspect of programming. In computer science literature, many data structures
13
+ and algorithms have been researched and extensively documented. However, there
14
+ is still no standard library in Ruby implementing useful structures and
15
+ algorithms like Red/Black Trees, tries, different sorting algorithms, etc.
16
+ This project will create such a library with documentation on when to use a
17
+ particular structure/algorithm. It will also come with a benchmark suite to
18
+ compare performance in different situations.
19
+
20
+ ## COMPLETED:
21
+
22
+ * Heaps Containers::Heap, Containers::MaxHeap, Containers::MinHeap
23
+ * Priority Queue Containers::PriorityQueue
24
+ * Deque Containers::Deque, Containers::CDeque (C ext)
25
+ * Stack Containers::Stack
26
+ * Queue Containers::Queue
27
+ * Red-Black Trees Containers::RBTreeMap, Containers::CRBTreeMap (C ext)
28
+ * Splay Trees Containers::SplayTreeMap, Containers::CSplayTreeMap (C ext)
29
+ * Tries Containers::Trie
30
+ * Suffix Array Containers::SuffixArray
31
+
32
+ * Search algorithms
33
+ - Binary Search Algorithms::Search.binary_search
34
+ - Knuth-Morris-Pratt Algorithms::Search.kmp_search
35
+ * Sorting algorithms
36
+ - Bubble sort Algorithms::Sort.bubble_sort
37
+ - Comb sort Algorithms::Sort.comb_sort
38
+ - Selection sort Algorithms::Sort.selection_sort
39
+ - Heapsort Algorithms::Sort.heapsort
40
+ - Insertion sort Algorithms::Sort.insertion_sort
41
+ - Shell sort Algorithms::Sort.shell_sort
42
+ - Quicksort Algorithms::Sort.quicksort
43
+ - Mergesort Algorithms::Sort.mergesort
44
+
45
+ ## SYNOPSIS:
46
+
47
+ require 'rubygems'
48
+ require 'algorithms'
49
+
50
+ max_heap = Containers::MaxHeap.new
51
+
52
+ # To not have to type "Containers::" before each class, use:
53
+ include Containers
54
+ max_heap = MaxHeap.new
55
+
56
+ ## REQUIREMENTS:
57
+
58
+ * Ruby 1.8, Ruby 1.9, JRuby
59
+ * C extensions (optional, but very much recommended for vast performance benefits)
60
+
61
+ ## LICENSE:
62
+
63
+ (The MIT License)
64
+
65
+ Ruby Algorithms and Containers project is Copyright (c) 2009 Kanwei Li
66
+
67
+ Permission is hereby granted, free of charge, to any person obtaining
68
+ a copy of this software and associated documentation files (the
69
+ 'Software'), to deal in the Software without restriction, including
70
+ without limitation the rights to use, copy, modify, merge, publish,
71
+ distribute, sublicense, and/or sell copies of the Software, and to
72
+ permit persons to whom the Software is furnished to do so, subject to
73
+ the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be
76
+ included in all copies or substantial portions of the Software.
77
+
78
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
79
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
80
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
81
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
82
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
83
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
84
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'rake/extensiontask'
3
+ require 'rspec/core/rake_task'
4
+ require 'bundler/gem_tasks'
5
+
6
+ Rake::ExtensionTask.new('algorithms/string') { |ext| ext.name = "CString" }
7
+ Rake::ExtensionTask.new('containers/deque') { |ext| ext.name = "CDeque" }
8
+ Rake::ExtensionTask.new('containers/bst') { |ext| ext.name = "CBst" }
9
+ Rake::ExtensionTask.new('containers/rbtree_map') { |ext| ext.name = "CRBTreeMap" }
10
+ Rake::ExtensionTask.new('containers/splaytree_map') { |ext| ext.name = "CSplayTreeMap" }
11
+
12
+ RSpec::Core::RakeTask.new
13
+
14
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
15
+ task :default => [:spec]
16
+ else
17
+ task :default => [:compile, :spec]
18
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "algorithms"
5
+ s.version = "0.6.1"
6
+
7
+ s.authors = ["Kanwei Li"]
8
+ s.email = "kanwei@gmail.com"
9
+ s.license = 'MIT'
10
+ s.date = "2013-01-22"
11
+ s.summary = "Useful algorithms and data structures for Ruby. Optional C extensions."
12
+ s.description = "Heap, Priority Queue, Deque, Stack, Queue, Red-Black Trees, Splay Trees, sorting algorithms, and more"
13
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
14
+ s.platform = "java"
15
+ else
16
+ s.extensions = ["ext/algorithms/string/extconf.rb", "ext/containers/bst/extconf.rb", "ext/containers/deque/extconf.rb", "ext/containers/rbtree_map/extconf.rb", "ext/containers/splaytree_map/extconf.rb"]
17
+ end
18
+ s.files = ["Gemfile", "CHANGELOG.markdown", "Manifest", "README.markdown", "Rakefile", "algorithms.gemspec", "benchmarks/deque.rb", "benchmarks/sorts.rb", "benchmarks/treemaps.rb", "ext/algorithms/string/extconf.rb", "ext/algorithms/string/string.c", "ext/containers/bst/bst.c", "ext/containers/bst/extconf.rb", "ext/containers/deque/deque.c", "ext/containers/deque/extconf.rb", "ext/containers/rbtree_map/extconf.rb", "ext/containers/rbtree_map/rbtree.c", "ext/containers/splaytree_map/extconf.rb", "ext/containers/splaytree_map/splaytree.c", "lib/algorithms.rb", "lib/algorithms/search.rb", "lib/algorithms/sort.rb", "lib/algorithms/string.rb", "lib/containers/deque.rb", "lib/containers/heap.rb", "lib/containers/kd_tree.rb", "lib/containers/priority_queue.rb", "lib/containers/queue.rb", "lib/containers/rb_tree_map.rb", "lib/containers/splay_tree_map.rb", "lib/containers/stack.rb", "lib/containers/suffix_array.rb", "lib/containers/trie.rb", "spec/bst_gc_mark_spec.rb", "spec/bst_spec.rb", "spec/deque_gc_mark_spec.rb", "spec/deque_spec.rb", "spec/heap_spec.rb", "spec/kd_expected_out.txt", "spec/kd_test_in.txt", "spec/kd_tree_spec.rb", "spec/map_gc_mark_spec.rb", "spec/priority_queue_spec.rb", "spec/queue_spec.rb", "spec/rb_tree_map_spec.rb", "spec/search_spec.rb", "spec/sort_spec.rb", "spec/splay_tree_map_spec.rb", "spec/stack_spec.rb", "spec/string_spec.rb", "spec/suffix_array_spec.rb", "spec/trie_spec.rb"]
19
+ s.homepage = "https://github.com/kanwei/algorithms"
20
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Algorithms", "--main", "README.markdown"]
21
+ s.require_paths = ["lib", "ext"]
22
+ s.rubyforge_project = "algorithms"
23
+ end
@@ -0,0 +1,17 @@
1
+ $: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
2
+ require 'algorithms'
3
+ include Algorithms
4
+
5
+ require 'rubygems'
6
+ require 'rbench'
7
+
8
+ RBench.run(2) do
9
+ %w(array deque).each { |s| self.send(:column, s.intern) }
10
+ deque = Containers::Deque.new
11
+ array = []
12
+
13
+ report "Insertion at end" do
14
+ array { 1000000.times { |x| array << x } }
15
+ deque { 1000000.times { |x| deque.push_back(x) } }
16
+ end
17
+ end
@@ -0,0 +1,34 @@
1
+ $: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
2
+ require 'algorithms'
3
+ include Algorithms
4
+
5
+ require 'rubygems'
6
+ require 'rbench'
7
+
8
+ RBench.run(5) do
9
+
10
+ sorts = %w(ruby comb_sort heapsort insertion_sort shell_sort quicksort mergesort)
11
+ sorts.each { |sort| self.send(:column, sort.intern) }
12
+
13
+ n = 1000
14
+
15
+ proc = lambda { |scope, ary|
16
+ scope.ruby { ary.dup.sort }
17
+ scope.comb_sort { Sort.comb_sort(ary.dup) }
18
+ scope.heapsort { Sort.heapsort(ary.dup) }
19
+ scope.insertion_sort { Sort.insertion_sort(ary.dup) }
20
+ scope.shell_sort { Sort.shell_sort(ary.dup) }
21
+ scope.quicksort { Sort.quicksort(ary.dup) }
22
+ scope.mergesort { Sort.mergesort(ary.dup) }
23
+ }
24
+
25
+ report "Already sorted" do
26
+ sorted_array = Array.new(n) { rand(n) }.sort
27
+ proc.call(self, sorted_array)
28
+ end
29
+
30
+ report "Random" do
31
+ random_array = Array.new(n) { rand(n) }
32
+ proc.call(self, random_array)
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ $: << File.join(File.expand_path(File.dirname(__FILE__)), '../lib')
2
+ require 'algorithms'
3
+ include Containers
4
+
5
+ require 'rubygems'
6
+ require 'rbench'
7
+
8
+ RBench.run(2) do
9
+ trees = %w(hash rbtree splaytree)
10
+ trees.each { |tree| self.send(:column, tree.intern) }
11
+
12
+ rbtree = RBTreeMap.new
13
+ splaytree = SplayTreeMap.new
14
+ hash = Hash.new
15
+
16
+ random_array = Array.new(300000) { |i| rand(i) }
17
+
18
+ report "Insertion" do
19
+ rbtree { random_array.each_with_index { |x,index| rbtree[index] = x } }
20
+ splaytree { random_array.each_with_index { |x,index| splaytree[index] = x } }
21
+ hash { random_array.each_with_index { |x,index| hash[index] = x } }
22
+ end
23
+
24
+ report "has_key? (linear order)" do
25
+ rbtree { random_array.each { |n| rbtree.has_key?(n) } }
26
+ splaytree { random_array.each { |n| splaytree.has_key?(n) } }
27
+ hash { random_array.each { |n| hash.has_key?(n) } }
28
+ end
29
+
30
+ report "Lookup in sorted order" do
31
+ rbtree { rbtree.each { |k, v| k } }
32
+ splaytree { splaytree.each { |k, v| k } }
33
+ hash { hash.sort.each { |k, v| k } }
34
+
35
+ # a1, a2, a3 = [], [], []
36
+ # rbtree.each { |k, v| a1 << k }
37
+ # splaytree.each { |k, v| a2 << k }
38
+ # hash.sort.each { |k, v| a3 << k }
39
+ #
40
+ # puts "Lookup correct" if a1 == a2 && a1 == a3
41
+ end
42
+
43
+ report "Random lookups in a smaller subset" do
44
+ select_subset = random_array[0..random_array.size/20] # 5%
45
+ size = select_subset.size
46
+ rbtree { 10000.times { rbtree[ select_subset[rand(size)] ] } }
47
+ splaytree { 10000.times { splaytree[ select_subset[rand(size)] ] } }
48
+ hash { 10000.times { hash[ select_subset[rand(size)] ] } }
49
+ end
50
+
51
+ end