smoothsort 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 35042a1158529cf10ea812c8a1801ec4a9ba42b5
4
+ data.tar.gz: 9ae819dcbd21a2f1e0166657ec0a99967104bef9
5
+ SHA512:
6
+ metadata.gz: 81a516b0bcc10e27d8ba8e64c31bede0d16cd937e8fa16dbaeda26715d4843809b6b39521c4f6f980745324151163217790a13c2325691055068911b3433ef57
7
+ data.tar.gz: f16654c696139220626b69d48e3a5cb3b41d968ded5feef61a5103ad4fbdd076f5e02d3b2e3c5b57e00c1ad5b533586b9baf17fc922f389f8c877f4e1de55886
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Katherine Whitlock
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = smoothsort
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to smoothsort
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Katherine Whitlock. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('smoothsort/smoothsort')
@@ -0,0 +1,170 @@
1
+ #include <ruby-2.0.0/ruby.h>
2
+
3
+ /* Adapted from Delphi implementation of Dijkstra's algorithm.
4
+ Second argument to smoothsort added. Functions IsAscending, UP, DOWN
5
+ replaced by macros.
6
+ */
7
+
8
+ /* Type of items to be sorted; for numeric types, replace by int, float or double */
9
+
10
+ /* Comparison function; for numeric types, use (A <= B) */
11
+
12
+ #define IsAscending(A,B) (RTEST(rb_funcall(A, rb_intern("<="), 1, B)))
13
+
14
+ #define UP(IA,IB) temp=IA; IA+=IB+1; IB=temp
15
+ #define DOWN(IA,IB) temp=IB; IB=IA-IB-1; IA=temp
16
+
17
+ static int q, r,
18
+ p, b, c,
19
+ r1, b1, c1;
20
+ static VALUE A;
21
+
22
+ static inline void sift(){
23
+ int r0, r2, temp;
24
+ VALUE T;
25
+ r0 = r1;
26
+ T = rb_ary_entry(A, r0);
27
+ while (b1 >= 3) {
28
+ r2 = r1 - b1 + c1;
29
+ if (!IsAscending(rb_ary_entry(A, r1 - 1), rb_ary_entry(A, r2))) { // A[r1 - 1], A[r2]
30
+ r2 = r1 - 1;
31
+ DOWN(b1,c1);
32
+ }
33
+ if (IsAscending(rb_ary_entry(A, r2), T)) // A[r2]
34
+ b1 = 1;
35
+ else {
36
+ rb_ary_store(A, r1, rb_ary_entry(A, r2)); // A[r1] = A[r2];
37
+ r1 = r2;
38
+ DOWN(b1,c1);
39
+ }
40
+ }
41
+ if (r1 - r0)
42
+ rb_ary_store(A, r1, T); // A[r1] = T;
43
+ }
44
+
45
+ static inline void trinkle(void){
46
+ int p1,r2,r3, r0, temp;
47
+ VALUE T;
48
+ p1 = p; b1 = b; c1 = c;
49
+ r0 = r1;
50
+ T = rb_ary_entry(A, r0); //A[r0]
51
+ while (p1 > 0) {
52
+ while ((p1 & 1) == 0) {
53
+ p1 >>= 1;
54
+ UP(b1, c1);
55
+ }
56
+ r3 = r1 - b1;
57
+ if ((p1 == 1) || IsAscending(rb_ary_entry(A, r3), T)) //A[r3]
58
+ p1 = 0;
59
+ else{
60
+ p1--;
61
+ if (b1 == 1) {
62
+ rb_ary_store(A, r1, rb_ary_entry(A, r3)); //A[r1] = A[r3];
63
+ r1 = r3;
64
+ } else if (b1 >= 3) {
65
+ r2 = r1 - b1 + c1;
66
+ if (! IsAscending(rb_ary_entry(A, r1 - 1), rb_ary_entry(A, r2))) { // A[r1 - 1], A[r2]
67
+ r2 = r1 - 1;
68
+ DOWN(b1, c1);
69
+ p1 <<= 1;
70
+ }
71
+ if (IsAscending(rb_ary_entry(A, r2), rb_ary_entry(A, r3))) { // A[r2], A[r3]
72
+ rb_ary_store(A, r1, rb_ary_entry(A, r3)); //A[r1] = A[r3];
73
+ r1 = r3;
74
+ } else {
75
+ rb_ary_store(A, r1, rb_ary_entry(A, r2)); //A[r1] = A[r2];
76
+ r1 = r2;
77
+ DOWN(b1, c1);
78
+ p1 = 0;
79
+ }
80
+ }
81
+ }
82
+ }
83
+ if (r0 - r1)
84
+ rb_ary_store(A, r1, T); //A[r1] = T;
85
+ sift();
86
+ }
87
+
88
+ static inline void semitrinkle(void){
89
+ VALUE T;
90
+ r1 = r - c;
91
+ if (! IsAscending(rb_ary_entry(A, r1), rb_ary_entry(A, r))) { //A[r1], A[r]
92
+ T = rb_ary_entry(A, r); //A[r];
93
+ rb_ary_store(A, r, rb_ary_entry(A, r1)); //A[r] = A[r1];
94
+ rb_ary_store(A, r1, T); //A[r1] = T;
95
+ trinkle();
96
+ }
97
+ }
98
+
99
+ void smoothsort(VALUE self, const int N){
100
+ int temp;
101
+ A = self; /* 0-base array; warning: A is shared by other functions */
102
+ q = 1; r = 0; p = 1; b = 1; c = 1;
103
+
104
+ /* building tree */
105
+ while (q < N) {
106
+ r1 = r;
107
+ if ((p & 7) == 3) {
108
+ b1 = b; c1 = c; sift();
109
+ p = (p + 1) >> 2;
110
+ UP(b, c);
111
+ UP(b, c);
112
+ } else if ((p & 3) == 1) {
113
+ if (q + c < N) {
114
+ b1 = b; c1 = c; sift();
115
+ } else trinkle();
116
+ DOWN(b, c);
117
+ p <<= 1;
118
+ while (b > 1) {
119
+ DOWN(b, c);
120
+ p <<= 1;
121
+ }
122
+ p++;
123
+ }
124
+ q++; r++;
125
+ }
126
+ r1 = r; trinkle();
127
+
128
+ /* building sorted array */
129
+ while (q > 1) {
130
+ q--;
131
+ if (b == 1) {
132
+ r--; p--;
133
+ while ((p & 1) == 0) {
134
+ p >>= 1;
135
+ UP(b, c);
136
+ }
137
+ } else if (b >= 3) {
138
+ p--; r = r-b+c;
139
+ if (p > 0) {
140
+ semitrinkle();
141
+ }
142
+ DOWN(b,c);
143
+ p = (p << 1) + 1;
144
+ r = r + c; semitrinkle();
145
+ DOWN(b, c);
146
+ p = (p << 1) + 1;
147
+ }
148
+ /* element q processed */
149
+ }
150
+ /* element 0 processed */
151
+ }
152
+
153
+ static VALUE smoothsort_ssort_bang(VALUE self) {
154
+ //return RTEST(rb_funcall(rb_ary_entry(self, 0), rb_intern("<="), 1, rb_ary_entry(self, 1))) ? Qtrue : Qfalse;
155
+ smoothsort(self, (int)RARRAY_LEN(self));
156
+ return self;
157
+ }
158
+
159
+ /* static VALUE smoothsort_ssort(VALUE self){ */
160
+ /* //rb_funcall(self, rb_intern("print"), 1, A); */
161
+ /* //rb_funcall(self, rb_intern("print"), 1, rb_str_new2("\n")); */
162
+ /* VALUE clone = rb_funcall(self, rb_intern("clone")) */
163
+
164
+ /* } */
165
+
166
+ void Init_smoothsort(void) {
167
+ VALUE module = rb_define_module("Enumerable");
168
+ rb_define_method(module, "ssort!", smoothsort_ssort_bang, 0);
169
+ }
170
+
@@ -0,0 +1,9 @@
1
+ require 'smoothsort/smoothsort'
2
+
3
+ module Enumerable
4
+ def ssort
5
+ a = self.clone
6
+ a.ssort!
7
+ a
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smoothsort
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Katherine Whitlock
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest-reporters
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rdoc
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: jeweler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.8.4
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.4
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: This is an implementation of Djikstra's smoothsort sorting algorithm,
98
+ usable on Enumerable objects
99
+ email: toroidalcode@gmail.com
100
+ executables: []
101
+ extensions:
102
+ - ext/smoothsort/extconf.rb
103
+ extra_rdoc_files:
104
+ - LICENSE.txt
105
+ - README.rdoc
106
+ files:
107
+ - ext/smoothsort/extconf.rb
108
+ - ext/smoothsort/smoothsort.c
109
+ - lib/smoothsort.rb
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ homepage: http://github.com/toroidal-code/smoothsort-rb
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.0.5
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: The smoothsort algorithm as a gem
136
+ test_files: []