stakach-algorithms 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/README.markdown +99 -97
  2. data/Rakefile +15 -27
  3. data/ext/algorithms/string/extconf.rb +4 -4
  4. data/ext/algorithms/string/string.c +68 -70
  5. data/ext/containers/bst/bst.c +249 -249
  6. data/ext/containers/bst/extconf.rb +4 -4
  7. data/ext/containers/deque/deque.c +248 -248
  8. data/ext/containers/deque/extconf.rb +4 -4
  9. data/ext/containers/rbtree_map/extconf.rb +4 -4
  10. data/ext/containers/rbtree_map/rbtree.c +500 -500
  11. data/ext/containers/splaytree_map/extconf.rb +4 -4
  12. data/ext/containers/splaytree_map/splaytree.c +421 -421
  13. data/lib/algorithms.rb +69 -69
  14. data/lib/algorithms/search.rb +85 -85
  15. data/lib/algorithms/sort.rb +242 -242
  16. data/lib/algorithms/string.rb +10 -10
  17. data/lib/algorithms/version.rb +3 -3
  18. data/lib/containers/deque.rb +176 -176
  19. data/lib/containers/heap.rb +501 -506
  20. data/lib/containers/kd_tree.rb +112 -112
  21. data/lib/containers/priority_queue.rb +116 -116
  22. data/lib/containers/queue.rb +71 -71
  23. data/lib/containers/rb_tree_map.rb +402 -402
  24. data/lib/containers/splay_tree_map.rb +273 -273
  25. data/lib/containers/stack.rb +70 -70
  26. data/lib/containers/suffix_array.rb +71 -71
  27. data/lib/containers/trie.rb +187 -187
  28. data/spec/bst_gc_mark_spec.rb +25 -0
  29. data/spec/bst_spec.rb +25 -0
  30. data/spec/deque_gc_mark_spec.rb +17 -0
  31. data/spec/deque_spec.rb +107 -0
  32. data/spec/heap_spec.rb +130 -0
  33. data/spec/helper.rb +4 -0
  34. data/spec/kd_expected_out.txt +10000 -0
  35. data/spec/kd_test_in.txt +10000 -0
  36. data/spec/kd_tree_spec.rb +33 -0
  37. data/spec/map_gc_mark_spec.rb +28 -0
  38. data/spec/priority_queue_spec.rb +74 -0
  39. data/spec/queue_spec.rb +60 -0
  40. data/spec/rb_tree_map_spec.rb +122 -0
  41. data/spec/search_spec.rb +27 -0
  42. data/spec/sort_spec.rb +26 -0
  43. data/spec/splay_tree_map_spec.rb +105 -0
  44. data/spec/stack_spec.rb +59 -0
  45. data/spec/string_spec.rb +12 -0
  46. data/spec/suffix_array_spec.rb +39 -0
  47. data/spec/trie_spec.rb +58 -0
  48. metadata +60 -4
@@ -1,4 +1,4 @@
1
- require 'mkmf'
2
- extension_name = "CBst"
3
- dir_config(extension_name)
4
- create_makefile(extension_name)
1
+ require 'mkmf'
2
+ extension_name = "CBst"
3
+ dir_config(extension_name)
4
+ create_makefile(extension_name)
@@ -1,249 +1,249 @@
1
- #include "ruby.h"
2
-
3
- #define FALSE 0
4
- #define TRUE 1
5
-
6
- typedef struct struct_deque_node {
7
- VALUE obj;
8
- struct struct_deque_node *left;
9
- struct struct_deque_node *right;
10
- } deque_node;
11
-
12
- typedef struct {
13
- unsigned int size;
14
- deque_node *front;
15
- deque_node *back;
16
- } deque;
17
-
18
- void free_nodes(deque_node *node) {
19
- deque_node *next;
20
- while(node) {
21
- next = node->right;
22
- free(node);
23
- node = next;
24
- }
25
- return;
26
- }
27
-
28
- void clear_deque(deque *a_deque) {
29
- if(a_deque->front)
30
- free_nodes(a_deque->front);
31
- a_deque->size = 0;
32
- a_deque->front = NULL;
33
- a_deque->back = NULL;
34
- return;
35
- }
36
-
37
- static deque* get_deque_from_self(VALUE self) {
38
- deque *a_deque;
39
- Data_Get_Struct(self, deque, a_deque);
40
- return a_deque;
41
- }
42
-
43
- static deque* create_deque() {
44
- deque *a_deque = ALLOC(deque);
45
- a_deque->size = 0;
46
- a_deque->front = NULL;
47
- a_deque->back = NULL;
48
- return a_deque;
49
- }
50
-
51
- static deque_node* create_node(VALUE obj) {
52
- deque_node *node = ALLOC(deque_node);
53
- node->obj = obj;
54
- node->left = NULL;
55
- node->right = NULL;
56
- return node;
57
- }
58
-
59
- static void deque_mark(void *ptr) {
60
- if (ptr) {
61
- deque *deque = ptr;
62
- deque_node *node = deque->front;
63
- while(node) {
64
- rb_gc_mark(node->obj);
65
- node = node->right;
66
- }
67
- }
68
- }
69
-
70
- static void deque_free(void *ptr) {
71
- if (ptr) {
72
- deque *deque = ptr;
73
- free_nodes(deque->front);
74
- free(deque);
75
- }
76
- }
77
-
78
- static VALUE deque_alloc(VALUE klass) {
79
- deque *deque = create_deque();
80
- return Data_Wrap_Struct(klass, deque_mark, deque_free, deque);
81
- }
82
-
83
- static VALUE deque_push_front(VALUE self, VALUE obj) {
84
- deque *deque = get_deque_from_self(self);
85
- deque_node *node = create_node(obj);
86
- if(deque->front) {
87
- node->right = deque->front;
88
- deque->front->left = node;
89
- deque->front = node;
90
- }
91
- else {
92
- deque->front = node;
93
- deque->back = node;
94
- }
95
- deque->size++;
96
- return obj;
97
- }
98
-
99
- static VALUE deque_push_back(VALUE self, VALUE obj) {
100
- deque *deque = get_deque_from_self(self);
101
- deque_node *node = create_node(obj);
102
- if(deque->back) {
103
- node->left = deque->back;
104
- deque->back->right = node;
105
- deque->back = node;
106
- }
107
- else {
108
- deque->front = node;
109
- deque->back = node;
110
- }
111
- deque->size++;
112
- return obj;
113
- }
114
-
115
- static VALUE deque_pop_front(VALUE self) {
116
- deque *deque = get_deque_from_self(self);
117
- VALUE obj;
118
- if(!deque->front)
119
- return Qnil;
120
- deque_node *node = deque->front;
121
- obj = node->obj;
122
- if(deque->size == 1) {
123
- clear_deque(deque);
124
- return obj;
125
- }
126
- deque->front->right->left = NULL;
127
- deque->front = deque->front->right;
128
- deque->size--;
129
- return obj;
130
- }
131
-
132
- static VALUE deque_front(VALUE self) {
133
- deque *deque = get_deque_from_self(self);
134
- if(deque->front)
135
- return deque->front->obj;
136
-
137
- return Qnil;
138
- }
139
-
140
- static VALUE deque_back(VALUE self) {
141
- deque *deque = get_deque_from_self(self);
142
- if(deque->back)
143
- return deque->back->obj;
144
-
145
- return Qnil;
146
- }
147
-
148
- static VALUE deque_pop_back(VALUE self) {
149
- deque *deque = get_deque_from_self(self);
150
- VALUE obj;
151
- if(!deque->back)
152
- return Qnil;
153
- deque_node *node = deque->back;
154
- obj = node->obj;
155
- if(deque->size == 1) {
156
- clear_deque(deque);
157
- return obj;
158
- }
159
- deque->back->left->right = NULL;
160
- deque->back = deque->back->left;
161
- deque->size--;
162
- return obj;
163
- }
164
-
165
- static VALUE deque_clear(VALUE self) {
166
- deque *deque = get_deque_from_self(self);
167
- clear_deque(deque);
168
- return Qnil;
169
- }
170
-
171
- static VALUE deque_size(VALUE self) {
172
- deque *deque = get_deque_from_self(self);
173
- return INT2NUM(deque->size);
174
- }
175
-
176
- static VALUE deque_is_empty(VALUE self) {
177
- deque *deque = get_deque_from_self(self);
178
- return (deque->size == 0) ? Qtrue : Qfalse;
179
- }
180
-
181
- static VALUE deque_each_forward(VALUE self) {
182
- deque *deque = get_deque_from_self(self);
183
- deque_node *node = deque->front;
184
- while(node) {
185
- rb_yield(node->obj);
186
- node = node->right;
187
- }
188
- return self;
189
- }
190
-
191
- static VALUE deque_each_backward(VALUE self) {
192
- deque *deque = get_deque_from_self(self);
193
- deque_node *node = deque->back;
194
- while(node) {
195
- rb_yield(node->obj);
196
- node = node->left;
197
- }
198
- return self;
199
- }
200
-
201
- static VALUE deque_init(int argc, VALUE *argv, VALUE self)
202
- {
203
- int len, i;
204
- VALUE ary;
205
-
206
- if(argc == 0) {
207
- return self;
208
- }
209
- else if(argc > 1) {
210
- rb_raise(rb_eArgError, "wrong number of arguments");
211
- }
212
- else {
213
- ary = rb_check_array_type(argv[0]);
214
- if(!NIL_P(ary)) {
215
- len = RARRAY_LEN(ary);
216
- for (i = 0; i < len; i++) {
217
- deque_push_back(self, RARRAY_PTR(ary)[i]);
218
- }
219
- }
220
- }
221
- return self;
222
- }
223
-
224
- static VALUE cDeque;
225
- static VALUE mContainers;
226
- static VALUE namespace;
227
-
228
- void Init_CDeque() {
229
- namespace = rb_define_module("Algorithms");
230
- mContainers = rb_define_module_under(namespace,"Containers");
231
- cDeque = rb_define_class_under(mContainers, "CDeque", rb_cObject);
232
- rb_define_alloc_func(cDeque, deque_alloc);
233
- rb_define_method(cDeque, "initialize", deque_init, -1);
234
- rb_define_method(cDeque, "push_front", deque_push_front, 1);
235
- rb_define_method(cDeque, "push_back", deque_push_back, 1);
236
- rb_define_method(cDeque, "clear", deque_clear, 0);
237
- rb_define_method(cDeque, "front", deque_front, 0);
238
- rb_define_method(cDeque, "back", deque_back, 0);
239
- rb_define_method(cDeque, "pop_front", deque_pop_front, 0);
240
- rb_define_method(cDeque, "pop_back", deque_pop_back, 0);
241
- rb_define_method(cDeque, "size", deque_size, 0);
242
- rb_define_alias(cDeque, "length", "size");
243
- rb_define_method(cDeque, "empty?", deque_is_empty, 0);
244
- rb_define_method(cDeque, "each_forward", deque_each_forward, 0);
245
- rb_define_method(cDeque, "each_backward", deque_each_backward, 0);
246
- rb_define_alias(cDeque, "each", "each_forward");
247
- rb_define_alias(cDeque, "reverse_each", "each_backward");
248
- rb_include_module(cDeque, rb_eval_string("Enumerable"));
1
+ #include "ruby.h"
2
+
3
+ #define FALSE 0
4
+ #define TRUE 1
5
+
6
+ typedef struct struct_deque_node {
7
+ VALUE obj;
8
+ struct struct_deque_node *left;
9
+ struct struct_deque_node *right;
10
+ } deque_node;
11
+
12
+ typedef struct {
13
+ unsigned int size;
14
+ deque_node *front;
15
+ deque_node *back;
16
+ } deque;
17
+
18
+ void free_nodes(deque_node *node) {
19
+ deque_node *next;
20
+ while(node) {
21
+ next = node->right;
22
+ free(node);
23
+ node = next;
24
+ }
25
+ return;
26
+ }
27
+
28
+ void clear_deque(deque *a_deque) {
29
+ if(a_deque->front)
30
+ free_nodes(a_deque->front);
31
+ a_deque->size = 0;
32
+ a_deque->front = NULL;
33
+ a_deque->back = NULL;
34
+ return;
35
+ }
36
+
37
+ static deque* get_deque_from_self(VALUE self) {
38
+ deque *a_deque;
39
+ Data_Get_Struct(self, deque, a_deque);
40
+ return a_deque;
41
+ }
42
+
43
+ static deque* create_deque() {
44
+ deque *a_deque = ALLOC(deque);
45
+ a_deque->size = 0;
46
+ a_deque->front = NULL;
47
+ a_deque->back = NULL;
48
+ return a_deque;
49
+ }
50
+
51
+ static deque_node* create_node(VALUE obj) {
52
+ deque_node *node = ALLOC(deque_node);
53
+ node->obj = obj;
54
+ node->left = NULL;
55
+ node->right = NULL;
56
+ return node;
57
+ }
58
+
59
+ static void deque_mark(void *ptr) {
60
+ if (ptr) {
61
+ deque *deque = ptr;
62
+ deque_node *node = deque->front;
63
+ while(node) {
64
+ rb_gc_mark(node->obj);
65
+ node = node->right;
66
+ }
67
+ }
68
+ }
69
+
70
+ static void deque_free(void *ptr) {
71
+ if (ptr) {
72
+ deque *deque = ptr;
73
+ free_nodes(deque->front);
74
+ free(deque);
75
+ }
76
+ }
77
+
78
+ static VALUE deque_alloc(VALUE klass) {
79
+ deque *deque = create_deque();
80
+ return Data_Wrap_Struct(klass, deque_mark, deque_free, deque);
81
+ }
82
+
83
+ static VALUE deque_push_front(VALUE self, VALUE obj) {
84
+ deque *deque = get_deque_from_self(self);
85
+ deque_node *node = create_node(obj);
86
+ if(deque->front) {
87
+ node->right = deque->front;
88
+ deque->front->left = node;
89
+ deque->front = node;
90
+ }
91
+ else {
92
+ deque->front = node;
93
+ deque->back = node;
94
+ }
95
+ deque->size++;
96
+ return obj;
97
+ }
98
+
99
+ static VALUE deque_push_back(VALUE self, VALUE obj) {
100
+ deque *deque = get_deque_from_self(self);
101
+ deque_node *node = create_node(obj);
102
+ if(deque->back) {
103
+ node->left = deque->back;
104
+ deque->back->right = node;
105
+ deque->back = node;
106
+ }
107
+ else {
108
+ deque->front = node;
109
+ deque->back = node;
110
+ }
111
+ deque->size++;
112
+ return obj;
113
+ }
114
+
115
+ static VALUE deque_pop_front(VALUE self) {
116
+ deque *deque = get_deque_from_self(self);
117
+ VALUE obj;
118
+ if(!deque->front)
119
+ return Qnil;
120
+ deque_node *node = deque->front;
121
+ obj = node->obj;
122
+ if(deque->size == 1) {
123
+ clear_deque(deque);
124
+ return obj;
125
+ }
126
+ deque->front->right->left = NULL;
127
+ deque->front = deque->front->right;
128
+ deque->size--;
129
+ return obj;
130
+ }
131
+
132
+ static VALUE deque_front(VALUE self) {
133
+ deque *deque = get_deque_from_self(self);
134
+ if(deque->front)
135
+ return deque->front->obj;
136
+
137
+ return Qnil;
138
+ }
139
+
140
+ static VALUE deque_back(VALUE self) {
141
+ deque *deque = get_deque_from_self(self);
142
+ if(deque->back)
143
+ return deque->back->obj;
144
+
145
+ return Qnil;
146
+ }
147
+
148
+ static VALUE deque_pop_back(VALUE self) {
149
+ deque *deque = get_deque_from_self(self);
150
+ VALUE obj;
151
+ if(!deque->back)
152
+ return Qnil;
153
+ deque_node *node = deque->back;
154
+ obj = node->obj;
155
+ if(deque->size == 1) {
156
+ clear_deque(deque);
157
+ return obj;
158
+ }
159
+ deque->back->left->right = NULL;
160
+ deque->back = deque->back->left;
161
+ deque->size--;
162
+ return obj;
163
+ }
164
+
165
+ static VALUE deque_clear(VALUE self) {
166
+ deque *deque = get_deque_from_self(self);
167
+ clear_deque(deque);
168
+ return Qnil;
169
+ }
170
+
171
+ static VALUE deque_size(VALUE self) {
172
+ deque *deque = get_deque_from_self(self);
173
+ return INT2NUM(deque->size);
174
+ }
175
+
176
+ static VALUE deque_is_empty(VALUE self) {
177
+ deque *deque = get_deque_from_self(self);
178
+ return (deque->size == 0) ? Qtrue : Qfalse;
179
+ }
180
+
181
+ static VALUE deque_each_forward(VALUE self) {
182
+ deque *deque = get_deque_from_self(self);
183
+ deque_node *node = deque->front;
184
+ while(node) {
185
+ rb_yield(node->obj);
186
+ node = node->right;
187
+ }
188
+ return self;
189
+ }
190
+
191
+ static VALUE deque_each_backward(VALUE self) {
192
+ deque *deque = get_deque_from_self(self);
193
+ deque_node *node = deque->back;
194
+ while(node) {
195
+ rb_yield(node->obj);
196
+ node = node->left;
197
+ }
198
+ return self;
199
+ }
200
+
201
+ static VALUE deque_init(int argc, VALUE *argv, VALUE self)
202
+ {
203
+ long len, i;
204
+ VALUE ary;
205
+
206
+ if(argc == 0) {
207
+ return self;
208
+ }
209
+ else if(argc > 1) {
210
+ rb_raise(rb_eArgError, "wrong number of arguments");
211
+ }
212
+ else {
213
+ ary = rb_check_array_type(argv[0]);
214
+ if(!NIL_P(ary)) {
215
+ len = RARRAY_LEN(ary);
216
+ for (i = 0; i < len; i++) {
217
+ deque_push_back(self, RARRAY_PTR(ary)[i]);
218
+ }
219
+ }
220
+ }
221
+ return self;
222
+ }
223
+
224
+ static VALUE cDeque;
225
+ static VALUE mContainers;
226
+ static VALUE namespace;
227
+
228
+ void Init_CDeque() {
229
+ namespace = rb_define_module("Algorithms");
230
+ mContainers = rb_define_module_under(namespace,"Containers");
231
+ cDeque = rb_define_class_under(mContainers, "CDeque", rb_cObject);
232
+ rb_define_alloc_func(cDeque, deque_alloc);
233
+ rb_define_method(cDeque, "initialize", deque_init, -1);
234
+ rb_define_method(cDeque, "push_front", deque_push_front, 1);
235
+ rb_define_method(cDeque, "push_back", deque_push_back, 1);
236
+ rb_define_method(cDeque, "clear", deque_clear, 0);
237
+ rb_define_method(cDeque, "front", deque_front, 0);
238
+ rb_define_method(cDeque, "back", deque_back, 0);
239
+ rb_define_method(cDeque, "pop_front", deque_pop_front, 0);
240
+ rb_define_method(cDeque, "pop_back", deque_pop_back, 0);
241
+ rb_define_method(cDeque, "size", deque_size, 0);
242
+ rb_define_alias(cDeque, "length", "size");
243
+ rb_define_method(cDeque, "empty?", deque_is_empty, 0);
244
+ rb_define_method(cDeque, "each_forward", deque_each_forward, 0);
245
+ rb_define_method(cDeque, "each_backward", deque_each_backward, 0);
246
+ rb_define_alias(cDeque, "each", "each_forward");
247
+ rb_define_alias(cDeque, "reverse_each", "each_backward");
248
+ rb_include_module(cDeque, rb_eval_string("Enumerable"));
249
249
  }