figure_set 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,10 +13,13 @@
13
13
  // Ruby Methods
14
14
  // ----------------------------------------------------------
15
15
 
16
+ static VALUE rb_cFigureSet;
17
+
16
18
  /**
17
19
  * allocate
18
20
  **/
19
- static VALUE t_allocate(VALUE klass)
21
+ static VALUE
22
+ t_allocate(VALUE klass)
20
23
  {
21
24
  VALUE obj;
22
25
  root_node root;
@@ -30,9 +33,10 @@ static VALUE t_allocate(VALUE klass)
30
33
  /**
31
34
  * initialize
32
35
  **/
33
- static VALUE t_initialize(int argc, VALUE *argv, VALUE self)
36
+ static VALUE
37
+ t_initialize(int argc, VALUE *argv, VALUE self)
34
38
  {
35
- VALUE num, ary_element;
39
+ VALUE ary_element;
36
40
  root_node root;
37
41
  unsigned long i, len;
38
42
 
@@ -54,7 +58,8 @@ static VALUE t_initialize(int argc, VALUE *argv, VALUE self)
54
58
  /**
55
59
  * initialize_copy
56
60
  **/
57
- static VALUE t_initialize_copy(VALUE self, VALUE orig)
61
+ static VALUE
62
+ t_initialize_copy(VALUE self, VALUE orig)
58
63
  {
59
64
  root_node root, orig_set;
60
65
 
@@ -68,7 +73,8 @@ static VALUE t_initialize_copy(VALUE self, VALUE orig)
68
73
  /**
69
74
  * add
70
75
  **/
71
- static VALUE t_add(VALUE self, VALUE value)
76
+ static VALUE
77
+ t_add(VALUE self, VALUE value)
72
78
  {
73
79
  root_node root;
74
80
 
@@ -84,7 +90,8 @@ static VALUE t_add(VALUE self, VALUE value)
84
90
  /**
85
91
  * delete
86
92
  **/
87
- static VALUE t_delete(VALUE self, VALUE value)
93
+ static VALUE
94
+ t_delete(VALUE self, VALUE value)
88
95
  {
89
96
  root_node root;
90
97
 
@@ -100,7 +107,8 @@ static VALUE t_delete(VALUE self, VALUE value)
100
107
  /**
101
108
  * intersection
102
109
  **/
103
- static VALUE t_intersection(VALUE self, VALUE other)
110
+ static VALUE
111
+ t_intersection(VALUE self, VALUE other)
104
112
  {
105
113
  VALUE obj;
106
114
  root_node result_set, set0, set1;
@@ -119,7 +127,8 @@ static VALUE t_intersection(VALUE self, VALUE other)
119
127
  /**
120
128
  * union
121
129
  **/
122
- static VALUE t_union(VALUE self, VALUE other)
130
+ static VALUE
131
+ t_union(VALUE self, VALUE other)
123
132
  {
124
133
  VALUE obj;
125
134
  root_node result_set, set0, set1;
@@ -138,7 +147,8 @@ static VALUE t_union(VALUE self, VALUE other)
138
147
  /**
139
148
  * to_a
140
149
  **/
141
- static VALUE t_to_a(VALUE self)
150
+ static VALUE
151
+ t_to_a(VALUE self)
142
152
  {
143
153
  root_node root;
144
154
  VALUE array;
@@ -154,7 +164,8 @@ static VALUE t_to_a(VALUE self)
154
164
  /**
155
165
  * sample
156
166
  **/
157
- static VALUE t_sample(int argc, VALUE *argv, VALUE self)
167
+ static VALUE
168
+ t_sample(int argc, VALUE *argv, VALUE self)
158
169
  {
159
170
  root_node root;
160
171
  VALUE array;
@@ -180,7 +191,8 @@ static VALUE t_sample(int argc, VALUE *argv, VALUE self)
180
191
  /**
181
192
  * size
182
193
  **/
183
- static VALUE t_size(VALUE self)
194
+ static VALUE
195
+ t_size(VALUE self)
184
196
  {
185
197
  root_node root;
186
198
 
@@ -192,7 +204,8 @@ static VALUE t_size(VALUE self)
192
204
  /**
193
205
  * empty?
194
206
  **/
195
- static VALUE t_empty(VALUE self)
207
+ static VALUE
208
+ t_empty(VALUE self)
196
209
  {
197
210
  root_node root;
198
211
 
@@ -208,7 +221,8 @@ static VALUE t_empty(VALUE self)
208
221
  /**
209
222
  * cler
210
223
  **/
211
- static VALUE t_clear(VALUE self)
224
+ static VALUE
225
+ t_clear(VALUE self)
212
226
  {
213
227
  root_node root;
214
228
 
@@ -225,7 +239,8 @@ static VALUE t_clear(VALUE self)
225
239
  /**
226
240
  * define class
227
241
  **/
228
- void Init_figure_set(void) {
242
+ void
243
+ Init_figure_set(void) {
229
244
  rb_cFigureSet = rb_define_class("FigureSet", rb_cObject);
230
245
  rb_define_alloc_func(rb_cFigureSet, t_allocate);
231
246
  rb_define_private_method(rb_cFigureSet, "initialize", t_initialize, -1);
data/ext/figure_set/or.c CHANGED
@@ -8,98 +8,107 @@
8
8
  #include <ruby.h>
9
9
  #include "figure_set.h"
10
10
 
11
+ static leaf_node init_and_join_leaf_node(root_node, leaf_node, leaf_node);
12
+ static branch_node init_and_join_brance_node(root_node, branch_node, branch_node);
13
+ static void last_join_branch_node(root_node, branch_node, branch_node, branch_node);
14
+ static void middle_join_branch_node(root_node, branch_node, branch_node, branch_node);
15
+
11
16
  //
12
17
  // join
13
18
  //
14
- void join(root_node result_set, root_node set0, root_node set1)
19
+ void
20
+ join(root_node result_set, root_node set0, root_node set1)
15
21
  {
16
- unsigned int i;
17
-
18
- if (set0->size == 0 && set1->size == 0) {
19
- return;
20
- } else if (set0->size == 0) {
21
- copy_root_node(result_set, set1);
22
- return;
23
- } else if (set1->size == 0) {
24
- copy_root_node(result_set, set0);
25
- return;
26
- }
27
-
28
- for (i = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE; i++) {
29
- if (set0->index[i] && set1->index[i]) {
30
- result_set->index[i] = init_and_join_brance_node(result_set, (branch_node)set0->index[i], (branch_node)set1->index[i]);
31
- result_set->children_size++;
32
- } else if(set0->index[i]) {
33
- result_set->index[i] = init_and_copy_brance_node(result_set, (branch_node)set0->index[i]);
34
- result_set->children_size++;
35
- } else if (set1->index[i]) {
36
- result_set->index[i] = init_and_copy_brance_node(result_set, (branch_node)set1->index[i]);
37
- result_set->children_size++;
38
- }
22
+ unsigned int i;
23
+
24
+ if (set0->size == 0 && set1->size == 0) {
25
+ return;
26
+ } else if (set0->size == 0) {
27
+ copy_root_node(result_set, set1);
28
+ return;
29
+ } else if (set1->size == 0) {
30
+ copy_root_node(result_set, set0);
31
+ return;
32
+ }
33
+
34
+ for (i = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE; i++) {
35
+ if (set0->index[i] && set1->index[i]) {
36
+ result_set->index[i] = init_and_join_brance_node(result_set, (branch_node)set0->index[i], (branch_node)set1->index[i]);
37
+ result_set->children_size++;
38
+ } else if(set0->index[i]) {
39
+ result_set->index[i] = init_and_copy_brance_node(result_set, (branch_node)set0->index[i]);
40
+ result_set->children_size++;
41
+ } else if (set1->index[i]) {
42
+ result_set->index[i] = init_and_copy_brance_node(result_set, (branch_node)set1->index[i]);
43
+ result_set->children_size++;
39
44
  }
45
+ }
40
46
  }
41
47
 
42
-
43
- void *init_and_join_brance_node(root_node result_set, branch_node set0, branch_node set1)
48
+ static branch_node
49
+ init_and_join_brance_node(root_node result_set, branch_node set0, branch_node set1)
44
50
  {
45
- branch_node branch;
51
+ branch_node branch;
46
52
 
47
- branch = (branch_node)init_branch_node();
53
+ branch = (branch_node)init_branch_node();
48
54
 
49
- if (set0->children_type == CT_LEAF) {
50
- branch->children_type = CT_LEAF;
51
- last_join_branch_node(result_set, branch, set0, set1);
52
- } else {
53
- middle_join_branch_node(result_set, branch, set0, set1);
54
- }
55
+ if (set0->children_type == CT_LEAF) {
56
+ branch->children_type = CT_LEAF;
57
+ last_join_branch_node(result_set, branch, set0, set1);
58
+ } else {
59
+ middle_join_branch_node(result_set, branch, set0, set1);
60
+ }
55
61
 
56
- return (void*)branch;
62
+ return branch;
57
63
  }
58
64
 
59
- void middle_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
65
+ static void
66
+ middle_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
60
67
  {
61
- unsigned int i;
62
-
63
- for (i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
64
- if (set0->index[i] && set1->index[i]) {
65
- branch->index[i] = init_and_join_brance_node(result_set, (branch_node)set0->index[i], (branch_node)set1->index[i]);
66
- branch->children_size++;
67
- } else if (set0->index[i]) {
68
- branch->index[i] = init_and_copy_brance_node(result_set, (branch_node)set0->index[i]);
69
- branch->children_size++;
70
- } else if (set1->index[i]) {
71
- branch->index[i] = init_and_copy_brance_node(result_set, (branch_node)set1->index[i]);
72
- branch->children_size++;
73
- }
68
+ unsigned int i;
69
+
70
+ for (i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
71
+ if (set0->index[i] && set1->index[i]) {
72
+ branch->index[i] = init_and_join_brance_node(result_set, (branch_node)set0->index[i], (branch_node)set1->index[i]);
73
+ branch->children_size++;
74
+ } else if (set0->index[i]) {
75
+ branch->index[i] = init_and_copy_brance_node(result_set, (branch_node)set0->index[i]);
76
+ branch->children_size++;
77
+ } else if (set1->index[i]) {
78
+ branch->index[i] = init_and_copy_brance_node(result_set, (branch_node)set1->index[i]);
79
+ branch->children_size++;
74
80
  }
81
+ }
75
82
  }
76
83
 
77
- void last_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
84
+ static void
85
+ last_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
78
86
  {
79
- unsigned int i;
80
-
81
- for (i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
82
- if (set0->index[i] && set1->index[i]) {
83
- branch->index[i] = init_and_join_leaf_node(result_set, (leaf_node)set0->index[i], (leaf_node)set1->index[i]);
84
- branch->children_size++;
85
- } else if (set0->index[i]) {
86
- branch->index[i] = init_and_copy_leaf_node(result_set, (leaf_node)set0->index[i]);
87
- branch->children_size++;
88
- } else if (set1->index[i]) {
89
- branch->index[i] = init_and_copy_leaf_node(result_set, (leaf_node)set1->index[i]);
90
- branch->children_size++;
91
- }
87
+ unsigned int i;
88
+
89
+ for (i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
90
+ if (set0->index[i] && set1->index[i]) {
91
+ branch->index[i] = init_and_join_leaf_node(result_set, (leaf_node)set0->index[i], (leaf_node)set1->index[i]);
92
+ branch->children_size++;
93
+ } else if (set0->index[i]) {
94
+ branch->index[i] = init_and_copy_leaf_node(result_set, (leaf_node)set0->index[i]);
95
+ branch->children_size++;
96
+ } else if (set1->index[i]) {
97
+ branch->index[i] = init_and_copy_leaf_node(result_set, (leaf_node)set1->index[i]);
98
+ branch->children_size++;
92
99
  }
100
+ }
93
101
  }
94
102
 
95
- void *init_and_join_leaf_node(root_node result_set, leaf_node set0, leaf_node set1)
103
+ static leaf_node
104
+ init_and_join_leaf_node(root_node result_set, leaf_node set0, leaf_node set1)
96
105
  {
97
- leaf_node leaf;
106
+ leaf_node leaf;
98
107
 
99
- leaf = (leaf_node)init_leaf_node(set0->offset);
108
+ leaf = (leaf_node)init_leaf_node(set0->offset);
100
109
 
101
- leaf->data = set0->data | set1->data;
102
- result_set->size += BIT_COUNT(leaf->data);
110
+ leaf->data = set0->data | set1->data;
111
+ result_set->size += BIT_COUNT(leaf->data);
103
112
 
104
- return (void*)leaf;
113
+ return leaf;
105
114
  }
@@ -9,79 +9,82 @@
9
9
  #include <ruby.h>
10
10
  #include "figure_set.h"
11
11
 
12
- //
13
- // output Array object from sample set
14
- //
15
- void sample(root_node root, VALUE array, unsigned long sample_count)
12
+ static void
13
+ search_and_sample_array_at_leaf(leaf_node leaf, VALUE array)
16
14
  {
17
- unsigned int start_sample, sample_now, i, count, target_point;
15
+ unsigned long i = 0, hit_count = 0;
16
+ unsigned long bit_count, target_point;
17
+ unsigned long x;
18
18
 
19
- start_sample = RARRAY_LEN(array);
19
+ x = leaf->data;
20
+ bit_count = BIT_COUNT(leaf->data);
21
+ if (!bit_count) return;
20
22
 
21
- for(sample_now = start_sample; sample_now < sample_count; sample_now++) {
22
- target_point = rand() % root->children_size;
23
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < root->children_size; i++) {
24
- if (root->index[i]) {
25
- if (count == target_point) {
26
- search_and_sample_array((branch_node)root->index[i], array);
27
- }
28
- count++;
29
- }
30
- }
31
- }
32
-
33
- if (RARRAY_LEN(array) < sample_count) {
34
- sample(root, array, sample_count);
35
- }
23
+ target_point = rand() % bit_count;
36
24
 
37
- rb_funcall(array, rb_intern("uniq!"), 0);
38
- if (RARRAY_LEN(array) < sample_count) {
39
- sample(root, array, sample_count);
25
+ while(x) {
26
+ if (x & 1UL) {
27
+ if (hit_count == target_point) {
28
+ rb_ary_push(array, ULONG2NUM(leaf->offset + i));
29
+ }
30
+ hit_count++;
40
31
  }
32
+ x = x >> 1UL;
33
+ i++;
34
+ }
41
35
  }
42
36
 
43
- void search_and_sample_array(branch_node branch, VALUE array)
37
+ static void
38
+ search_and_sample_array(branch_node branch, VALUE array)
44
39
  {
45
- unsigned int i, count, target_point;
40
+ unsigned int i, count, target_point;
46
41
 
47
- if (!branch->children_size) return;
42
+ if (!branch->children_size) return;
48
43
 
49
- target_point = rand() % branch->children_size;
44
+ target_point = rand() % branch->children_size;
50
45
 
51
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
52
- if (branch->index[i]) {
53
- if (count == target_point) {
54
- if (branch->children_type == CT_LEAF) {
55
- search_and_sample_array_at_leaf((leaf_node)branch->index[i], array);
56
- } else {
57
- search_and_sample_array((branch_node)branch->index[i], array);
58
- }
59
- }
60
- count++;
46
+ for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
47
+ if (branch->index[i]) {
48
+ if (count == target_point) {
49
+ if (branch->children_type == CT_LEAF) {
50
+ search_and_sample_array_at_leaf((leaf_node)branch->index[i], array);
51
+ } else {
52
+ search_and_sample_array((branch_node)branch->index[i], array);
61
53
  }
54
+ }
55
+ count++;
62
56
  }
57
+ }
63
58
  }
64
59
 
65
- void search_and_sample_array_at_leaf(leaf_node leaf, VALUE array)
60
+ //
61
+ // output Array object from sample set
62
+ //
63
+ void
64
+ sample(root_node root, VALUE array, unsigned long sample_count)
66
65
  {
67
- unsigned long i = 0, hit_count = 0;
68
- unsigned long bit_count, target_point;
69
- unsigned long x;
66
+ unsigned int start_sample, sample_now, i, count, target_point;
70
67
 
71
- x = leaf->data;
72
- bit_count = BIT_COUNT(leaf->data);
73
- if (!bit_count) return;
68
+ start_sample = (unsigned int)RARRAY_LEN(array);
74
69
 
75
- target_point = rand() % bit_count;
76
-
77
- while(x) {
78
- if (x & 1UL) {
79
- if (hit_count == target_point) {
80
- rb_ary_push(array, ULONG2NUM(leaf->offset + i));
81
- }
82
- hit_count++;
70
+ for(sample_now = start_sample; sample_now < sample_count; sample_now++) {
71
+ target_point = rand() % root->children_size;
72
+ for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < root->children_size; i++) {
73
+ if (root->index[i]) {
74
+ if (count == target_point) {
75
+ search_and_sample_array((branch_node)root->index[i], array);
83
76
  }
84
- x = x >> 1UL;
85
- i++;
77
+ count++;
78
+ }
86
79
  }
80
+ }
81
+
82
+ if ((unsigned int)RARRAY_LEN(array) < sample_count) {
83
+ sample(root, array, sample_count);
84
+ }
85
+
86
+ rb_funcall(array, rb_intern("uniq!"), 0);
87
+ if ((unsigned int)RARRAY_LEN(array) < sample_count) {
88
+ sample(root, array, sample_count);
89
+ }
87
90
  }
@@ -1,5 +1,5 @@
1
1
  class FigureSet
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
 
4
4
  class << self
5
5
  def version