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.
@@ -1,249 +1,249 @@
1
- #include "ruby.h"
2
-
3
- typedef struct struct_bst_node {
4
- VALUE key;
5
- VALUE value;
6
- struct struct_bst_node *left;
7
- struct struct_bst_node *right;
8
- struct struct_bst_node *parent;
9
- } bst_node;
10
-
11
- typedef struct struct_bst {
12
- bst_node *root;
13
- int (*compare_function)(VALUE key1, VALUE key2);
14
- unsigned int size;
15
- } bst;
16
-
17
- static VALUE bst_initialize(VALUE self) {
18
- return self;
19
- }
20
-
21
- static bst* get_bst_from_self(VALUE self) {
22
- bst *tree;
23
- Data_Get_Struct(self, bst, tree);
24
- return tree;
25
- }
26
-
27
- static bst* bst_each_node(bst *tree, bst_node *node, void (*each)(bst *tree_, bst_node *node_, void* args), void* arguments) {
28
- if (!node)
29
- return NULL;
30
-
31
- if (node->left)
32
- bst_each_node(tree, node->left, each, arguments);
33
-
34
- (*each)(tree, node, arguments);
35
-
36
- if (node->right)
37
- bst_each_node(tree, node->right, each, arguments);
38
- return tree;
39
- }
40
-
41
- static bst* bst_each(bst *tree, void (*each)(bst *tree, bst_node *node, void *args), void* arguments) {
42
- if (tree->root)
43
- bst_each_node(tree, tree->root, each, arguments);
44
- return tree;
45
- }
46
-
47
- static int id_compare_operator;
48
-
49
- static int bst_compare_function(VALUE a, VALUE b) {
50
- if (a == b) return 0;
51
- if (FIXNUM_P(a) && FIXNUM_P(b)) {
52
- long x = FIX2LONG(a), y = FIX2LONG(b);
53
- if (x == y) return 0;
54
- if (x > y) return 1;
55
- return -1;
56
- }
57
- if (TYPE(a) == T_STRING && rb_obj_is_kind_of(a, rb_cString) &&
58
- TYPE(b) == T_STRING && rb_obj_is_kind_of(b, rb_cString)) {
59
- return rb_str_cmp(a, b);
60
- }
61
- return FIX2INT(rb_funcall((VALUE) a, id_compare_operator, 1, (VALUE) b));
62
- }
63
-
64
- static void insert_element(bst *tree, bst_node **t,bst_node *newElement) {
65
- int cmp;
66
- bst_node *y = NULL;
67
- bst_node *x = *t;
68
- while (x != NULL) {
69
- y = x;
70
- cmp = tree->compare_function(newElement->key, x->key);
71
- if (cmp < 0) x = x->left;
72
- else x = x->right;
73
- }
74
- newElement->parent = y;
75
- if (y == NULL) *t = newElement;
76
- else {
77
- cmp = tree->compare_function(newElement->key, y->key);
78
- if (cmp < 0)
79
- y->left = newElement;
80
- else
81
- y->right = newElement;
82
- }
83
- }
84
-
85
-
86
- static bst_node* create_node(VALUE key_value,VALUE value) {
87
- bst_node *new_node = ALLOC(bst_node);
88
- new_node->value = value;
89
- new_node->key = key_value;
90
- new_node->left = NULL;
91
- new_node->right = NULL;
92
- new_node->parent = NULL;
93
- return new_node;
94
- }
95
-
96
- static bst_node* tree_minimum (bst_node *tree) {
97
- bst_node *x = tree;
98
- while (x->left) x = x->left;
99
- return x;
100
- }
101
-
102
- static bst_node* tree_maximum (bst_node *tree) {
103
- bst_node *x = tree;
104
- while (x->right) x = x->right;
105
- return x;
106
- }
107
-
108
- static bst_node* node_successor (bst_node *tree,bst_node *x) {
109
- if (x->right) return tree_minimum(x->right);
110
- bst_node *y = x->parent;
111
- while (y && x == y->right) {
112
- x = y;
113
- y = x->parent;
114
- }
115
- return y;
116
- }
117
-
118
-
119
- static bst_node* delete_node (bst_node **tree,bst_node *tobeDeleted) {
120
- bst_node *y,*x;
121
-
122
- if ((tobeDeleted->left == NULL) || (tobeDeleted->right == NULL)) y = tobeDeleted;
123
- else y = node_successor(*tree,tobeDeleted);
124
-
125
- if (y->left) x = y->left;
126
- else x = y->right;
127
-
128
- if (x) x->parent = y->parent;
129
-
130
- if (y->parent == NULL) {
131
- *tree = x;
132
- return y;
133
- } else if (y == y->parent->left) {
134
- y->parent->left = x;
135
- } else {
136
- y->parent->right = x;
137
- }
138
-
139
- if (tobeDeleted != y) tobeDeleted->key = y->key;
140
- return y;
141
- }
142
-
143
- static bst_node* search_node(bst *tree, bst_node *node, VALUE key) {
144
- bst_node *x = node;
145
- int cmp;
146
-
147
- while(x) {
148
- cmp = tree->compare_function(key, x->key);
149
- if (cmp == 0) return x;
150
- else if (cmp < 0) { x = x->left; }
151
- else { x = x->right; }
152
- }
153
- return NULL;
154
- }
155
-
156
- static void recursively_mark_nodes(bst_node *node) {
157
- if(node) {
158
- rb_gc_mark(node->key);
159
- rb_gc_mark(node->value);
160
- recursively_mark_nodes(node->left);
161
- recursively_mark_nodes(node->right);
162
- }
163
- }
164
-
165
- static void bst_mark(bst *tree) {
166
- if (tree) {
167
- recursively_mark_nodes(tree->root);
168
- }
169
- }
170
-
171
- static void recursively_free_nodes(bst_node *node) {
172
- if(node) {
173
- recursively_free_nodes(node->left);
174
- recursively_free_nodes(node->right);
175
- free(node);
176
- }
177
- }
178
-
179
- static void bst_free(bst *tree) {
180
- if (tree) {
181
- recursively_free_nodes(tree->root);
182
- }
183
- }
184
-
185
- static bst* create_bst(int (*compare_function)(VALUE, VALUE)) {
186
- bst *tree = ALLOC(bst);
187
- tree->compare_function = compare_function;
188
- tree->root = NULL;
189
- tree->size = 0;
190
- return tree;
191
- }
192
-
193
- static VALUE bst_alloc(VALUE klass) {
194
- bst *tree = create_bst(&bst_compare_function);
195
- return Data_Wrap_Struct(klass, bst_mark, bst_free, tree);
196
- }
197
-
198
- static VALUE rb_bst_push_value(VALUE self, VALUE key, VALUE value) {
199
- bst *tree = get_bst_from_self(self);
200
- insert_element(tree, &(tree->root), create_node(key,value));
201
- tree->size++;
202
- return self;
203
- }
204
-
205
- static void bst_each_helper(bst *tree, bst_node *node, void *args) {
206
- rb_yield(rb_ary_new3(2, node->key, node->value));
207
- };
208
-
209
- static VALUE rb_bst_each(VALUE self) {
210
- bst *tree = get_bst_from_self(self);
211
- bst_each(tree, &bst_each_helper, NULL);
212
- return self;
213
- }
214
-
215
- static VALUE rb_bst_delete(VALUE self, VALUE key) {
216
- bst *tree = get_bst_from_self(self);
217
- bst_node *tobeDeleted = search_node(tree, tree->root, key);
218
- if(tobeDeleted) {
219
- tree->size -= 1;
220
- bst_node *deletedNode = delete_node(&(tree->root),tobeDeleted);
221
- return deletedNode->value;
222
- }
223
- return Qnil;
224
- }
225
-
226
- static VALUE rb_bst_size(VALUE self) {
227
- bst *tree;
228
- Data_Get_Struct(self,bst,tree);
229
- return INT2FIX(tree->size);
230
- }
231
-
232
- static VALUE CBst;
233
- static VALUE mContainers;
234
- static VALUE namespace;
235
-
236
- void Init_CBst() {
237
- id_compare_operator = rb_intern("<=>");
238
-
239
- namespace = rb_define_module("Algorithms");
240
- mContainers = rb_define_module_under(namespace,"Containers");
241
- CBst = rb_define_class_under(mContainers, "CBst", rb_cObject);
242
- rb_define_alloc_func(CBst, bst_alloc);
243
- rb_define_method(CBst, "initialize", bst_initialize, 0);
244
- rb_define_method(CBst, "push", rb_bst_push_value, 2);
245
- rb_define_alias(CBst, "[]=", "push");
246
- rb_define_method(CBst, "each", rb_bst_each, 0);
247
- rb_define_method(CBst, "delete", rb_bst_delete, 1);
248
- rb_define_method(CBst, "size", rb_bst_size, 0);
249
- }
1
+ #include "ruby.h"
2
+
3
+ typedef struct struct_bst_node {
4
+ VALUE key;
5
+ VALUE value;
6
+ struct struct_bst_node *left;
7
+ struct struct_bst_node *right;
8
+ struct struct_bst_node *parent;
9
+ } bst_node;
10
+
11
+ typedef struct struct_bst {
12
+ bst_node *root;
13
+ int (*compare_function)(VALUE key1, VALUE key2);
14
+ unsigned int size;
15
+ } bst;
16
+
17
+ static VALUE bst_initialize(VALUE self) {
18
+ return self;
19
+ }
20
+
21
+ static bst* get_bst_from_self(VALUE self) {
22
+ bst *tree;
23
+ Data_Get_Struct(self, bst, tree);
24
+ return tree;
25
+ }
26
+
27
+ static bst* bst_each_node(bst *tree, bst_node *node, void (*each)(bst *tree_, bst_node *node_, void* args), void* arguments) {
28
+ if (!node)
29
+ return NULL;
30
+
31
+ if (node->left)
32
+ bst_each_node(tree, node->left, each, arguments);
33
+
34
+ (*each)(tree, node, arguments);
35
+
36
+ if (node->right)
37
+ bst_each_node(tree, node->right, each, arguments);
38
+ return tree;
39
+ }
40
+
41
+ static bst* bst_each(bst *tree, void (*each)(bst *tree, bst_node *node, void *args), void* arguments) {
42
+ if (tree->root)
43
+ bst_each_node(tree, tree->root, each, arguments);
44
+ return tree;
45
+ }
46
+
47
+ static int id_compare_operator;
48
+
49
+ static int bst_compare_function(VALUE a, VALUE b) {
50
+ if (a == b) return 0;
51
+ if (FIXNUM_P(a) && FIXNUM_P(b)) {
52
+ long x = FIX2LONG(a), y = FIX2LONG(b);
53
+ if (x == y) return 0;
54
+ if (x > y) return 1;
55
+ return -1;
56
+ }
57
+ if (TYPE(a) == T_STRING && rb_obj_is_kind_of(a, rb_cString) &&
58
+ TYPE(b) == T_STRING && rb_obj_is_kind_of(b, rb_cString)) {
59
+ return rb_str_cmp(a, b);
60
+ }
61
+ return FIX2INT(rb_funcall((VALUE) a, id_compare_operator, 1, (VALUE) b));
62
+ }
63
+
64
+ static void insert_element(bst *tree, bst_node **t,bst_node *newElement) {
65
+ int cmp;
66
+ bst_node *y = NULL;
67
+ bst_node *x = *t;
68
+ while (x != NULL) {
69
+ y = x;
70
+ cmp = tree->compare_function(newElement->key, x->key);
71
+ if (cmp < 0) x = x->left;
72
+ else x = x->right;
73
+ }
74
+ newElement->parent = y;
75
+ if (y == NULL) *t = newElement;
76
+ else {
77
+ cmp = tree->compare_function(newElement->key, y->key);
78
+ if (cmp < 0)
79
+ y->left = newElement;
80
+ else
81
+ y->right = newElement;
82
+ }
83
+ }
84
+
85
+
86
+ static bst_node* create_node(VALUE key_value,VALUE value) {
87
+ bst_node *new_node = ALLOC(bst_node);
88
+ new_node->value = value;
89
+ new_node->key = key_value;
90
+ new_node->left = NULL;
91
+ new_node->right = NULL;
92
+ new_node->parent = NULL;
93
+ return new_node;
94
+ }
95
+
96
+ static bst_node* tree_minimum (bst_node *tree) {
97
+ bst_node *x = tree;
98
+ while (x->left) x = x->left;
99
+ return x;
100
+ }
101
+
102
+ static bst_node* tree_maximum (bst_node *tree) {
103
+ bst_node *x = tree;
104
+ while (x->right) x = x->right;
105
+ return x;
106
+ }
107
+
108
+ static bst_node* node_successor (bst_node *tree,bst_node *x) {
109
+ if (x->right) return tree_minimum(x->right);
110
+ bst_node *y = x->parent;
111
+ while (y && x == y->right) {
112
+ x = y;
113
+ y = x->parent;
114
+ }
115
+ return y;
116
+ }
117
+
118
+
119
+ static bst_node* delete_node (bst_node **tree,bst_node *tobeDeleted) {
120
+ bst_node *y,*x;
121
+
122
+ if ((tobeDeleted->left == NULL) || (tobeDeleted->right == NULL)) y = tobeDeleted;
123
+ else y = node_successor(*tree,tobeDeleted);
124
+
125
+ if (y->left) x = y->left;
126
+ else x = y->right;
127
+
128
+ if (x) x->parent = y->parent;
129
+
130
+ if (y->parent == NULL) {
131
+ *tree = x;
132
+ return y;
133
+ } else if (y == y->parent->left) {
134
+ y->parent->left = x;
135
+ } else {
136
+ y->parent->right = x;
137
+ }
138
+
139
+ if (tobeDeleted != y) tobeDeleted->key = y->key;
140
+ return y;
141
+ }
142
+
143
+ static bst_node* search_node(bst *tree, bst_node *node, VALUE key) {
144
+ bst_node *x = node;
145
+ int cmp;
146
+
147
+ while(x) {
148
+ cmp = tree->compare_function(key, x->key);
149
+ if (cmp == 0) return x;
150
+ else if (cmp < 0) { x = x->left; }
151
+ else { x = x->right; }
152
+ }
153
+ return NULL;
154
+ }
155
+
156
+ static void recursively_mark_nodes(bst_node *node) {
157
+ if(node) {
158
+ rb_gc_mark(node->key);
159
+ rb_gc_mark(node->value);
160
+ recursively_mark_nodes(node->left);
161
+ recursively_mark_nodes(node->right);
162
+ }
163
+ }
164
+
165
+ static void bst_mark(bst *tree) {
166
+ if (tree) {
167
+ recursively_mark_nodes(tree->root);
168
+ }
169
+ }
170
+
171
+ static void recursively_free_nodes(bst_node *node) {
172
+ if(node) {
173
+ recursively_free_nodes(node->left);
174
+ recursively_free_nodes(node->right);
175
+ free(node);
176
+ }
177
+ }
178
+
179
+ static void bst_free(bst *tree) {
180
+ if (tree) {
181
+ recursively_free_nodes(tree->root);
182
+ }
183
+ }
184
+
185
+ static bst* create_bst(int (*compare_function)(VALUE, VALUE)) {
186
+ bst *tree = ALLOC(bst);
187
+ tree->compare_function = compare_function;
188
+ tree->root = NULL;
189
+ tree->size = 0;
190
+ return tree;
191
+ }
192
+
193
+ static VALUE bst_alloc(VALUE klass) {
194
+ bst *tree = create_bst(&bst_compare_function);
195
+ return Data_Wrap_Struct(klass, bst_mark, bst_free, tree);
196
+ }
197
+
198
+ static VALUE rb_bst_push_value(VALUE self, VALUE key, VALUE value) {
199
+ bst *tree = get_bst_from_self(self);
200
+ insert_element(tree, &(tree->root), create_node(key,value));
201
+ tree->size++;
202
+ return self;
203
+ }
204
+
205
+ static void bst_each_helper(bst *tree, bst_node *node, void *args) {
206
+ rb_yield(rb_ary_new3(2, node->key, node->value));
207
+ };
208
+
209
+ static VALUE rb_bst_each(VALUE self) {
210
+ bst *tree = get_bst_from_self(self);
211
+ bst_each(tree, &bst_each_helper, NULL);
212
+ return self;
213
+ }
214
+
215
+ static VALUE rb_bst_delete(VALUE self, VALUE key) {
216
+ bst *tree = get_bst_from_self(self);
217
+ bst_node *tobeDeleted = search_node(tree, tree->root, key);
218
+ if(tobeDeleted) {
219
+ tree->size -= 1;
220
+ bst_node *deletedNode = delete_node(&(tree->root),tobeDeleted);
221
+ return deletedNode->value;
222
+ }
223
+ return Qnil;
224
+ }
225
+
226
+ static VALUE rb_bst_size(VALUE self) {
227
+ bst *tree;
228
+ Data_Get_Struct(self,bst,tree);
229
+ return INT2FIX(tree->size);
230
+ }
231
+
232
+ static VALUE CBst;
233
+ static VALUE mContainers;
234
+ static VALUE namespace;
235
+
236
+ void Init_CBst() {
237
+ id_compare_operator = rb_intern("<=>");
238
+
239
+ namespace = rb_define_module("Algorithms");
240
+ mContainers = rb_define_module_under(namespace,"Containers");
241
+ CBst = rb_define_class_under(mContainers, "CBst", rb_cObject);
242
+ rb_define_alloc_func(CBst, bst_alloc);
243
+ rb_define_method(CBst, "initialize", bst_initialize, 0);
244
+ rb_define_method(CBst, "push", rb_bst_push_value, 2);
245
+ rb_define_alias(CBst, "[]=", "push");
246
+ rb_define_method(CBst, "each", rb_bst_each, 0);
247
+ rb_define_method(CBst, "delete", rb_bst_delete, 1);
248
+ rb_define_method(CBst, "size", rb_bst_size, 0);
249
+ }
@@ -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)