figure_set 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/ext/figure_set/and.c DELETED
@@ -1,118 +0,0 @@
1
- //************************************
2
- // and.c
3
- //************************************
4
-
5
- #include <stdio.h>
6
- #include <stdlib.h>
7
- #include <string.h>
8
- #include <ruby.h>
9
- #include "figure_set.h"
10
-
11
- //
12
- // intersection
13
- //
14
- void intersection(root_node result_set, root_node set0, root_node set1)
15
- {
16
- unsigned int i, count;
17
- root_node base, other;
18
- branch_node branch;
19
-
20
- if (set0->size == 0 || set1->size == 0) {
21
- return;
22
- } else if (set0->size > set1->size) {
23
- base = set1;
24
- other = set0;
25
- } else {
26
- base = set0;
27
- other = set1;
28
- }
29
-
30
- for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < base->children_size; i++) {
31
- if (base->index[i]) {
32
- count++;
33
- if (other->index[i]) {
34
- branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]);
35
- if (branch) {
36
- result_set->index[i] = (void*)branch;
37
- result_set->children_size++;
38
- }
39
- }
40
- }
41
- }
42
- }
43
-
44
- void *init_and_intersection_branch_node(root_node result_set, branch_node base, branch_node other)
45
- {
46
- branch_node branch;
47
-
48
- branch = (branch_node)init_branch_node();
49
-
50
- if (base->children_type == CT_LEAF) {
51
- last_intersection_branch_node(result_set, branch, base, other);
52
- } else {
53
- middel_intersection_branch_node(result_set, branch, base, other);
54
- }
55
-
56
- if (branch->children_size) {
57
- branch->children_type = base->children_type;
58
- return (void*)branch;
59
- } else {
60
- destroy_branch(branch);
61
- return (void*)NULL;
62
- }
63
- }
64
-
65
- void middel_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
66
- {
67
- unsigned int i, count;
68
- branch_node middle_branch;
69
-
70
- for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) {
71
- if (base->index[i]) {
72
- count++;
73
- if (other->index[i]) {
74
- middle_branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]);
75
- if (middle_branch) {
76
- branch->index[i] = (void*)middle_branch;
77
- branch->children_size++;
78
- }
79
- }
80
- }
81
- }
82
- }
83
-
84
- void last_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
85
- {
86
- unsigned int i, count;
87
- leaf_node leaf;
88
-
89
- for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) {
90
- if (base->index[i]) {
91
- count++;
92
- if (other->index[i]) {
93
- leaf = (leaf_node)init_and_intersection_leaf_node(result_set, (leaf_node)base->index[i], (leaf_node)other->index[i]);
94
- if (leaf) {
95
- branch->index[i] = (void*)leaf;
96
- branch->children_size++;
97
- }
98
- }
99
- }
100
- }
101
- }
102
-
103
-
104
- void *init_and_intersection_leaf_node(root_node result_set, leaf_node base, leaf_node other)
105
- {
106
- unsigned long data;
107
- leaf_node leaf;
108
-
109
- data = base->data & other->data;
110
-
111
- if (!data) return (void*)NULL;
112
-
113
- leaf = (leaf_node)init_leaf_node(base->offset);
114
- leaf->data = data;
115
- result_set->size += BIT_COUNT(leaf->data);
116
-
117
- return (void*)leaf;
118
- }
@@ -1,98 +0,0 @@
1
- //************************************
2
- // array.c
3
- //************************************
4
-
5
- #include <stdio.h>
6
- #include <stdlib.h>
7
- #include <string.h>
8
- #include <ruby.h>
9
- #include "figure_set.h"
10
-
11
- //
12
- // output Array object from internal set
13
- //
14
- void to_array(root_node root, VALUE array)
15
- {
16
- unsigned int i, count;
17
-
18
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < root->children_size; i++) {
19
- if (root->index[i]) {
20
- search_and_get_array((branch_node)root->index[i], array);
21
- count++;
22
- }
23
- }
24
- }
25
-
26
- void search_and_get_array(branch_node branch, VALUE array)
27
- {
28
- unsigned int i, count;
29
-
30
- if (branch->children_type == CT_LEAF) {
31
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
32
- if (branch->index[i]) {
33
- search_and_get_array_at_leaf((leaf_node)branch->index[i], array);
34
- count++;
35
- }
36
- }
37
- } else {
38
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
39
- if (branch->index[i]) {
40
- search_and_get_array((branch_node)branch->index[i], array);
41
- count++;
42
- }
43
- }
44
- }
45
- }
46
-
47
- void search_and_get_array_at_leaf(leaf_node leaf, VALUE array)
48
- {
49
- unsigned long i = 0;
50
- unsigned long x;
51
-
52
- x = leaf->data;
53
-
54
- while(x) {
55
- i = BIT_COUNT((x & (-x))-1);
56
- rb_ary_push(array, ULONG2NUM(leaf->offset + i));
57
- x = x ^ (1UL << i);
58
- }
59
-
60
-
61
- /*
62
- while(x) {
63
- if (x & 1UL) {
64
- rb_ary_push(array, ULONG2NUM(leaf->offset + i));
65
- }
66
- x = x >> 1UL;
67
- i++;
68
- }
69
- */
70
- }
71
-
72
-
73
- //
74
- // bit count
75
- //
76
-
77
- // 32 bit
78
- unsigned long bit32_count(unsigned long x)
79
- {
80
- x = x - ((x >> 1UL) & 0x55555555UL);
81
- x = (x & 0x33333333UL) + ((x >> 2UL) & 0x33333333UL);
82
- x = (x + (x >> 4UL)) & 0x0f0f0f0fUL;
83
- x = x + (x >> 8UL);
84
- x = x + (x >> 16UL);
85
- return x & 0x0000003FUL;
86
- }
87
-
88
- // 64 bit
89
- unsigned long bit64_count(unsigned long x)
90
- {
91
- x = x - ((x >> 1UL) & 0x5555555555555555UL);
92
- x = (x & 0x3333333333333333UL) + ((x >> 2UL) & 0x3333333333333333UL);
93
- x = (x & 0x0f0f0f0f0f0f0f0fUL) + ((x >> 4UL) & 0x0f0f0f0f0f0f0f0fUL);
94
- x = (x + (x >> 8UL)) & 0x00ff00ff00ff00ffUL;
95
- x = x + (x >> 16UL);
96
- x = x + (x >> 32UL);
97
- return x & 0x000000000000007FUL;
98
- }
@@ -1,2 +0,0 @@
1
- require "mkmf"
2
- create_makefile("figure_set")
@@ -1,118 +0,0 @@
1
- //************************************
2
- // index.c
3
- //************************************
4
-
5
- #include <stdio.h>
6
- #include <stdlib.h>
7
- #include <string.h>
8
- #include <ruby.h>
9
- #include "figure_set.h"
10
-
11
- //
12
- // insert element into set
13
- //
14
- void add_num(root_node root, unsigned long value)
15
- {
16
- unsigned long quotient, remainder;
17
-
18
- quotient = value / OFFSET_SCALE[0];
19
- remainder = value % OFFSET_SCALE[0];
20
-
21
- if(!(root->index[quotient])) {
22
- root->index[quotient] = init_branch_node();
23
- root->children_size++;
24
- }
25
-
26
- if (search_and_insert((branch_node)root->index[quotient], 1, remainder, value)) {
27
- root->size++;
28
- }
29
- }
30
-
31
- unsigned int search_and_insert(branch_node branch, unsigned long level, unsigned long value, unsigned long original)
32
- {
33
- unsigned long quotient, remainder;
34
-
35
- quotient = value / OFFSET_SCALE[level];
36
- remainder = value % OFFSET_SCALE[level];
37
-
38
- if(!(branch->index[quotient])) {
39
- if(branch->children_type == CT_LEAF) {
40
- branch->index[quotient] = init_leaf_node((unsigned long)(original / ULONG_BIT_COUNT) * ULONG_BIT_COUNT);
41
- } else {
42
- branch->index[quotient] = init_branch_node();
43
- if ((level + 1) == LAST_BRANCH_LEVEL) {
44
- ((branch_node)(branch->index[quotient]))->children_type = CT_LEAF;
45
- }
46
- }
47
- branch->children_size++;
48
- }
49
-
50
- if (branch->children_type == CT_LEAF) {
51
- return search_and_insert_at_leaf((leaf_node)branch->index[quotient], remainder);
52
- } else {
53
- return search_and_insert((branch_node)branch->index[quotient], level + 1, remainder, original);
54
- }
55
- }
56
-
57
- unsigned int search_and_insert_at_leaf(leaf_node leaf, unsigned long value)
58
- {
59
- unsigned int exist_flag = 0;
60
- unsigned long target_bit;
61
-
62
- target_bit = 1UL << value;
63
-
64
- if (!(leaf->data & target_bit)) {
65
- leaf->data |= target_bit;
66
- exist_flag = 1;
67
- }
68
-
69
- return exist_flag;
70
- }
71
-
72
- //
73
- // remove element from set
74
- //
75
- void delete_num(root_node root, unsigned long value)
76
- {
77
- unsigned long quotient, remainder;
78
-
79
- quotient = value / OFFSET_SCALE[0];
80
- remainder = value % OFFSET_SCALE[0];
81
-
82
- if (!(root->index[quotient])) return;
83
-
84
- if (search_and_remove((branch_node)root->index[quotient], 1, remainder, value)) {
85
- root->size--;
86
- }
87
- }
88
-
89
- unsigned int search_and_remove(branch_node branch, unsigned long level, unsigned long value, unsigned long original)
90
- {
91
- unsigned long quotient, remainder;
92
-
93
- quotient = value / OFFSET_SCALE[level];
94
- remainder = value % OFFSET_SCALE[level];
95
-
96
- if (!(branch->index[quotient])) return 0;
97
-
98
- if (branch->children_type == CT_LEAF) {
99
- return search_and_remove_at_leaf((leaf_node)branch->index[quotient], remainder);
100
- } else {
101
- return search_and_remove((branch_node)branch->index[quotient], level + 1, remainder, original);
102
- }
103
- }
104
-
105
- unsigned int search_and_remove_at_leaf(leaf_node leaf, unsigned long value)
106
- {
107
- unsigned int exist_flag = 0;
108
- unsigned long target_bit;
109
-
110
- target_bit = 1UL << value;
111
-
112
- if ((leaf->data & target_bit)) {
113
- leaf->data ^= target_bit;
114
- exist_flag = 1;
115
- }
116
-
117
- return exist_flag;
118
- }
@@ -1,167 +0,0 @@
1
- //************************************
2
- // init.c
3
- //************************************
4
-
5
- #include <stdio.h>
6
- #include <stdlib.h>
7
- #include <string.h>
8
- #include <ruby.h>
9
- #include "figure_set.h"
10
-
11
- //
12
- // initialize
13
- //
14
- void init_root_node(root_node root)
15
- {
16
- unsigned int i;
17
-
18
- for(i = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE; i++) {
19
- root->index[i] = (void*)NULL;
20
- }
21
- root->size = 0;
22
- root->children_size = 0;
23
- }
24
-
25
- void *init_branch_node()
26
- {
27
- unsigned int i;
28
- branch_node branch;
29
-
30
- if (!(branch = (branch_node)malloc(sizeof(struct _branch_node)))) {
31
- rb_raise(rb_eStandardError, "memory is not enough");
32
- }
33
-
34
- for(i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
35
- branch->index[i] = (void*)NULL;
36
- }
37
-
38
- branch->children_size = 0;
39
- branch->children_type = CT_BRANCH;
40
-
41
- return (void*)branch;
42
- }
43
-
44
- void *init_leaf_node(unsigned long offset)
45
- {
46
- leaf_node leaf;
47
-
48
- if(!(leaf = (leaf_node)malloc(sizeof(struct _leaf_node)))) {
49
- rb_raise(rb_eStandardError, "memory is not enough");
50
- }
51
-
52
- leaf->offset = offset;
53
- leaf->data = 0;
54
-
55
- return (void*)leaf;
56
- }
57
-
58
- //
59
- // initialize copy
60
- //
61
- void copy_root_node(root_node root, root_node orig)
62
- {
63
- unsigned int i, count;
64
-
65
- root->children_size = orig->children_size;
66
-
67
- for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < orig->children_size; i++) {
68
- if (orig->index[i]) {
69
- root->index[i] = init_and_copy_brance_node(root, (branch_node)orig->index[i]);
70
- count++;
71
- }
72
- }
73
- }
74
-
75
- void copy_branch_node(root_node root, branch_node branch, branch_node orig)
76
- {
77
- unsigned int i, count;
78
-
79
- branch->children_type = orig->children_type;
80
- branch->children_size = orig->children_size;
81
-
82
- if (orig->children_type == CT_LEAF) {
83
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < orig->children_size; i++) {
84
- if (orig->index[i]) {
85
- branch->index[i] = init_and_copy_leaf_node(root, (leaf_node)orig->index[i]);
86
- count++;
87
- }
88
- }
89
- } else {
90
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < orig->children_size; i++) {
91
- if (orig->index[i]) {
92
- branch->index[i] = init_and_copy_brance_node(root, (branch_node)orig->index[i]);
93
- count++;
94
- }
95
- }
96
- }
97
- }
98
-
99
- void *init_and_copy_brance_node(root_node root, branch_node orig)
100
- {
101
- branch_node branch;
102
-
103
- branch = (branch_node)init_branch_node();
104
- copy_branch_node(root, branch, orig);
105
-
106
- return (void*)branch;
107
- }
108
-
109
- void *init_and_copy_leaf_node(root_node root, leaf_node orig)
110
- {
111
- leaf_node leaf;
112
-
113
- leaf = (leaf_node)init_leaf_node(orig->offset);
114
- leaf->data = orig->data;
115
- root->size += BIT_COUNT(leaf->data);
116
-
117
- return (void*)leaf;
118
- }
119
-
120
- //
121
- // memory free
122
- //
123
- void destroy_all(root_node root)
124
- {
125
- destroy_all_branches(root);
126
- free(root);
127
- }
128
-
129
- void destroy_all_branches(root_node root)
130
- {
131
- unsigned int i, count;
132
-
133
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < root->children_size; i++) {
134
- if (root->index[i]) {
135
- destroy_branch((branch_node)root->index[i]);
136
- root->index[i] = (void*)NULL;
137
- count++;
138
- }
139
- }
140
-
141
- root->size = 0;
142
- root->children_size = 0;
143
- }
144
-
145
- void destroy_branch(branch_node branch)
146
- {
147
- unsigned int i, count;
148
-
149
- if (branch->children_type == CT_LEAF) {
150
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
151
- if (branch->index[i]) {
152
- free((leaf_node)branch->index[i]);
153
- branch->index[i] = (void*)NULL;
154
- count++;
155
- }
156
- }
157
- } else {
158
- for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
159
- if (branch->index[i]) {
160
- destroy_branch((branch_node)branch->index[i]);
161
- branch->index[i] = (void*)NULL;
162
- count++;
163
- }
164
- }
165
- }
166
- free(branch);
167
- }
data/ext/figure_set/or.c DELETED
@@ -1,105 +0,0 @@
1
- //************************************
2
- // or.c
3
- //************************************
4
-
5
- #include <stdio.h>
6
- #include <stdlib.h>
7
- #include <string.h>
8
- #include <ruby.h>
9
- #include "figure_set.h"
10
-
11
- //
12
- // join
13
- //
14
- void join(root_node result_set, root_node set0, root_node set1)
15
- {
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
- }
39
- }
40
- }
41
-
42
-
43
- void *init_and_join_brance_node(root_node result_set, branch_node set0, branch_node set1)
44
- {
45
- branch_node branch;
46
-
47
- branch = (branch_node)init_branch_node();
48
-
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
-
56
- return (void*)branch;
57
- }
58
-
59
- void middle_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
60
- {
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
- }
74
- }
75
- }
76
-
77
- void last_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
78
- {
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
- }
92
- }
93
- }
94
-
95
- void *init_and_join_leaf_node(root_node result_set, leaf_node set0, leaf_node set1)
96
- {
97
- leaf_node leaf;
98
-
99
- leaf = (leaf_node)init_leaf_node(set0->offset);
100
-
101
- leaf->data = set0->data | set1->data;
102
- result_set->size += BIT_COUNT(leaf->data);
103
-
104
- return (void*)leaf;
105
- }
@@ -1,87 +0,0 @@
1
- //************************************
2
- // sample.c
3
- //************************************
4
-
5
- #include <stdio.h>
6
- #include <stdlib.h>
7
- #include <string.h>
8
- #include <time.h>
9
- #include <ruby.h>
10
- #include "figure_set.h"
11
-
12
- //
13
- // output Array object from sample set
14
- //
15
- void sample(root_node root, VALUE array, unsigned long sample_count)
16
- {
17
- unsigned int start_sample, sample_now, i, count, target_point;
18
-
19
- start_sample = RARRAY_LEN(array);
20
-
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
- }
36
-
37
- rb_funcall(array, rb_intern("uniq!"), 0);
38
- if (RARRAY_LEN(array) < sample_count) {
39
- sample(root, array, sample_count);
40
- }
41
- }
42
-
43
- void search_and_sample_array(branch_node branch, VALUE array)
44
- {
45
- unsigned int i, count, target_point;
46
-
47
- if (!branch->children_size) return;
48
-
49
- target_point = rand() % branch->children_size;
50
-
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++;
61
- }
62
- }
63
- }
64
-
65
- void search_and_sample_array_at_leaf(leaf_node leaf, VALUE array)
66
- {
67
- unsigned long i = 0, hit_count = 0;
68
- unsigned long bit_count, target_point;
69
- unsigned long x;
70
-
71
- x = leaf->data;
72
- bit_count = BIT_COUNT(leaf->data);
73
- if (!bit_count) return;
74
-
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++;
83
- }
84
- x = x >> 1UL;
85
- i++;
86
- }
87
- }