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.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/test +8 -0
- data/ext/figure_set/and.c +94 -87
- data/ext/figure_set/array.c +59 -66
- data/ext/figure_set/figure_set.h +14 -63
- data/ext/figure_set/index.c +87 -75
- data/ext/figure_set/init.c +114 -104
- data/ext/figure_set/methods.c +29 -14
- data/ext/figure_set/or.c +78 -69
- data/ext/figure_set/sample.c +58 -55
- data/lib/figure_set/version.rb +1 -1
- metadata +5 -2
data/ext/figure_set/methods.c
CHANGED
@@ -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
|
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
|
36
|
+
static VALUE
|
37
|
+
t_initialize(int argc, VALUE *argv, VALUE self)
|
34
38
|
{
|
35
|
-
VALUE
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
19
|
+
void
|
20
|
+
join(root_node result_set, root_node set0, root_node set1)
|
15
21
|
{
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
48
|
+
static branch_node
|
49
|
+
init_and_join_brance_node(root_node result_set, branch_node set0, branch_node set1)
|
44
50
|
{
|
45
|
-
|
51
|
+
branch_node branch;
|
46
52
|
|
47
|
-
|
53
|
+
branch = (branch_node)init_branch_node();
|
48
54
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
-
|
62
|
+
return branch;
|
57
63
|
}
|
58
64
|
|
59
|
-
void
|
65
|
+
static void
|
66
|
+
middle_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
|
60
67
|
{
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
84
|
+
static void
|
85
|
+
last_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
|
78
86
|
{
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
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
|
-
|
103
|
+
static leaf_node
|
104
|
+
init_and_join_leaf_node(root_node result_set, leaf_node set0, leaf_node set1)
|
96
105
|
{
|
97
|
-
|
106
|
+
leaf_node leaf;
|
98
107
|
|
99
|
-
|
108
|
+
leaf = (leaf_node)init_leaf_node(set0->offset);
|
100
109
|
|
101
|
-
|
102
|
-
|
110
|
+
leaf->data = set0->data | set1->data;
|
111
|
+
result_set->size += BIT_COUNT(leaf->data);
|
103
112
|
|
104
|
-
|
113
|
+
return leaf;
|
105
114
|
}
|
data/ext/figure_set/sample.c
CHANGED
@@ -9,79 +9,82 @@
|
|
9
9
|
#include <ruby.h>
|
10
10
|
#include "figure_set.h"
|
11
11
|
|
12
|
-
|
13
|
-
|
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
|
-
|
15
|
+
unsigned long i = 0, hit_count = 0;
|
16
|
+
unsigned long bit_count, target_point;
|
17
|
+
unsigned long x;
|
18
18
|
|
19
|
-
|
19
|
+
x = leaf->data;
|
20
|
+
bit_count = BIT_COUNT(leaf->data);
|
21
|
+
if (!bit_count) return;
|
20
22
|
|
21
|
-
|
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
|
-
|
38
|
-
if (
|
39
|
-
|
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
|
37
|
+
static void
|
38
|
+
search_and_sample_array(branch_node branch, VALUE array)
|
44
39
|
{
|
45
|
-
|
40
|
+
unsigned int i, count, target_point;
|
46
41
|
|
47
|
-
|
42
|
+
if (!branch->children_size) return;
|
48
43
|
|
49
|
-
|
44
|
+
target_point = rand() % branch->children_size;
|
50
45
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
72
|
-
bit_count = BIT_COUNT(leaf->data);
|
73
|
-
if (!bit_count) return;
|
68
|
+
start_sample = (unsigned int)RARRAY_LEN(array);
|
74
69
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
85
|
-
|
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
|
}
|
data/lib/figure_set/version.rb
CHANGED