amorim-algorithms 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
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 +87 -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 +499 -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 +108 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 588b03be1dd0e9aafdb59ff3bb9f16ea1bc34493
4
+ data.tar.gz: 6dcaf86182d26f05a25f42d55bf822e7ab061819
5
+ SHA512:
6
+ metadata.gz: d0fc04e9d6fc6b96498924baceecdef51b23247e72510b99a743e88a8099dc44ae015a56f96232f5fd3fecd7b450215a491f28fa91786bcb580c11db63f8357b
7
+ data.tar.gz: d5268551194ea05b17f218098214eedcd7dfc36dacbf9b4485f73b1c7fe51530e2a3added6a1f2d490f1e1655de82c1fb35aa720267a0d7eb9f6d5957c12fe94
@@ -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,87 @@
1
+ # algorithms [![Build Status](https://travis-ci.org/kanwei/algorithms.png)](https://travis-ci.org/kanwei/algorithms)
2
+
3
+ [API Documentation](http://kanwei.github.io/algorithms/)
4
+
5
+ ## DESCRIPTION:
6
+
7
+ Started as a [Google Summer of Code 2008](http://code.google.com/soc/2008/ruby/about.html) project
8
+
9
+ Written by [Kanwei Li](http://kanwei.com/), mentored by Austin Ziegler
10
+
11
+ ### Original Proposal: ###
12
+
13
+ Using the right data structure or algorithm for the situation is an important
14
+ aspect of programming. In computer science literature, many data structures
15
+ and algorithms have been researched and extensively documented. However, there
16
+ is still no standard library in Ruby implementing useful structures and
17
+ algorithms like Red/Black Trees, tries, different sorting algorithms, etc.
18
+ This project will create such a library with documentation on when to use a
19
+ particular structure/algorithm. It will also come with a benchmark suite to
20
+ compare performance in different situations.
21
+
22
+ ## COMPLETED:
23
+
24
+ * Heaps Containers::Heap, Containers::MaxHeap, Containers::MinHeap
25
+ * Priority Queue Containers::PriorityQueue
26
+ * Deque Containers::Deque, Containers::CDeque (C ext)
27
+ * Stack Containers::Stack
28
+ * Queue Containers::Queue
29
+ * Red-Black Trees Containers::RBTreeMap, Containers::CRBTreeMap (C ext)
30
+ * Splay Trees Containers::SplayTreeMap, Containers::CSplayTreeMap (C ext)
31
+ * Tries Containers::Trie
32
+ * Suffix Array Containers::SuffixArray
33
+
34
+ * Search algorithms
35
+ - Binary Search Algorithms::Search.binary_search
36
+ - Knuth-Morris-Pratt Algorithms::Search.kmp_search
37
+ * Sorting algorithms
38
+ - Bubble sort Algorithms::Sort.bubble_sort
39
+ - Comb sort Algorithms::Sort.comb_sort
40
+ - Selection sort Algorithms::Sort.selection_sort
41
+ - Heapsort Algorithms::Sort.heapsort
42
+ - Insertion sort Algorithms::Sort.insertion_sort
43
+ - Shell sort Algorithms::Sort.shell_sort
44
+ - Quicksort Algorithms::Sort.quicksort
45
+ - Mergesort Algorithms::Sort.mergesort
46
+ - Dual-Pivot Quicksort Algorithms::Sort.dualpivotquicksort
47
+
48
+ ## SYNOPSIS:
49
+
50
+ require 'rubygems'
51
+ require 'algorithms'
52
+
53
+ max_heap = Containers::MaxHeap.new
54
+
55
+ # To not have to type "Containers::" before each class, use:
56
+ include Containers
57
+ max_heap = MaxHeap.new
58
+
59
+ ## REQUIREMENTS:
60
+
61
+ * Ruby 1.8, Ruby 1.9, JRuby
62
+ * C extensions (optional, but very much recommended for vast performance benefits)
63
+
64
+ ## LICENSE:
65
+
66
+ (The MIT License)
67
+
68
+ Ruby Algorithms and Containers project is Copyright (c) 2009 Kanwei Li
69
+
70
+ Permission is hereby granted, free of charge, to any person obtaining
71
+ a copy of this software and associated documentation files (the
72
+ 'Software'), to deal in the Software without restriction, including
73
+ without limitation the rights to use, copy, modify, merge, publish,
74
+ distribute, sublicense, and/or sell copies of the Software, and to
75
+ permit persons to whom the Software is furnished to do so, subject to
76
+ the following conditions:
77
+
78
+ The above copyright notice and this permission notice shall be
79
+ included in all copies or substantial portions of the Software.
80
+
81
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
82
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
83
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
84
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
85
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
86
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
87
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
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
19
+
20
+ task :rdoc do
21
+ `rdoc -f hanna --main algorithms.rb -t "Ruby Algorithms and Containers Documentation"`
22
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "amorim-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