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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e561bc42e983722b42d4633631eebe1cb3d9ef8a7f5429867840657ed0e6ae2
4
- data.tar.gz: c81d4e8b25d38e7f4974f1d627d396421a60da2d64fd02f068d88927875f8e16
3
+ metadata.gz: 68703cf5d7a17b70ea0c0eda48afc85c3b6830902cff9ea5bb4f6aa0056d04fa
4
+ data.tar.gz: 5d11a68c4f9cff705c533faba0288a3f97031217de241e49efe978597c203ceb
5
5
  SHA512:
6
- metadata.gz: f02e42c9ea2ca6e993b95d1e6fbd34f927ab862664df1f0259a0e76721e8b6c4d504f5f20efba0c50564849e7d928e384d899da8084d1432e8e5a13ccbf3f071
7
- data.tar.gz: c7ff2ea5d1961280b43faf257d766e9af8d64c567c723c3e221ab8265b6609cd714648de3050cf012708e0d2ddf86c57098bb0a4e85139dcfbd8062149b8288e
6
+ metadata.gz: d3ed337f5e9a5282c6a901f181179230f6bf39f316eac3f200eae741499abcdb0fe7186d7e6172df1fdc68e6ca2b9abb6bec1d60b6f7b893fee003650ace3efc
7
+ data.tar.gz: abf5fa5906669b40633979657ef3bf4dba93e6f3c841fc8e29e14476b950b6728dbae5a45e3c6a57644ccb95c847e3f7f6ab164507435608b9b3e1ae4be5ae07
data/.travis.yml CHANGED
@@ -2,14 +2,15 @@ language: ruby
2
2
  before_install:
3
3
  - gem install bundler
4
4
  before_script:
5
- - bundle update
6
- - "bundle exec rake compile"
5
+ - "bin/setup"
7
6
  script:
8
- - "bundle exec rake test"
7
+ - "bin/test"
9
8
  cache: bundler
10
9
  rvm:
11
10
  - 2.2.9
12
11
  - 2.3.6
13
12
  - 2.4.3
14
13
  - 2.5.0
14
+ matrix:
15
+ fast_finish: true
15
16
  bundler_args: --jobs 3 --retry 3
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "figure_set"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install --path .bundle
6
+ bundle exec rake compile
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/test ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle exec rake test
7
+
8
+ # Do any other automated setup that you need to do here
data/ext/figure_set/and.c CHANGED
@@ -8,111 +8,118 @@
8
8
  #include <ruby.h>
9
9
  #include "figure_set.h"
10
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
- }
11
+ static void middel_intersection_branch_node(root_node, branch_node, branch_node, branch_node);
12
+ static void last_intersection_branch_node(root_node, branch_node, branch_node, branch_node);
29
13
 
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
- }
14
+ static branch_node
15
+ init_and_intersection_branch_node(root_node result_set, branch_node base, branch_node other)
16
+ {
17
+ branch_node branch;
18
+
19
+ branch = (branch_node)init_branch_node();
20
+
21
+ if (base->children_type == CT_LEAF) {
22
+ last_intersection_branch_node(result_set, branch, base, other);
23
+ } else {
24
+ middel_intersection_branch_node(result_set, branch, base, other);
25
+ }
26
+
27
+ if (branch->children_size) {
28
+ branch->children_type = base->children_type;
29
+ return branch;
30
+ } else {
31
+ destroy_branch(branch);
32
+ return (branch_node)NULL;
33
+ }
42
34
  }
43
35
 
44
- void *init_and_intersection_branch_node(root_node result_set, branch_node base, branch_node other)
36
+ static leaf_node
37
+ init_and_intersection_leaf_node(root_node result_set, leaf_node base, leaf_node other)
45
38
  {
46
- branch_node branch;
39
+ unsigned long data;
40
+ leaf_node leaf;
47
41
 
48
- branch = (branch_node)init_branch_node();
42
+ data = base->data & other->data;
49
43
 
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
- }
44
+ if (!data) return (leaf_node)NULL;
55
45
 
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
- }
46
+ leaf = (leaf_node)init_leaf_node(base->offset);
47
+ leaf->data = data;
48
+ result_set->size += BIT_COUNT(leaf->data);
49
+
50
+ return leaf;
63
51
  }
64
52
 
65
- void middel_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
53
+ static void
54
+ middel_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
66
55
  {
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
- }
56
+ unsigned int i, count;
57
+ branch_node middle_branch;
58
+
59
+ for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) {
60
+ if (base->index[i]) {
61
+ count++;
62
+ if (other->index[i]) {
63
+ middle_branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]);
64
+ if (middle_branch) {
65
+ branch->index[i] = (void*)middle_branch;
66
+ branch->children_size++;
80
67
  }
68
+ }
81
69
  }
70
+ }
82
71
  }
83
72
 
84
- void last_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
73
+ static void
74
+ last_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
85
75
  {
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
- }
76
+ unsigned int i, count;
77
+ leaf_node leaf;
78
+
79
+ for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < base->children_size; i++) {
80
+ if (base->index[i]) {
81
+ count++;
82
+ if (other->index[i]) {
83
+ leaf = (leaf_node)init_and_intersection_leaf_node(result_set, (leaf_node)base->index[i], (leaf_node)other->index[i]);
84
+ if (leaf) {
85
+ branch->index[i] = (void*)leaf;
86
+ branch->children_size++;
99
87
  }
88
+ }
100
89
  }
90
+ }
101
91
  }
102
92
 
103
-
104
- void *init_and_intersection_leaf_node(root_node result_set, leaf_node base, leaf_node other)
93
+ //
94
+ // intersection
95
+ //
96
+ void
97
+ intersection(root_node result_set, root_node set0, root_node set1)
105
98
  {
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;
99
+ unsigned int i, count;
100
+ root_node base, other;
101
+ branch_node branch;
102
+
103
+ if (set0->size == 0 || set1->size == 0) {
104
+ return;
105
+ } else if (set0->size > set1->size) {
106
+ base = set1;
107
+ other = set0;
108
+ } else {
109
+ base = set0;
110
+ other = set1;
111
+ }
112
+
113
+ for (i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < base->children_size; i++) {
114
+ if (base->index[i]) {
115
+ count++;
116
+ if (other->index[i]) {
117
+ branch = (branch_node)init_and_intersection_branch_node(result_set, (branch_node)base->index[i], (branch_node)other->index[i]);
118
+ if (branch) {
119
+ result_set->index[i] = (void*)branch;
120
+ result_set->children_size++;
121
+ }
122
+ }
123
+ }
124
+ }
118
125
  }
@@ -8,91 +8,84 @@
8
8
  #include <ruby.h>
9
9
  #include "figure_set.h"
10
10
 
11
- //
12
- // output Array object from internal set
13
- //
14
- void to_array(root_node root, VALUE array)
11
+ static void
12
+ search_and_get_array_at_leaf(leaf_node leaf, VALUE array)
15
13
  {
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
- }
14
+ unsigned long i = 0;
15
+ unsigned long x;
25
16
 
26
- void search_and_get_array(branch_node branch, VALUE array)
27
- {
28
- unsigned int i, count;
17
+ x = leaf->data;
29
18
 
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
- }
19
+ while(x) {
20
+ i = BIT_COUNT((x & (-x))-1);
21
+ rb_ary_push(array, ULONG2NUM(leaf->offset + i));
22
+ x = x ^ (1UL << i);
23
+ }
45
24
  }
46
25
 
47
- void search_and_get_array_at_leaf(leaf_node leaf, VALUE array)
26
+ static void
27
+ search_and_get_array(branch_node branch, VALUE array)
48
28
  {
49
- unsigned long i = 0;
50
- unsigned long x;
29
+ unsigned int i, count;
51
30
 
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);
31
+ if (branch->children_type == CT_LEAF) {
32
+ for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
33
+ if (branch->index[i]) {
34
+ search_and_get_array_at_leaf((leaf_node)branch->index[i], array);
35
+ count++;
36
+ }
58
37
  }
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++;
38
+ } else {
39
+ for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_BRANCH || count < branch->children_size; i++) {
40
+ if (branch->index[i]) {
41
+ search_and_get_array((branch_node)branch->index[i], array);
42
+ count++;
43
+ }
68
44
  }
69
- */
45
+ }
70
46
  }
71
47
 
72
-
73
48
  //
74
49
  // bit count
75
50
  //
76
51
 
77
52
  // 32 bit
78
- unsigned long bit32_count(unsigned long x)
53
+ unsigned long
54
+ bit32_count(unsigned long x)
79
55
  {
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;
56
+ x = x - ((x >> 1UL) & 0x55555555UL);
57
+ x = (x & 0x33333333UL) + ((x >> 2UL) & 0x33333333UL);
58
+ x = (x + (x >> 4UL)) & 0x0f0f0f0fUL;
59
+ x = x + (x >> 8UL);
60
+ x = x + (x >> 16UL);
61
+ return x & 0x0000003FUL;
86
62
  }
87
63
 
88
64
  // 64 bit
89
- unsigned long bit64_count(unsigned long x)
65
+ unsigned long
66
+ bit64_count(unsigned long x)
67
+ {
68
+ x = x - ((x >> 1UL) & 0x5555555555555555UL);
69
+ x = (x & 0x3333333333333333UL) + ((x >> 2UL) & 0x3333333333333333UL);
70
+ x = (x & 0x0f0f0f0f0f0f0f0fUL) + ((x >> 4UL) & 0x0f0f0f0f0f0f0f0fUL);
71
+ x = (x + (x >> 8UL)) & 0x00ff00ff00ff00ffUL;
72
+ x = x + (x >> 16UL);
73
+ x = x + (x >> 32UL);
74
+ return x & 0x000000000000007FUL;
75
+ }
76
+
77
+ //
78
+ // output Array object from internal set
79
+ //
80
+ void
81
+ to_array(root_node root, VALUE array)
90
82
  {
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;
83
+ unsigned int i, count;
84
+
85
+ for(i = 0, count = 0; i < MAX_CHILDREN_SIZE_OF_ROOT_NODE || count < root->children_size; i++) {
86
+ if (root->index[i]) {
87
+ search_and_get_array((branch_node)root->index[i], array);
88
+ count++;
89
+ }
90
+ }
98
91
  }
@@ -7,18 +7,6 @@
7
7
 
8
8
  #include <math.h>
9
9
 
10
- #ifndef RUBY_19
11
- #ifndef RFLOAT_VALUE
12
- #define RFLOAT_VALUE(v) (RFLOAT(v)->value)
13
- #endif
14
- #ifndef RARRAY_LEN
15
- #define RARRAY_LEN(v) (RARRAY(v)->len)
16
- #endif
17
- #ifndef RARRAY_PTR
18
- #define RARRAY_PTR(v) (RARRAY(v)->ptr)
19
- #endif
20
- #endif
21
-
22
10
  // max children size of branch
23
11
  #define MAX_CHILDREN_SIZE_OF_BRANCH (unsigned long)16
24
12
  #define LAST_BRANCH_LEVEL (unsigned long)6
@@ -43,9 +31,6 @@ static unsigned long OFFSET_SCALE[] = {0x20000000, 0x2000000, 0x200000, 0x20000,
43
31
  #define VALID_MIN_VALUE UINT2NUM(0)
44
32
  #define VALID_MAX_VALUE UINT2NUM(0xffffffff)
45
33
 
46
- // FigureSet class object
47
- static VALUE rb_cFigureSet;
48
-
49
34
  // leaf node
50
35
  // example:
51
36
  // data = 5 # (bit is 0...0101) exist bit numbers is (2, 0)
@@ -68,78 +53,44 @@ typedef struct _branch_node {
68
53
  } *branch_node;
69
54
 
70
55
  typedef struct _root_node {
71
- unsigned char children_size;
56
+ unsigned char children_size;
72
57
  unsigned long size; // number of elements in set
73
58
  void *index[MAX_CHILDREN_SIZE_OF_ROOT_NODE]; // children pointer
74
59
  } *root_node;
75
60
 
61
+ // bit count
62
+ unsigned long bit32_count(unsigned long);
63
+ unsigned long bit64_count(unsigned long);
64
+
76
65
  // initialize
77
66
  void init_root_node(root_node);
78
- void *init_branch_node();
79
- void *init_leaf_node(unsigned long);
67
+ branch_node init_branch_node();
68
+ leaf_node init_leaf_node(unsigned long);
80
69
 
81
70
  // init_copy
82
71
  void copy_root_node(root_node, root_node);
83
- void copy_branch_node(root_node, branch_node, branch_node);
84
- void *init_and_copy_brance_node(root_node, branch_node);
85
- void *init_and_copy_leaf_node(root_node, leaf_node);
72
+ branch_node init_and_copy_brance_node(root_node, branch_node);
73
+ leaf_node init_and_copy_leaf_node(root_node, leaf_node);
74
+
75
+ // memory free
76
+ void destroy_all(root_node);
77
+ void destroy_all_branches(root_node);
78
+ void destroy_branch(branch_node);
86
79
 
87
80
  // insert element into set
88
81
  void add_num(root_node, unsigned long);
89
- unsigned int search_and_insert(branch_node, unsigned long, unsigned long, unsigned long);
90
- unsigned int search_and_insert_at_leaf(leaf_node, unsigned long);
91
82
 
92
83
  // remove element from set
93
84
  void delete_num(root_node, unsigned long);
94
- unsigned int search_and_remove(branch_node, unsigned long, unsigned long, unsigned long);
95
- unsigned int search_and_remove_at_leaf(leaf_node, unsigned long);
96
85
 
97
86
  // output Array object from internal set
98
87
  void to_array(root_node, VALUE);
99
- void search_and_get_array(branch_node, VALUE);
100
- void search_and_get_array_at_leaf(leaf_node, VALUE);
101
88
 
102
89
  // join
103
90
  void join(root_node, root_node, root_node);
104
- void middle_join_branch_node(root_node, branch_node, branch_node, branch_node);
105
- void last_join_branch_node(root_node, branch_node, branch_node, branch_node);
106
- void *init_and_join_brance_node(root_node, branch_node, branch_node);
107
- void *init_and_join_leaf_node(root_node, leaf_node, leaf_node);
108
91
 
109
92
  // intersection
110
93
  void intersection(root_node, root_node, root_node);
111
- void middel_intersection_branch_node(root_node, branch_node, branch_node, branch_node);
112
- void last_intersection_branch_node(root_node, branch_node, branch_node, branch_node);
113
- void *init_and_intersection_leaf_node(root_node, leaf_node, leaf_node);
114
- void *init_and_intersection_branch_node(root_node, branch_node, branch_node);
115
- unsigned long bit32_count(unsigned long);
116
- unsigned long bit64_count(unsigned long);
117
94
 
118
95
  // sample
119
96
  void sample(root_node, VALUE, unsigned long);
120
- void search_and_sample_array(branch_node, VALUE);
121
- void search_and_sample_array_at_leaf(leaf_node, VALUE);
122
-
123
- // memory free
124
- void destroy_all(root_node);
125
- void destroy_all_branches(root_node);
126
- void destroy_branch(branch_node);
127
-
128
- //-----------------------------------------------------------
129
- // Ruby Methods
130
- // ----------------------------------------------------------
131
-
132
- static VALUE t_allocate(VALUE);
133
- static VALUE t_initialize(int, VALUE *, VALUE);
134
- static VALUE t_initialize_copy(VALUE, VALUE);
135
- static VALUE t_add(VALUE, VALUE);
136
- static VALUE t_delete(VALUE, VALUE);
137
- static VALUE t_intersection(VALUE, VALUE);
138
- static VALUE t_union(VALUE, VALUE);
139
- static VALUE t_to_a(VALUE);
140
- static VALUE t_sample(int, VALUE *, VALUE);
141
- static VALUE t_size(VALUE);
142
- static VALUE t_empty(VALUE);
143
- static VALUE t_clear(VALUE);
144
- void Init_figure_set(void);
145
-