kanwei-algorithms 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/History.txt +168 -0
  2. data/Manifest +41 -0
  3. data/README.markdown +92 -0
  4. data/Rakefile +31 -0
  5. data/algorithms.gemspec +32 -0
  6. data/benchmarks/deque.rb +17 -0
  7. data/benchmarks/sorts.rb +34 -0
  8. data/benchmarks/treemaps.rb +36 -0
  9. data/ext/containers/deque/deque.c +247 -0
  10. data/ext/containers/deque/extconf.rb +4 -0
  11. data/ext/containers/rbtree_map/extconf.rb +4 -0
  12. data/ext/containers/rbtree_map/rbtree.c +473 -0
  13. data/ext/containers/splaytree_map/extconf.rb +4 -0
  14. data/ext/containers/splaytree_map/splaytree.c +370 -0
  15. data/lib/algorithms/search.rb +84 -0
  16. data/lib/algorithms/sort.rb +238 -0
  17. data/lib/algorithms.rb +67 -0
  18. data/lib/containers/deque.rb +171 -0
  19. data/lib/containers/heap.rb +486 -0
  20. data/lib/containers/kd_tree.rb +87 -0
  21. data/lib/containers/priority_queue.rb +113 -0
  22. data/lib/containers/queue.rb +68 -0
  23. data/lib/containers/rb_tree_map.rb +398 -0
  24. data/lib/containers/splay_tree_map.rb +269 -0
  25. data/lib/containers/stack.rb +67 -0
  26. data/lib/containers/suffix_array.rb +68 -0
  27. data/lib/containers/trie.rb +182 -0
  28. data/spec/deque_gc_mark_spec.rb +18 -0
  29. data/spec/deque_spec.rb +108 -0
  30. data/spec/heap_spec.rb +126 -0
  31. data/spec/kd_tree_spec.rb +4 -0
  32. data/spec/priority_queue_spec.rb +75 -0
  33. data/spec/queue_spec.rb +61 -0
  34. data/spec/rb_tree_map_gc_mark_spec.rb +25 -0
  35. data/spec/rb_tree_map_spec.rb +123 -0
  36. data/spec/search_spec.rb +28 -0
  37. data/spec/sort_spec.rb +28 -0
  38. data/spec/splay_tree_map_spec.rb +102 -0
  39. data/spec/stack_spec.rb +60 -0
  40. data/spec/suffix_array_spec.rb +40 -0
  41. data/spec/trie_spec.rb +59 -0
  42. metadata +120 -0
data/History.txt ADDED
@@ -0,0 +1,168 @@
1
+ === March 28, 2009
2
+
3
+ * Implemented SplayTree in C
4
+ * Made recursively_free_nodes methods static to fix a SEGFAULT
5
+ * Improved CBst
6
+ * Moved to Markdown for README
7
+ * 0.2.0 release
8
+
9
+ === January 19, 2009
10
+
11
+ * kd-tree for points in multi-dimensional space
12
+
13
+ === November 25, 2008
14
+
15
+ * Checked in gnufied's C BST
16
+
17
+ === November 13, 2008
18
+
19
+ * Removed #each for Hash and Priority Queue (Feature)
20
+
21
+ === September 15, 2008
22
+
23
+ * Added comb sort
24
+ * Benchmark work on sorting algorithms
25
+
26
+ === September 1, 2008
27
+
28
+ * Switched to Hanna rdoc template
29
+ * RBTree#isred now private
30
+
31
+ === August 20, 2008
32
+
33
+ * Implemented Knuth-Morris-Pratt substring algorithm
34
+
35
+ === August 15, 2008
36
+
37
+ * Updated README to reflect progress
38
+
39
+ === August 10, 2008
40
+
41
+ * Implemented mergesort, insertion_sort, shell_sort, quicksort
42
+
43
+ === August 8, 2008
44
+
45
+ * Implemented bubble_sort, selection_sort, heapsort
46
+
47
+ === August 5, 2008
48
+
49
+ * Started Algorithms portion
50
+ * Implemented Search#binary_search
51
+
52
+ === July 20, 2008
53
+
54
+ * Iterate over trees iteratively instead of recursively
55
+ * Implemented Deque in C
56
+
57
+ === July 15, 2008
58
+
59
+ * Refactored namespaces, thank you Austin Ziegler!
60
+
61
+ === July 14, 2008
62
+
63
+ * Use alias_method instead of alias
64
+ * Found and fixed RBTree#delete bug (finally!)
65
+ * Refactored Trie, SuffixArray, SplayTreeMap
66
+
67
+ === July 13, 2008
68
+
69
+ * Refactored Deque
70
+ * Implemented Deque#reverse_each (like Array's)
71
+
72
+ === July 12, 2008
73
+
74
+ * Reformatted some specs to be more idiomatic (Thank you Federico Builes)
75
+
76
+ === July 10, 2008
77
+
78
+ * Added algorithm complexity information for all Containers
79
+ * Implemented Trie for string representation
80
+ * Implmented SuffixArray for fast substring search
81
+ * Fixed memory leak in CRBTree
82
+ * Updated Manifest and algorithms.rb to match progress
83
+
84
+ === July 9, 2008
85
+
86
+ * Implemented Deque
87
+ * Stack and Queue now use Deque
88
+ * Fixed issues with CRBTree's #empty? and delete methods
89
+
90
+ === July 8, 2008
91
+
92
+ * Can now iterate over a heap
93
+ * Renamed #contains_key -> has_key? since it's more idiomatic
94
+ * Implented #change_key and #delete for Heap
95
+ * Priority Queue is now implemented with the new Fibonacci Heap
96
+ * Removed old Priority Queue code as a result
97
+ * Heap: fixed #delete bug not checking if item exists, #has_key? bug
98
+ for not returning boolean
99
+ * Heap: value field is now optional and defaults to the key if not specified
100
+ * More refactoring of RBTreeMap (both Ruby and C)
101
+
102
+ === July 7, 2008
103
+
104
+ * Heap is now implemented with a Fibonacci Heap, not a binomial heap
105
+
106
+ === July 4, 2008
107
+
108
+ * Implemented SplayTreeMap
109
+ * Heap now uses kind_of? to check for other heaps when doing #merge
110
+ * Renamed some Heap methods for consistency with the rest of the library
111
+ * RBTreeMap#contains? -> contains_key?
112
+ * Refactored RBTreeMap to be more object-oriented
113
+ * More documentation for RBTreeMap
114
+
115
+ === July 3, 2008
116
+
117
+ * Added documentation for Stack and Queue
118
+
119
+ === June 24, 2008
120
+
121
+ * Imported Brian Amberg's priority queue implementation
122
+ * Now uses Echoe to build gem
123
+ * Gem builds for the first time
124
+
125
+ === June 18, 2008
126
+
127
+ * Can now enumerate over RBTreeMap
128
+
129
+ === June 17, 2008
130
+
131
+ * RBTreemap#delete now returns deleted value
132
+ * Added delete method to C implementation
133
+
134
+ === June 16, 2008
135
+
136
+ * Implemented delete methods for RBTreeMap
137
+
138
+ === June 14, 2008
139
+
140
+ * Renamed the data structures module to "Containers"
141
+ * Removed dependence on stdbool.h
142
+ * Renamed RBTree to RBTreeMap
143
+
144
+ === June 13, 2008
145
+
146
+ * Implemented Sedgewick's Left Leaning Red Black Tree in C!
147
+
148
+ === June 12, 2008
149
+
150
+ * Implemented Sedgewick's Left Leaning Red Black Tree
151
+
152
+ === June 10, 2008
153
+
154
+ * Implemented merge! for other heaps and heap initialization from an array
155
+ * Implemented Queue
156
+
157
+ === June 9, 2008
158
+
159
+ * Finished binomial heap implementation
160
+
161
+ === June 8, 2008
162
+
163
+ * Added Stack
164
+ * Working on heap
165
+
166
+ === April 20
167
+
168
+ * Accepted to Google Summer of Code!
data/Manifest ADDED
@@ -0,0 +1,41 @@
1
+ algorithms.gemspec
2
+ benchmarks/deque.rb
3
+ benchmarks/sorts.rb
4
+ benchmarks/treemaps.rb
5
+ ext/containers/deque/deque.c
6
+ ext/containers/deque/extconf.rb
7
+ ext/containers/rbtree_map/extconf.rb
8
+ ext/containers/rbtree_map/rbtree.c
9
+ ext/containers/splaytree_map/extconf.rb
10
+ ext/containers/splaytree_map/splaytree.c
11
+ History.txt
12
+ lib/algorithms/search.rb
13
+ lib/algorithms/sort.rb
14
+ lib/algorithms.rb
15
+ lib/containers/deque.rb
16
+ lib/containers/heap.rb
17
+ lib/containers/kd_tree.rb
18
+ lib/containers/priority_queue.rb
19
+ lib/containers/queue.rb
20
+ lib/containers/rb_tree_map.rb
21
+ lib/containers/splay_tree_map.rb
22
+ lib/containers/stack.rb
23
+ lib/containers/suffix_array.rb
24
+ lib/containers/trie.rb
25
+ Manifest
26
+ Rakefile
27
+ README.markdown
28
+ spec/deque_gc_mark_spec.rb
29
+ spec/deque_spec.rb
30
+ spec/heap_spec.rb
31
+ spec/kd_tree_spec.rb
32
+ spec/priority_queue_spec.rb
33
+ spec/queue_spec.rb
34
+ spec/rb_tree_map_gc_mark_spec.rb
35
+ spec/rb_tree_map_spec.rb
36
+ spec/search_spec.rb
37
+ spec/sort_spec.rb
38
+ spec/splay_tree_map_spec.rb
39
+ spec/stack_spec.rb
40
+ spec/suffix_array_spec.rb
41
+ spec/trie_spec.rb
data/README.markdown ADDED
@@ -0,0 +1,92 @@
1
+ # algorithms
2
+
3
+ * Official homes are here on github, and at [rubyforge](http://rubyforge.org/projects/algorithms/)
4
+ * Documentation: [http://algorithms.rubyforge.org/](http://algorithms.rubyforge.org/)
5
+
6
+ ## DESCRIPTION:
7
+
8
+ Started as a [Google Summer of Code 2008](http://code.google.com/soc/2008/ruby/about.html) project
9
+
10
+ Written by [Kanwei Li](http://kanwei.com/), mentored by Austin Ziegler
11
+
12
+ Original Proposal: Using the right data structure or algorithm for the situation is an important
13
+ aspect of programming. In computer science literature, many data structures
14
+ and algorithms have been researched and extensively documented. However, there
15
+ is still no standard library in Ruby implementing useful structures and
16
+ algorithms like Red/Black Trees, tries, different sorting algorithms, etc.
17
+ This project will create such a library with documentation on when to use a
18
+ particular structure/algorithm. It will also come with a benchmark suite to
19
+ compare performance in different situations.
20
+
21
+ ## FEATURES:
22
+
23
+ Done so far:
24
+
25
+ * Heaps Containers::Heap, Containers::MaxHeap, Containers::MinHeap
26
+ * Priority Queue Containers::PriorityQueue
27
+ * Deque Containers::Deque, Containers::CDeque (C extension), Containers::RubyDeque
28
+ * Stack Containers::Stack (uses Deque)
29
+ * Queue Containers::Queue (uses Deque)
30
+ * Red-Black Trees Containers::RBTreeMap, Containers::CRBTreeMap (C extension), Containers::RubyRBTreeMap
31
+ * Splay Trees Containers::SplayTreeMap, Containers::CSplayTreeMap (C extension), Containers::RubySplayTreeMap
32
+ * Tries Containers::Trie
33
+ * Suffix Array Containers::SuffixArray
34
+
35
+ * Search algorithms
36
+ - Binary Search Algorithms::Search.binary_search
37
+ - Knuth-Morris-Pratt Algorithms::Search.kmp_search
38
+ * Sort algorithms
39
+ - Bubble sort Algorithms::Sort.bubble_sort
40
+ - Comb sort Algorithms::Sort.comb_sort
41
+ - Selection sort Algorithms::Sort.selection_sort
42
+ - Heapsort Algorithms::Sort.heapsort
43
+ - Insertion sort Algorithms::Sort.insertion_sort
44
+ - Shell sort Algorithms::Sort.shell_sort
45
+ - Quicksort Algorithms::Sort.quicksort
46
+ - Mergesort Algorithms::Sort.mergesort
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
+
60
+ ## REQUIREMENTS:
61
+
62
+ * Ruby 1.8 compatible Ruby, or Ruby 1.9
63
+ * C compiler for C extensions (optional, but very much recommended for vast performance benefits)
64
+
65
+ ## INSTALL:
66
+
67
+ * sudo gem install algorithms
68
+
69
+ ## LICENSE:
70
+
71
+ (The MIT License)
72
+
73
+ Algorithms and Containers project is Copyright (c) 2009 Kanwei Li
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining
76
+ a copy of this software and associated documentation files (the
77
+ 'Software'), to deal in the Software without restriction, including
78
+ without limitation the rights to use, copy, modify, merge, publish,
79
+ distribute, sublicense, and/or sell copies of the Software, and to
80
+ permit persons to whom the Software is furnished to do so, subject to
81
+ the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be
84
+ included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
89
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
90
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
91
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
92
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'echoe'
3
+
4
+ Echoe.new('algorithms') do |p|
5
+ p.author = 'Kanwei Li'
6
+ p.email = 'kanwei@gmail.com'
7
+ p.summary = 'A library of algorithms and containers.'
8
+ p.url = 'http://rubyforge.org/projects/algorithms/'
9
+ p.version = "0.2.0"
10
+ p.runtime_dependencies = []
11
+ end
12
+
13
+ task :default => :spec
14
+
15
+ task :spec do
16
+ sh "spec spec/*.rb --color"
17
+ end
18
+
19
+ task :push do
20
+ sh "git push" # Rubyforge
21
+ sh "git push --tags" # Rubyforge
22
+ sh "git push gh" # Github
23
+ sh "git push gh --tags" # Github
24
+ end
25
+
26
+ task :hanna do
27
+ sh "rm -fr doc"
28
+ sh "hanna -SN lib/ -m Algorithms"
29
+ sh "scp -rq doc/* kanwei@rubyforge.org:/var/www/gforge-projects/algorithms"
30
+ end
31
+
@@ -0,0 +1,32 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{algorithms}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Kanwei Li"]
9
+ s.date = %q{2009-03-29}
10
+ s.description = %q{A library of algorithms and containers.}
11
+ s.email = %q{kanwei@gmail.com}
12
+ s.extensions = ["ext/containers/deque/extconf.rb", "ext/containers/rbtree_map/extconf.rb", "ext/containers/splaytree_map/extconf.rb"]
13
+ s.extra_rdoc_files = ["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/search.rb", "lib/algorithms/sort.rb", "lib/algorithms.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", "README.markdown"]
14
+ s.files = ["algorithms.gemspec", "benchmarks/deque.rb", "benchmarks/sorts.rb", "benchmarks/treemaps.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", "History.txt", "lib/algorithms/search.rb", "lib/algorithms/sort.rb", "lib/algorithms.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", "Manifest", "Rakefile", "README.markdown", "spec/deque_gc_mark_spec.rb", "spec/deque_spec.rb", "spec/heap_spec.rb", "spec/kd_tree_spec.rb", "spec/priority_queue_spec.rb", "spec/queue_spec.rb", "spec/rb_tree_map_gc_mark_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/suffix_array_spec.rb", "spec/trie_spec.rb"]
15
+ s.has_rdoc = true
16
+ s.homepage = %q{http://rubyforge.org/projects/algorithms/}
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Algorithms", "--main", "README.markdown"]
18
+ s.require_paths = ["lib", "ext"]
19
+ s.rubyforge_project = %q{algorithms}
20
+ s.rubygems_version = %q{1.3.1}
21
+ s.summary = %q{A library of algorithms and containers.}
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 2
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ else
29
+ end
30
+ else
31
+ end
32
+ 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,36 @@
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(10) do
9
+ trees = %w(hash rbtree splaytree)
10
+ trees.each { |tree| self.send(:column, tree.intern) }
11
+
12
+ rbtree = Containers::RBTreeMap.new
13
+ splaytree = Containers::SplayTreeMap.new
14
+ hash = Hash.new
15
+
16
+ random_array = Array.new(10000) { |i| rand(i) }
17
+ num = 1000
18
+
19
+ report "Insertion" do
20
+ rbtree { random_array.each_with_index { |x,index| rbtree[index] = x } }
21
+ splaytree { random_array.each_with_index { |x,index| splaytree[index] = x } }
22
+ hash { random_array.each_with_index { |x,index| hash[index] = x } }
23
+ end
24
+
25
+ report "has_key?" do
26
+ rbtree { num.times { |n| rbtree.has_key?(n) } }
27
+ splaytree { num.times { |n| splaytree.has_key?(n) } }
28
+ hash { num.times { |n| hash.has_key?(n) } }
29
+ end
30
+
31
+ report "Lookup in sorted order" do
32
+ rbtree { rbtree.each { |k, v| k } }
33
+ splaytree { splaytree.each { |k, v| k } }
34
+ hash { hash.sort.each { |k, v| k } }
35
+ end
36
+ end