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
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68703cf5d7a17b70ea0c0eda48afc85c3b6830902cff9ea5bb4f6aa0056d04fa
|
4
|
+
data.tar.gz: 5d11a68c4f9cff705c533faba0288a3f97031217de241e49efe978597c203ceb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
-
|
6
|
-
- "bundle exec rake compile"
|
5
|
+
- "bin/setup"
|
7
6
|
script:
|
8
|
-
- "
|
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
data/bin/test
ADDED
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
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
-
|
36
|
+
static leaf_node
|
37
|
+
init_and_intersection_leaf_node(root_node result_set, leaf_node base, leaf_node other)
|
45
38
|
{
|
46
|
-
|
39
|
+
unsigned long data;
|
40
|
+
leaf_node leaf;
|
47
41
|
|
48
|
-
|
42
|
+
data = base->data & other->data;
|
49
43
|
|
50
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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
|
53
|
+
static void
|
54
|
+
middel_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
|
66
55
|
{
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
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
|
73
|
+
static void
|
74
|
+
last_intersection_branch_node(root_node result_set, branch_node branch, branch_node base, branch_node other)
|
85
75
|
{
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
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
|
-
|
93
|
+
//
|
94
|
+
// intersection
|
95
|
+
//
|
96
|
+
void
|
97
|
+
intersection(root_node result_set, root_node set0, root_node set1)
|
105
98
|
{
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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
|
}
|
data/ext/figure_set/array.c
CHANGED
@@ -8,91 +8,84 @@
|
|
8
8
|
#include <ruby.h>
|
9
9
|
#include "figure_set.h"
|
10
10
|
|
11
|
-
|
12
|
-
|
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
|
-
|
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
|
-
|
27
|
-
{
|
28
|
-
unsigned int i, count;
|
17
|
+
x = leaf->data;
|
29
18
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
26
|
+
static void
|
27
|
+
search_and_get_array(branch_node branch, VALUE array)
|
48
28
|
{
|
49
|
-
|
50
|
-
unsigned long x;
|
29
|
+
unsigned int i, count;
|
51
30
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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
|
-
|
63
|
-
|
64
|
-
|
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
|
53
|
+
unsigned long
|
54
|
+
bit32_count(unsigned long x)
|
79
55
|
{
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
}
|
data/ext/figure_set/figure_set.h
CHANGED
@@ -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
|
-
|
79
|
-
|
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
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|