stakach-algorithms 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +97 -97
- data/Rakefile +27 -27
- data/ext/algorithms/string/extconf.rb +4 -4
- data/ext/algorithms/string/string.c +70 -70
- data/ext/containers/bst/bst.c +249 -249
- data/ext/containers/bst/extconf.rb +4 -4
- data/ext/containers/deque/deque.c +248 -248
- data/ext/containers/deque/extconf.rb +4 -4
- data/ext/containers/rbtree_map/extconf.rb +4 -4
- data/ext/containers/rbtree_map/rbtree.c +500 -500
- data/ext/containers/splaytree_map/extconf.rb +4 -4
- data/ext/containers/splaytree_map/splaytree.c +421 -421
- data/lib/algorithms/search.rb +85 -85
- data/lib/algorithms/sort.rb +242 -242
- data/lib/algorithms/string.rb +10 -10
- data/lib/algorithms/version.rb +1 -1
- data/lib/algorithms.rb +69 -69
- data/lib/containers/deque.rb +176 -176
- data/lib/containers/heap.rb +506 -506
- data/lib/containers/kd_tree.rb +112 -112
- data/lib/containers/priority_queue.rb +116 -116
- data/lib/containers/queue.rb +71 -71
- data/lib/containers/rb_tree_map.rb +402 -402
- data/lib/containers/splay_tree_map.rb +273 -273
- data/lib/containers/stack.rb +70 -70
- data/lib/containers/suffix_array.rb +71 -71
- data/lib/containers/trie.rb +187 -187
- metadata +3 -3
data/README.markdown
CHANGED
@@ -1,97 +1,97 @@
|
|
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 Algorithms::Containers::Heap, Containers::MaxHeap, Containers::MinHeap
|
26
|
-
* Priority Queue Algorithms::Containers::PriorityQueue
|
27
|
-
* Deque Algorithms::Containers::Deque, Containers::CDeque (C extension), Containers::RubyDeque
|
28
|
-
* Stack Algorithms::Containers::Stack (uses Deque)
|
29
|
-
* Queue Algorithms::Containers::Queue (uses Deque)
|
30
|
-
* Red-Black Trees Algorithms::Containers::RBTreeMap, Containers::CRBTreeMap (C extension), Containers::RubyRBTreeMap
|
31
|
-
* Splay Trees Algorithms::Containers::SplayTreeMap, Containers::CSplayTreeMap (C extension), Containers::RubySplayTreeMap
|
32
|
-
* Tries Algorithms::Containers::Trie
|
33
|
-
* Suffix Array Algorithms::Containers::SuffixArray
|
34
|
-
* kd Tree Algorithms::Containers::KDTree
|
35
|
-
|
36
|
-
* Search algorithms
|
37
|
-
- Binary Search Algorithms::Algorithms::Search.binary_search
|
38
|
-
- Knuth-Morris-Pratt Algorithms::Algorithms::Search.kmp_search
|
39
|
-
* Sort algorithms
|
40
|
-
- Bubble sort Algorithms::Algorithms::Sort.bubble_sort
|
41
|
-
- Comb sort Algorithms::Algorithms::Sort.comb_sort
|
42
|
-
- Selection sort Algorithms::Algorithms::Sort.selection_sort
|
43
|
-
- Heapsort Algorithms::Algorithms::Sort.heapsort
|
44
|
-
- Insertion sort Algorithms::Algorithms::Sort.insertion_sort
|
45
|
-
- Shell sort Algorithms::Algorithms::Sort.shell_sort
|
46
|
-
- Quicksort Algorithms::Algorithms::Sort.quicksort
|
47
|
-
- Mergesort Algorithms::Algorithms::Sort.mergesort
|
48
|
-
|
49
|
-
## SYNOPSIS:
|
50
|
-
|
51
|
-
require 'rubygems'
|
52
|
-
require 'algorithms'
|
53
|
-
max_heap = Algorithms::Containers::MaxHeap.new
|
54
|
-
|
55
|
-
# To not have to type "Algorithms::" before each class, use:
|
56
|
-
include Algorithms
|
57
|
-
|
58
|
-
max_heap = Containers::MaxHeap.new
|
59
|
-
|
60
|
-
# To not have to type "Containers::" before each class, use:
|
61
|
-
include Containers
|
62
|
-
max_heap = MaxHeap.new
|
63
|
-
|
64
|
-
|
65
|
-
## REQUIREMENTS:
|
66
|
-
|
67
|
-
* Ruby 1.8 compatible Ruby, or Ruby 1.9
|
68
|
-
* C compiler for C extensions (optional, but very much recommended for vast performance benefits)
|
69
|
-
|
70
|
-
## INSTALL:
|
71
|
-
|
72
|
-
* sudo gem install algorithms
|
73
|
-
|
74
|
-
## LICENSE:
|
75
|
-
|
76
|
-
(The MIT License)
|
77
|
-
|
78
|
-
Algorithms and Containers project is Copyright (c) 2009 Kanwei Li
|
79
|
-
|
80
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
81
|
-
a copy of this software and associated documentation files (the
|
82
|
-
'Software'), to deal in the Software without restriction, including
|
83
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
84
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
85
|
-
permit persons to whom the Software is furnished to do so, subject to
|
86
|
-
the following conditions:
|
87
|
-
|
88
|
-
The above copyright notice and this permission notice shall be
|
89
|
-
included in all copies or substantial portions of the Software.
|
90
|
-
|
91
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
92
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
93
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
94
|
-
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
95
|
-
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
96
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
97
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
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 Algorithms::Containers::Heap, Containers::MaxHeap, Containers::MinHeap
|
26
|
+
* Priority Queue Algorithms::Containers::PriorityQueue
|
27
|
+
* Deque Algorithms::Containers::Deque, Containers::CDeque (C extension), Containers::RubyDeque
|
28
|
+
* Stack Algorithms::Containers::Stack (uses Deque)
|
29
|
+
* Queue Algorithms::Containers::Queue (uses Deque)
|
30
|
+
* Red-Black Trees Algorithms::Containers::RBTreeMap, Containers::CRBTreeMap (C extension), Containers::RubyRBTreeMap
|
31
|
+
* Splay Trees Algorithms::Containers::SplayTreeMap, Containers::CSplayTreeMap (C extension), Containers::RubySplayTreeMap
|
32
|
+
* Tries Algorithms::Containers::Trie
|
33
|
+
* Suffix Array Algorithms::Containers::SuffixArray
|
34
|
+
* kd Tree Algorithms::Containers::KDTree
|
35
|
+
|
36
|
+
* Search algorithms
|
37
|
+
- Binary Search Algorithms::Algorithms::Search.binary_search
|
38
|
+
- Knuth-Morris-Pratt Algorithms::Algorithms::Search.kmp_search
|
39
|
+
* Sort algorithms
|
40
|
+
- Bubble sort Algorithms::Algorithms::Sort.bubble_sort
|
41
|
+
- Comb sort Algorithms::Algorithms::Sort.comb_sort
|
42
|
+
- Selection sort Algorithms::Algorithms::Sort.selection_sort
|
43
|
+
- Heapsort Algorithms::Algorithms::Sort.heapsort
|
44
|
+
- Insertion sort Algorithms::Algorithms::Sort.insertion_sort
|
45
|
+
- Shell sort Algorithms::Algorithms::Sort.shell_sort
|
46
|
+
- Quicksort Algorithms::Algorithms::Sort.quicksort
|
47
|
+
- Mergesort Algorithms::Algorithms::Sort.mergesort
|
48
|
+
|
49
|
+
## SYNOPSIS:
|
50
|
+
|
51
|
+
require 'rubygems'
|
52
|
+
require 'algorithms'
|
53
|
+
max_heap = Algorithms::Containers::MaxHeap.new
|
54
|
+
|
55
|
+
# To not have to type "Algorithms::" before each class, use:
|
56
|
+
include Algorithms
|
57
|
+
|
58
|
+
max_heap = Containers::MaxHeap.new
|
59
|
+
|
60
|
+
# To not have to type "Containers::" before each class, use:
|
61
|
+
include Containers
|
62
|
+
max_heap = MaxHeap.new
|
63
|
+
|
64
|
+
|
65
|
+
## REQUIREMENTS:
|
66
|
+
|
67
|
+
* Ruby 1.8 compatible Ruby, or Ruby 1.9, should also work with jRuby
|
68
|
+
* C compiler for C extensions (optional, but very much recommended for vast performance benefits)
|
69
|
+
|
70
|
+
## INSTALL:
|
71
|
+
|
72
|
+
* sudo gem install stakach-algorithms
|
73
|
+
|
74
|
+
## LICENSE:
|
75
|
+
|
76
|
+
(The MIT License)
|
77
|
+
|
78
|
+
Algorithms and Containers project is Copyright (c) 2009 Kanwei Li
|
79
|
+
|
80
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
81
|
+
a copy of this software and associated documentation files (the
|
82
|
+
'Software'), to deal in the Software without restriction, including
|
83
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
84
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
85
|
+
permit persons to whom the Software is furnished to do so, subject to
|
86
|
+
the following conditions:
|
87
|
+
|
88
|
+
The above copyright notice and this permission notice shall be
|
89
|
+
included in all copies or substantial portions of the Software.
|
90
|
+
|
91
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
92
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
93
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
94
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
95
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
96
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
97
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
CHANGED
@@ -1,27 +1,27 @@
|
|
1
|
-
require 'echoe'
|
2
|
-
require "rake/clean"
|
3
|
-
OBJ = FileList['**/*.rbc']
|
4
|
-
CLEAN.include(OBJ)
|
5
|
-
|
6
|
-
Echoe.new('algorithms') do |p|
|
7
|
-
p.author = 'Kanwei Li'
|
8
|
-
p.email = 'kanwei@gmail.com'
|
9
|
-
p.summary = 'A library of algorithms and containers.'
|
10
|
-
p.url = 'http://rubyforge.org/projects/algorithms/'
|
11
|
-
p.version = "0.2.0"
|
12
|
-
p.runtime_dependencies = []
|
13
|
-
end
|
14
|
-
|
15
|
-
task :push do
|
16
|
-
sh "git push" # Rubyforge
|
17
|
-
sh "git push --tags" # Rubyforge
|
18
|
-
sh "git push gh" # Github
|
19
|
-
sh "git push gh --tags" # Github
|
20
|
-
end
|
21
|
-
|
22
|
-
task :hanna do
|
23
|
-
sh "rm -fr doc"
|
24
|
-
sh "hanna -SN lib/ -m Algorithms"
|
25
|
-
sh "scp -rq doc/* kanwei@rubyforge.org:/var/www/gforge-projects/algorithms"
|
26
|
-
end
|
27
|
-
|
1
|
+
require 'echoe'
|
2
|
+
require "rake/clean"
|
3
|
+
OBJ = FileList['**/*.rbc']
|
4
|
+
CLEAN.include(OBJ)
|
5
|
+
|
6
|
+
Echoe.new('algorithms') do |p|
|
7
|
+
p.author = 'Kanwei Li'
|
8
|
+
p.email = 'kanwei@gmail.com'
|
9
|
+
p.summary = 'A library of algorithms and containers.'
|
10
|
+
p.url = 'http://rubyforge.org/projects/algorithms/'
|
11
|
+
p.version = "0.2.0"
|
12
|
+
p.runtime_dependencies = []
|
13
|
+
end
|
14
|
+
|
15
|
+
task :push do
|
16
|
+
sh "git push" # Rubyforge
|
17
|
+
sh "git push --tags" # Rubyforge
|
18
|
+
sh "git push gh" # Github
|
19
|
+
sh "git push gh --tags" # Github
|
20
|
+
end
|
21
|
+
|
22
|
+
task :hanna do
|
23
|
+
sh "rm -fr doc"
|
24
|
+
sh "hanna -SN lib/ -m Algorithms"
|
25
|
+
sh "scp -rq doc/* kanwei@rubyforge.org:/var/www/gforge-projects/algorithms"
|
26
|
+
end
|
27
|
+
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'mkmf'
|
2
|
-
extension_name = "CString"
|
3
|
-
dir_config(extension_name)
|
4
|
-
create_makefile(extension_name)
|
1
|
+
require 'mkmf'
|
2
|
+
extension_name = "CString"
|
3
|
+
dir_config(extension_name)
|
4
|
+
create_makefile(extension_name)
|
@@ -1,70 +1,70 @@
|
|
1
|
-
#include "ruby.h"
|
2
|
-
|
3
|
-
int get_min(int a, int b, int c) {
|
4
|
-
int min = a;
|
5
|
-
if (b < min)
|
6
|
-
min = b;
|
7
|
-
if( c < min)
|
8
|
-
min = c;
|
9
|
-
return min;
|
10
|
-
}
|
11
|
-
|
12
|
-
int levenshtein_distance(VALUE str1, VALUE str2) {
|
13
|
-
int i, j, s1_len, s2_len, *d;
|
14
|
-
char * s = RSTRING_PTR(str1);
|
15
|
-
char * t = RSTRING_PTR(str2);
|
16
|
-
s1_len = RSTRING_LEN(str1);
|
17
|
-
s2_len = RSTRING_LEN(str2);
|
18
|
-
|
19
|
-
if (s1_len == 0) {
|
20
|
-
return s2_len;
|
21
|
-
} else if (s2_len == 0) {
|
22
|
-
return s1_len;
|
23
|
-
}
|
24
|
-
|
25
|
-
// We need one extra col and row for the matrix for starting values
|
26
|
-
s1_len++;
|
27
|
-
s2_len++;
|
28
|
-
|
29
|
-
d = malloc(sizeof(int) * (s1_len) * (s2_len));
|
30
|
-
|
31
|
-
for (i = 0; i < s1_len; i++) {
|
32
|
-
d[i] = i; // d[i, 0] = i
|
33
|
-
}
|
34
|
-
for (j = 0; j < s2_len; j++) {
|
35
|
-
d[j*s1_len] = j; // d[0, j] = j
|
36
|
-
}
|
37
|
-
|
38
|
-
for (i = 1; i < s1_len; i++) {
|
39
|
-
for (j = 1; j < s2_len; j++) {
|
40
|
-
if (s[i-1] == t[j-1]) {
|
41
|
-
d[j * s1_len + i] = d[(j-1) * s1_len + (i-1)];
|
42
|
-
} else {
|
43
|
-
d[j * s1_len + i] = get_min(
|
44
|
-
d[j * s1_len + (i-1)],
|
45
|
-
d[(j-1) * s1_len + i],
|
46
|
-
d[(j-1) * s1_len + (i-1)]
|
47
|
-
) + 1;
|
48
|
-
}
|
49
|
-
}
|
50
|
-
}
|
51
|
-
i = d[s1_len * s2_len -1];
|
52
|
-
free(d);
|
53
|
-
return i;
|
54
|
-
}
|
55
|
-
|
56
|
-
static VALUE lev_dist(VALUE self, VALUE str1, VALUE str2) {
|
57
|
-
return INT2FIX(levenshtein_distance( str1, str2 ));
|
58
|
-
}
|
59
|
-
|
60
|
-
static VALUE namespace;
|
61
|
-
static VALUE mAlgorithms;
|
62
|
-
static VALUE mString;
|
63
|
-
|
64
|
-
void Init_CString() {
|
65
|
-
namespace = rb_define_module("Algorithms");
|
66
|
-
mAlgorithms = rb_define_module_under(namespace,"Algorithms");
|
67
|
-
mString = rb_define_module_under(mAlgorithms, "String");
|
68
|
-
rb_define_singleton_method(mString, "levenshtein_dist", lev_dist, 2);
|
69
|
-
}
|
70
|
-
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
int get_min(int a, int b, int c) {
|
4
|
+
int min = a;
|
5
|
+
if (b < min)
|
6
|
+
min = b;
|
7
|
+
if( c < min)
|
8
|
+
min = c;
|
9
|
+
return min;
|
10
|
+
}
|
11
|
+
|
12
|
+
int levenshtein_distance(VALUE str1, VALUE str2) {
|
13
|
+
int i, j, s1_len, s2_len, *d;
|
14
|
+
char * s = RSTRING_PTR(str1);
|
15
|
+
char * t = RSTRING_PTR(str2);
|
16
|
+
s1_len = RSTRING_LEN(str1);
|
17
|
+
s2_len = RSTRING_LEN(str2);
|
18
|
+
|
19
|
+
if (s1_len == 0) {
|
20
|
+
return s2_len;
|
21
|
+
} else if (s2_len == 0) {
|
22
|
+
return s1_len;
|
23
|
+
}
|
24
|
+
|
25
|
+
// We need one extra col and row for the matrix for starting values
|
26
|
+
s1_len++;
|
27
|
+
s2_len++;
|
28
|
+
|
29
|
+
d = malloc(sizeof(int) * (s1_len) * (s2_len));
|
30
|
+
|
31
|
+
for (i = 0; i < s1_len; i++) {
|
32
|
+
d[i] = i; // d[i, 0] = i
|
33
|
+
}
|
34
|
+
for (j = 0; j < s2_len; j++) {
|
35
|
+
d[j*s1_len] = j; // d[0, j] = j
|
36
|
+
}
|
37
|
+
|
38
|
+
for (i = 1; i < s1_len; i++) {
|
39
|
+
for (j = 1; j < s2_len; j++) {
|
40
|
+
if (s[i-1] == t[j-1]) {
|
41
|
+
d[j * s1_len + i] = d[(j-1) * s1_len + (i-1)];
|
42
|
+
} else {
|
43
|
+
d[j * s1_len + i] = get_min(
|
44
|
+
d[j * s1_len + (i-1)],
|
45
|
+
d[(j-1) * s1_len + i],
|
46
|
+
d[(j-1) * s1_len + (i-1)]
|
47
|
+
) + 1;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
}
|
51
|
+
i = d[s1_len * s2_len -1];
|
52
|
+
free(d);
|
53
|
+
return i;
|
54
|
+
}
|
55
|
+
|
56
|
+
static VALUE lev_dist(VALUE self, VALUE str1, VALUE str2) {
|
57
|
+
return INT2FIX(levenshtein_distance( str1, str2 ));
|
58
|
+
}
|
59
|
+
|
60
|
+
static VALUE namespace;
|
61
|
+
static VALUE mAlgorithms;
|
62
|
+
static VALUE mString;
|
63
|
+
|
64
|
+
void Init_CString() {
|
65
|
+
namespace = rb_define_module("Algorithms");
|
66
|
+
mAlgorithms = rb_define_module_under(namespace,"Algorithms");
|
67
|
+
mString = rb_define_module_under(mAlgorithms, "String");
|
68
|
+
rb_define_singleton_method(mString, "levenshtein_dist", lev_dist, 2);
|
69
|
+
}
|
70
|
+
|