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