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/index.c
CHANGED
@@ -8,111 +8,123 @@
|
|
8
8
|
#include <ruby.h>
|
9
9
|
#include "figure_set.h"
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
{
|
16
|
-
unsigned long quotient, remainder;
|
11
|
+
static unsigned int search_and_insert(branch_node, unsigned long, unsigned long, unsigned long);
|
12
|
+
static unsigned int search_and_insert_at_leaf(leaf_node, unsigned long);
|
13
|
+
static unsigned int search_and_remove(branch_node, unsigned long, unsigned long, unsigned long);
|
14
|
+
static unsigned int search_and_remove_at_leaf(leaf_node, unsigned long);
|
17
15
|
|
18
|
-
quotient = value / OFFSET_SCALE[0];
|
19
|
-
remainder = value % OFFSET_SCALE[0];
|
20
16
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
17
|
+
static unsigned int
|
18
|
+
search_and_insert(branch_node branch, unsigned long level, unsigned long value, unsigned long original)
|
19
|
+
{
|
20
|
+
unsigned long quotient, remainder;
|
21
|
+
|
22
|
+
quotient = value / OFFSET_SCALE[level];
|
23
|
+
remainder = value % OFFSET_SCALE[level];
|
25
24
|
|
26
|
-
|
27
|
-
|
25
|
+
if(!(branch->index[quotient])) {
|
26
|
+
if(branch->children_type == CT_LEAF) {
|
27
|
+
branch->index[quotient] = init_leaf_node((unsigned long)(original / ULONG_BIT_COUNT) * ULONG_BIT_COUNT);
|
28
|
+
} else {
|
29
|
+
branch->index[quotient] = init_branch_node();
|
30
|
+
if ((level + 1) == LAST_BRANCH_LEVEL) {
|
31
|
+
((branch_node)(branch->index[quotient]))->children_type = CT_LEAF;
|
32
|
+
}
|
28
33
|
}
|
34
|
+
branch->children_size++;
|
35
|
+
}
|
36
|
+
|
37
|
+
if (branch->children_type == CT_LEAF) {
|
38
|
+
return search_and_insert_at_leaf((leaf_node)branch->index[quotient], remainder);
|
39
|
+
} else {
|
40
|
+
return search_and_insert((branch_node)branch->index[quotient], level + 1, remainder, original);
|
41
|
+
}
|
29
42
|
}
|
30
43
|
|
31
|
-
unsigned int
|
44
|
+
static unsigned int
|
45
|
+
search_and_insert_at_leaf(leaf_node leaf, unsigned long value)
|
32
46
|
{
|
33
|
-
|
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
|
-
}
|
47
|
+
unsigned int exist_flag = 0;
|
48
|
+
unsigned long target_bit;
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
target_bit = 1UL << value;
|
51
|
+
|
52
|
+
if (!(leaf->data & target_bit)) {
|
53
|
+
leaf->data |= target_bit;
|
54
|
+
exist_flag = 1;
|
55
|
+
}
|
56
|
+
|
57
|
+
return exist_flag;
|
55
58
|
}
|
56
59
|
|
57
|
-
unsigned int
|
60
|
+
static unsigned int
|
61
|
+
search_and_remove(branch_node branch, unsigned long level, unsigned long value, unsigned long original)
|
58
62
|
{
|
59
|
-
|
60
|
-
unsigned long target_bit;
|
63
|
+
unsigned long quotient, remainder;
|
61
64
|
|
62
|
-
|
65
|
+
quotient = value / OFFSET_SCALE[level];
|
66
|
+
remainder = value % OFFSET_SCALE[level];
|
63
67
|
|
64
|
-
|
65
|
-
leaf->data |= target_bit;
|
66
|
-
exist_flag = 1;
|
67
|
-
}
|
68
|
+
if (!(branch->index[quotient])) return 0;
|
68
69
|
|
69
|
-
|
70
|
+
if (branch->children_type == CT_LEAF) {
|
71
|
+
return search_and_remove_at_leaf((leaf_node)branch->index[quotient], remainder);
|
72
|
+
} else {
|
73
|
+
return search_and_remove((branch_node)branch->index[quotient], level + 1, remainder, original);
|
74
|
+
}
|
70
75
|
}
|
71
76
|
|
72
|
-
|
73
|
-
|
74
|
-
//
|
75
|
-
void delete_num(root_node root, unsigned long value)
|
77
|
+
static unsigned int
|
78
|
+
search_and_remove_at_leaf(leaf_node leaf, unsigned long value)
|
76
79
|
{
|
77
|
-
|
80
|
+
unsigned int exist_flag = 0;
|
81
|
+
unsigned long target_bit;
|
78
82
|
|
79
|
-
|
80
|
-
remainder = value % OFFSET_SCALE[0];
|
83
|
+
target_bit = 1UL << value;
|
81
84
|
|
82
|
-
|
85
|
+
if ((leaf->data & target_bit)) {
|
86
|
+
leaf->data ^= target_bit;
|
87
|
+
exist_flag = 1;
|
88
|
+
}
|
83
89
|
|
84
|
-
|
85
|
-
root->size--;
|
86
|
-
}
|
90
|
+
return exist_flag;
|
87
91
|
}
|
88
92
|
|
89
|
-
|
93
|
+
//
|
94
|
+
// insert element into set
|
95
|
+
//
|
96
|
+
void
|
97
|
+
add_num(root_node root, unsigned long value)
|
90
98
|
{
|
91
|
-
|
99
|
+
unsigned long quotient, remainder;
|
92
100
|
|
93
|
-
|
94
|
-
|
101
|
+
quotient = value / OFFSET_SCALE[0];
|
102
|
+
remainder = value % OFFSET_SCALE[0];
|
95
103
|
|
96
|
-
|
104
|
+
if(!(root->index[quotient])) {
|
105
|
+
root->index[quotient] = init_branch_node();
|
106
|
+
root->children_size++;
|
107
|
+
}
|
97
108
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
return search_and_remove((branch_node)branch->index[quotient], level + 1, remainder, original);
|
102
|
-
}
|
109
|
+
if (search_and_insert((branch_node)root->index[quotient], 1, remainder, value)) {
|
110
|
+
root->size++;
|
111
|
+
}
|
103
112
|
}
|
104
113
|
|
105
|
-
|
114
|
+
//
|
115
|
+
// remove element from set
|
116
|
+
//
|
117
|
+
void
|
118
|
+
delete_num(root_node root, unsigned long value)
|
106
119
|
{
|
107
|
-
|
108
|
-
unsigned long target_bit;
|
120
|
+
unsigned long quotient, remainder;
|
109
121
|
|
110
|
-
|
122
|
+
quotient = value / OFFSET_SCALE[0];
|
123
|
+
remainder = value % OFFSET_SCALE[0];
|
111
124
|
|
112
|
-
|
113
|
-
leaf->data ^= target_bit;
|
114
|
-
exist_flag = 1;
|
115
|
-
}
|
125
|
+
if (!(root->index[quotient])) return;
|
116
126
|
|
117
|
-
|
127
|
+
if (search_and_remove((branch_node)root->index[quotient], 1, remainder, value)) {
|
128
|
+
root->size--;
|
129
|
+
}
|
118
130
|
}
|
data/ext/figure_set/init.c
CHANGED
@@ -8,160 +8,170 @@
|
|
8
8
|
#include <ruby.h>
|
9
9
|
#include "figure_set.h"
|
10
10
|
|
11
|
+
static void
|
12
|
+
copy_branch_node(root_node root, branch_node branch, branch_node orig)
|
13
|
+
{
|
14
|
+
unsigned int i, count;
|
15
|
+
|
16
|
+
branch->children_type = orig->children_type;
|
17
|
+
branch->children_size = orig->children_size;
|
18
|
+
|
19
|
+
if (orig->children_type == CT_LEAF) {
|
20
|
+
for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < orig->children_size; i++) {
|
21
|
+
if (orig->index[i]) {
|
22
|
+
branch->index[i] = init_and_copy_leaf_node(root, (leaf_node)orig->index[i]);
|
23
|
+
count++;
|
24
|
+
}
|
25
|
+
}
|
26
|
+
} else {
|
27
|
+
for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < orig->children_size; i++) {
|
28
|
+
if (orig->index[i]) {
|
29
|
+
branch->index[i] = init_and_copy_brance_node(root, (branch_node)orig->index[i]);
|
30
|
+
count++;
|
31
|
+
}
|
32
|
+
}
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
11
36
|
//
|
12
37
|
// initialize
|
13
38
|
//
|
14
|
-
void
|
39
|
+
void
|
40
|
+
init_root_node(root_node root)
|
15
41
|
{
|
16
|
-
|
42
|
+
unsigned int i;
|
17
43
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
44
|
+
for(i = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE; i++) {
|
45
|
+
root->index[i] = (void*)NULL;
|
46
|
+
}
|
47
|
+
root->size = 0;
|
48
|
+
root->children_size = 0;
|
23
49
|
}
|
24
50
|
|
25
|
-
|
51
|
+
branch_node
|
52
|
+
init_branch_node()
|
26
53
|
{
|
27
|
-
|
28
|
-
|
54
|
+
unsigned int i;
|
55
|
+
branch_node branch;
|
29
56
|
|
30
|
-
|
31
|
-
|
32
|
-
|
57
|
+
if (!(branch = (branch_node)malloc(sizeof(struct _branch_node)))) {
|
58
|
+
rb_raise(rb_eStandardError, "memory is not enough");
|
59
|
+
}
|
33
60
|
|
34
|
-
|
35
|
-
|
36
|
-
|
61
|
+
for(i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
|
62
|
+
branch->index[i] = (branch_node)NULL;
|
63
|
+
}
|
37
64
|
|
38
|
-
|
39
|
-
|
65
|
+
branch->children_size = 0;
|
66
|
+
branch->children_type = CT_BRANCH;
|
40
67
|
|
41
|
-
|
68
|
+
return branch;
|
42
69
|
}
|
43
70
|
|
44
|
-
|
71
|
+
leaf_node
|
72
|
+
init_leaf_node(unsigned long offset)
|
45
73
|
{
|
46
|
-
|
74
|
+
leaf_node leaf;
|
47
75
|
|
48
|
-
|
49
|
-
|
50
|
-
|
76
|
+
if(!(leaf = (leaf_node)malloc(sizeof(struct _leaf_node)))) {
|
77
|
+
rb_raise(rb_eStandardError, "memory is not enough");
|
78
|
+
}
|
51
79
|
|
52
|
-
|
53
|
-
|
80
|
+
leaf->offset = offset;
|
81
|
+
leaf->data = 0;
|
54
82
|
|
55
|
-
|
83
|
+
return leaf;
|
56
84
|
}
|
57
85
|
|
58
86
|
//
|
59
87
|
// initialize copy
|
60
88
|
//
|
61
|
-
void
|
89
|
+
void
|
90
|
+
copy_root_node(root_node root, root_node orig)
|
62
91
|
{
|
63
|
-
|
92
|
+
unsigned int i, count;
|
64
93
|
|
65
|
-
|
94
|
+
root->children_size = orig->children_size;
|
66
95
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
+
for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < orig->children_size; i++) {
|
97
|
+
if (orig->index[i]) {
|
98
|
+
root->index[i] = init_and_copy_brance_node(root, (branch_node)orig->index[i]);
|
99
|
+
count++;
|
96
100
|
}
|
101
|
+
}
|
97
102
|
}
|
98
103
|
|
99
|
-
|
104
|
+
branch_node
|
105
|
+
init_and_copy_brance_node(root_node root, branch_node orig)
|
100
106
|
{
|
101
|
-
|
107
|
+
branch_node branch;
|
102
108
|
|
103
|
-
|
104
|
-
|
109
|
+
branch = (branch_node)init_branch_node();
|
110
|
+
copy_branch_node(root, branch, orig);
|
105
111
|
|
106
|
-
|
112
|
+
return branch;
|
107
113
|
}
|
108
114
|
|
109
|
-
|
115
|
+
leaf_node
|
116
|
+
init_and_copy_leaf_node(root_node root, leaf_node orig)
|
110
117
|
{
|
111
|
-
|
118
|
+
leaf_node leaf;
|
112
119
|
|
113
|
-
|
114
|
-
|
115
|
-
|
120
|
+
leaf = (leaf_node)init_leaf_node(orig->offset);
|
121
|
+
leaf->data = orig->data;
|
122
|
+
root->size += BIT_COUNT(leaf->data);
|
116
123
|
|
117
|
-
|
124
|
+
return leaf;
|
118
125
|
}
|
119
126
|
|
120
127
|
//
|
121
128
|
// memory free
|
122
129
|
//
|
123
|
-
void
|
130
|
+
void
|
131
|
+
destroy_all(root_node root)
|
124
132
|
{
|
125
|
-
|
126
|
-
|
133
|
+
destroy_all_branches(root);
|
134
|
+
free(root);
|
127
135
|
}
|
128
136
|
|
129
|
-
void
|
137
|
+
void
|
138
|
+
destroy_all_branches(root_node root)
|
130
139
|
{
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
}
|
140
|
+
unsigned int i, count;
|
141
|
+
|
142
|
+
for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < root->children_size; i++) {
|
143
|
+
if (root->index[i]) {
|
144
|
+
destroy_branch((branch_node)root->index[i]);
|
145
|
+
root->index[i] = (void*)NULL;
|
146
|
+
count++;
|
139
147
|
}
|
148
|
+
}
|
140
149
|
|
141
|
-
|
142
|
-
|
150
|
+
root->size = 0;
|
151
|
+
root->children_size = 0;
|
143
152
|
}
|
144
153
|
|
145
|
-
void
|
154
|
+
void
|
155
|
+
destroy_branch(branch_node branch)
|
146
156
|
{
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
}
|
157
|
+
unsigned int i, count;
|
158
|
+
|
159
|
+
if (branch->children_type == CT_LEAF) {
|
160
|
+
for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
|
161
|
+
if (branch->index[i]) {
|
162
|
+
free((leaf_node)branch->index[i]);
|
163
|
+
branch->index[i] = (void*)NULL;
|
164
|
+
count++;
|
165
|
+
}
|
166
|
+
}
|
167
|
+
} else {
|
168
|
+
for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
|
169
|
+
if (branch->index[i]) {
|
170
|
+
destroy_branch((branch_node)branch->index[i]);
|
171
|
+
branch->index[i] = (void*)NULL;
|
172
|
+
count++;
|
173
|
+
}
|
165
174
|
}
|
166
|
-
|
175
|
+
}
|
176
|
+
free(branch);
|
167
177
|
}
|