figure_set 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
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
+ 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
+
36
+ //
37
+ // initialize
38
+ //
39
+ void
40
+ init_root_node(root_node root)
41
+ {
42
+ unsigned int i;
43
+
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;
49
+ }
50
+
51
+ branch_node
52
+ init_branch_node()
53
+ {
54
+ unsigned int i;
55
+ branch_node branch;
56
+
57
+ if (!(branch = (branch_node)malloc(sizeof(struct _branch_node)))) {
58
+ rb_raise(rb_eStandardError, "memory is not enough");
59
+ }
60
+
61
+ for(i = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH; i++) {
62
+ branch->index[i] = (branch_node)NULL;
63
+ }
64
+
65
+ branch->children_size = 0;
66
+ branch->children_type = CT_BRANCH;
67
+
68
+ return branch;
69
+ }
70
+
71
+ leaf_node
72
+ init_leaf_node(unsigned long offset)
73
+ {
74
+ leaf_node leaf;
75
+
76
+ if(!(leaf = (leaf_node)malloc(sizeof(struct _leaf_node)))) {
77
+ rb_raise(rb_eStandardError, "memory is not enough");
78
+ }
79
+
80
+ leaf->offset = offset;
81
+ leaf->data = 0;
82
+
83
+ return leaf;
84
+ }
85
+
86
+ //
87
+ // initialize copy
88
+ //
89
+ void
90
+ copy_root_node(root_node root, root_node orig)
91
+ {
92
+ unsigned int i, count;
93
+
94
+ root->children_size = orig->children_size;
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++;
100
+ }
101
+ }
102
+ }
103
+
104
+ branch_node
105
+ init_and_copy_brance_node(root_node root, branch_node orig)
106
+ {
107
+ branch_node branch;
108
+
109
+ branch = (branch_node)init_branch_node();
110
+ copy_branch_node(root, branch, orig);
111
+
112
+ return branch;
113
+ }
114
+
115
+ leaf_node
116
+ init_and_copy_leaf_node(root_node root, leaf_node orig)
117
+ {
118
+ leaf_node leaf;
119
+
120
+ leaf = (leaf_node)init_leaf_node(orig->offset);
121
+ leaf->data = orig->data;
122
+ root->size += BIT_COUNT(leaf->data);
123
+
124
+ return leaf;
125
+ }
126
+
127
+ //
128
+ // memory free
129
+ //
130
+ void
131
+ destroy_all(root_node root)
132
+ {
133
+ destroy_all_branches(root);
134
+ free(root);
135
+ }
136
+
137
+ void
138
+ destroy_all_branches(root_node root)
139
+ {
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++;
147
+ }
148
+ }
149
+
150
+ root->size = 0;
151
+ root->children_size = 0;
152
+ }
153
+
154
+ void
155
+ destroy_branch(branch_node branch)
156
+ {
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
+ }
174
+ }
175
+ }
176
+ free(branch);
177
+ }
@@ -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);
@@ -0,0 +1,114 @@
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
+ 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
+
16
+ //
17
+ // join
18
+ //
19
+ void
20
+ join(root_node result_set, root_node set0, root_node set1)
21
+ {
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++;
44
+ }
45
+ }
46
+ }
47
+
48
+ static branch_node
49
+ init_and_join_brance_node(root_node result_set, branch_node set0, branch_node set1)
50
+ {
51
+ branch_node branch;
52
+
53
+ branch = (branch_node)init_branch_node();
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
+ }
61
+
62
+ return branch;
63
+ }
64
+
65
+ static void
66
+ middle_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
67
+ {
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++;
80
+ }
81
+ }
82
+ }
83
+
84
+ static void
85
+ last_join_branch_node(root_node result_set, branch_node branch, branch_node set0, branch_node set1)
86
+ {
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++;
99
+ }
100
+ }
101
+ }
102
+
103
+ static leaf_node
104
+ init_and_join_leaf_node(root_node result_set, leaf_node set0, leaf_node set1)
105
+ {
106
+ leaf_node leaf;
107
+
108
+ leaf = (leaf_node)init_leaf_node(set0->offset);
109
+
110
+ leaf->data = set0->data | set1->data;
111
+ result_set->size += BIT_COUNT(leaf->data);
112
+
113
+ return leaf;
114
+ }
@@ -0,0 +1,90 @@
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
+ static void
13
+ search_and_sample_array_at_leaf(leaf_node leaf, VALUE array)
14
+ {
15
+ unsigned long i = 0, hit_count = 0;
16
+ unsigned long bit_count, target_point;
17
+ unsigned long x;
18
+
19
+ x = leaf->data;
20
+ bit_count = BIT_COUNT(leaf->data);
21
+ if (!bit_count) return;
22
+
23
+ target_point = rand() % bit_count;
24
+
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++;
31
+ }
32
+ x = x >> 1UL;
33
+ i++;
34
+ }
35
+ }
36
+
37
+ static void
38
+ search_and_sample_array(branch_node branch, VALUE array)
39
+ {
40
+ unsigned int i, count, target_point;
41
+
42
+ if (!branch->children_size) return;
43
+
44
+ target_point = rand() % branch->children_size;
45
+
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);
53
+ }
54
+ }
55
+ count++;
56
+ }
57
+ }
58
+ }
59
+
60
+ //
61
+ // output Array object from sample set
62
+ //
63
+ void
64
+ sample(root_node root, VALUE array, unsigned long sample_count)
65
+ {
66
+ unsigned int start_sample, sample_now, i, count, target_point;
67
+
68
+ start_sample = (unsigned int)RARRAY_LEN(array);
69
+
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);
76
+ }
77
+ count++;
78
+ }
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
+ }
90
+ }
data/figure_set.gemspec CHANGED
@@ -17,10 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
18
  spec.bindir = "exe"
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib", "ext"]
21
- spec.extensions = ["ext/figure_set/extconf.rb"]
22
-
23
- spec.required_ruby_version = '>= 2.2'
20
+ spec.require_paths = ["lib"]
21
+ spec.extensions = ["ext/figure_set/figure_set/extconf.rb"]
24
22
 
25
23
  spec.add_development_dependency "bundler", ">= 1.3.0", "< 2.0"
26
24
  spec.add_development_dependency "rake", ">= 0.8.7"
@@ -1,5 +1,5 @@
1
1
  class FigureSet
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
 
4
4
  class << self
5
5
  def version
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: figure_set
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsukasa OISHI
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-31 00:00:00.000000000 Z
11
+ date: 2023-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -91,7 +91,7 @@ email:
91
91
  - tsukasa.oishi@gmail.com
92
92
  executables: []
93
93
  extensions:
94
- - ext/figure_set/extconf.rb
94
+ - ext/figure_set/figure_set/extconf.rb
95
95
  extra_rdoc_files: []
96
96
  files:
97
97
  - ".gitignore"
@@ -104,15 +104,18 @@ files:
104
104
  - README.rdoc
105
105
  - Rakefile
106
106
  - benchmark/sample_benchmark.rb
107
- - ext/figure_set/and.c
108
- - ext/figure_set/array.c
109
- - ext/figure_set/extconf.rb
110
- - ext/figure_set/figure_set.h
111
- - ext/figure_set/index.c
112
- - ext/figure_set/init.c
113
- - ext/figure_set/methods.c
114
- - ext/figure_set/or.c
115
- - ext/figure_set/sample.c
107
+ - bin/console
108
+ - bin/setup
109
+ - bin/test
110
+ - ext/figure_set/figure_set/and.c
111
+ - ext/figure_set/figure_set/array.c
112
+ - ext/figure_set/figure_set/extconf.rb
113
+ - ext/figure_set/figure_set/figure_set.h
114
+ - ext/figure_set/figure_set/index.c
115
+ - ext/figure_set/figure_set/init.c
116
+ - ext/figure_set/figure_set/methods.c
117
+ - ext/figure_set/figure_set/or.c
118
+ - ext/figure_set/figure_set/sample.c
116
119
  - figure_set.gemspec
117
120
  - lib/figure_set.rb
118
121
  - lib/figure_set/version.rb
@@ -120,25 +123,23 @@ homepage: https://github.com/tsukasaoishi/figure_set
120
123
  licenses:
121
124
  - MIT
122
125
  metadata: {}
123
- post_install_message:
126
+ post_install_message:
124
127
  rdoc_options: []
125
128
  require_paths:
126
129
  - lib
127
- - ext
128
130
  required_ruby_version: !ruby/object:Gem::Requirement
129
131
  requirements:
130
132
  - - ">="
131
133
  - !ruby/object:Gem::Version
132
- version: '2.2'
134
+ version: '0'
133
135
  required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  requirements:
135
137
  - - ">="
136
138
  - !ruby/object:Gem::Version
137
139
  version: '0'
138
140
  requirements: []
139
- rubyforge_project:
140
- rubygems_version: 2.7.3
141
- signing_key:
141
+ rubygems_version: 3.4.1
142
+ signing_key:
142
143
  specification_version: 4
143
144
  summary: FigureSet is the library which treats set operation.
144
145
  test_files: []