roaring 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/Rakefile +11 -1
- data/ext/roaring/bitmap32.c +294 -0
- data/ext/roaring/bitmap64.c +300 -0
- data/ext/roaring/cext.c +4 -295
- data/ext/roaring/extconf.rb +1 -4
- data/ext/roaring/{roaring/roaring.c → roaring.c} +16679 -10313
- data/ext/roaring/roaring.h +2949 -0
- data/ext/roaring/roaring_ruby.h +17 -0
- data/lib/roaring/version.rb +1 -1
- data/lib/roaring.rb +63 -40
- metadata +8 -8
- data/ext/roaring/roaring/LICENSE +0 -235
- data/ext/roaring/roaring/README.md +0 -42
- data/ext/roaring/roaring/roaring.h +0 -1031
- data/ext/roaring/roaring/roaring.hh +0 -2016
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e21e62ddcac0d57cf83948329add1ead73e1299adc691b57dd940e7e5ca5306
|
4
|
+
data.tar.gz: 1367042b6b8d27c24f9e124cf8c081bc18aa153e4431cb6339181c65904dbb68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b94ccd487a8b5e28b6130e25a759f4ab1d226670cb30063cede4c5815a19c69481a5a5f7bbfafc6ebbca6e55fdf6e85166e63f0c5fe13a82bfeaba3d107ec7d
|
7
|
+
data.tar.gz: 9735a5df2fff46c07e917fc4cbd421fbff2bcca9304cf0da3f70c9f9e0ea12ed54447280b7a17b18f9d5a43a0f178f2d6fd840d5017dc80494b1f7add81ee242
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
@@ -17,4 +17,14 @@ Rake::ExtensionTask.new("roaring") do |ext|
|
|
17
17
|
ext.lib_dir = "lib/roaring"
|
18
18
|
end
|
19
19
|
|
20
|
-
task
|
20
|
+
task :update_roaring do
|
21
|
+
require "open-uri"
|
22
|
+
|
23
|
+
["roaring.c", "roaring.h"].each do |filename|
|
24
|
+
url = "https://github.com/RoaringBitmap/CRoaring/releases/latest/download/#{filename}"
|
25
|
+
source = URI.open(url).read
|
26
|
+
File.write("#{__dir__}/ext/roaring/#{filename}", source)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
task default: %i[compile test]
|
@@ -0,0 +1,294 @@
|
|
1
|
+
#include "roaring_ruby.h"
|
2
|
+
|
3
|
+
#include <stdio.h>
|
4
|
+
|
5
|
+
static VALUE cRoaringBitmap32;
|
6
|
+
|
7
|
+
static inline uint32_t
|
8
|
+
NUM2UINT32(VALUE num) {
|
9
|
+
if (!FIXNUM_P(num) && !RB_TYPE_P(num, T_BIGNUM)) {
|
10
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(num));
|
11
|
+
} else if ((SIGNED_VALUE)num < (SIGNED_VALUE)INT2FIX(0)) {
|
12
|
+
rb_raise(rb_eRangeError, "Integer %"PRIdVALUE " must be >= 0 to use with Roaring::Bitmap", num);
|
13
|
+
} else {
|
14
|
+
return FIX2UINT(num);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
static void rb_roaring32_free(void *data)
|
19
|
+
{
|
20
|
+
roaring_bitmap_free(data);
|
21
|
+
}
|
22
|
+
|
23
|
+
static size_t rb_roaring32_memsize(const void *data)
|
24
|
+
{
|
25
|
+
// This is probably an estimate, "frozen" refers to the "frozen"
|
26
|
+
// serialization format, which mimics the in-memory representation.
|
27
|
+
return sizeof(roaring_bitmap_t) + roaring_bitmap_frozen_size_in_bytes(data);
|
28
|
+
}
|
29
|
+
|
30
|
+
static const rb_data_type_t roaring_type = {
|
31
|
+
.wrap_struct_name = "roaring/bitmap",
|
32
|
+
.function = {
|
33
|
+
.dfree = rb_roaring32_free,
|
34
|
+
.dsize = rb_roaring32_memsize
|
35
|
+
},
|
36
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
37
|
+
};
|
38
|
+
|
39
|
+
static VALUE rb_roaring32_alloc(VALUE self)
|
40
|
+
{
|
41
|
+
roaring_bitmap_t *data = roaring_bitmap_create();
|
42
|
+
return TypedData_Wrap_Struct(self, &roaring_type, data);
|
43
|
+
}
|
44
|
+
|
45
|
+
static roaring_bitmap_t *get_bitmap(VALUE obj) {
|
46
|
+
roaring_bitmap_t *bitmap;
|
47
|
+
TypedData_Get_Struct(obj, roaring_bitmap_t, &roaring_type, bitmap);
|
48
|
+
return bitmap;
|
49
|
+
}
|
50
|
+
|
51
|
+
static VALUE rb_roaring32_replace(VALUE self, VALUE other) {
|
52
|
+
roaring_bitmap_t *self_data = get_bitmap(self);
|
53
|
+
roaring_bitmap_t *other_data = get_bitmap(other);
|
54
|
+
|
55
|
+
roaring_bitmap_overwrite(self_data, other_data);
|
56
|
+
|
57
|
+
return self;
|
58
|
+
}
|
59
|
+
|
60
|
+
static VALUE rb_roaring32_cardinality(VALUE self)
|
61
|
+
{
|
62
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
63
|
+
uint64_t cardinality = roaring_bitmap_get_cardinality(data);
|
64
|
+
return ULONG2NUM(cardinality);
|
65
|
+
}
|
66
|
+
|
67
|
+
static VALUE rb_roaring32_add(VALUE self, VALUE val)
|
68
|
+
{
|
69
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
70
|
+
|
71
|
+
uint32_t num = NUM2UINT32(val);
|
72
|
+
roaring_bitmap_add(data, num);
|
73
|
+
return self;
|
74
|
+
}
|
75
|
+
|
76
|
+
static VALUE rb_roaring32_add_p(VALUE self, VALUE val)
|
77
|
+
{
|
78
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
79
|
+
|
80
|
+
uint32_t num = NUM2UINT32(val);
|
81
|
+
return roaring_bitmap_add_checked(data, num) ? self : Qnil;
|
82
|
+
}
|
83
|
+
|
84
|
+
static VALUE rb_roaring32_remove(VALUE self, VALUE val)
|
85
|
+
{
|
86
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
87
|
+
|
88
|
+
uint32_t num = NUM2UINT32(val);
|
89
|
+
roaring_bitmap_remove(data, num);
|
90
|
+
return self;
|
91
|
+
}
|
92
|
+
|
93
|
+
static VALUE rb_roaring32_remove_p(VALUE self, VALUE val)
|
94
|
+
{
|
95
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
96
|
+
|
97
|
+
uint32_t num = NUM2UINT32(val);
|
98
|
+
return roaring_bitmap_remove_checked(data, num) ? self : Qnil;
|
99
|
+
}
|
100
|
+
|
101
|
+
static VALUE rb_roaring32_include_p(VALUE self, VALUE val)
|
102
|
+
{
|
103
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
104
|
+
|
105
|
+
uint32_t num = NUM2UINT32(val);
|
106
|
+
return RBOOL(roaring_bitmap_contains(data, num));
|
107
|
+
}
|
108
|
+
|
109
|
+
static VALUE rb_roaring32_empty_p(VALUE self)
|
110
|
+
{
|
111
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
112
|
+
return RBOOL(roaring_bitmap_is_empty(data));
|
113
|
+
}
|
114
|
+
|
115
|
+
static VALUE rb_roaring32_clear(VALUE self)
|
116
|
+
{
|
117
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
118
|
+
roaring_bitmap_clear(data);
|
119
|
+
return self;
|
120
|
+
}
|
121
|
+
|
122
|
+
bool rb_roaring32_each_i(uint32_t value, void *param) {
|
123
|
+
rb_yield(UINT2NUM(value));
|
124
|
+
return true; // iterate till the end
|
125
|
+
}
|
126
|
+
|
127
|
+
static VALUE rb_roaring32_each(VALUE self)
|
128
|
+
{
|
129
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
130
|
+
roaring_iterate(data, rb_roaring32_each_i, NULL);
|
131
|
+
return self;
|
132
|
+
}
|
133
|
+
|
134
|
+
static VALUE rb_roaring32_aref(VALUE self, VALUE rankv)
|
135
|
+
{
|
136
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
137
|
+
|
138
|
+
uint32_t rank = NUM2UINT32(rankv);
|
139
|
+
uint32_t val;
|
140
|
+
|
141
|
+
if (roaring_bitmap_select(data, rank, &val)) {
|
142
|
+
return UINT2NUM(val);
|
143
|
+
} else {
|
144
|
+
return Qnil;
|
145
|
+
}
|
146
|
+
return self;
|
147
|
+
}
|
148
|
+
|
149
|
+
static VALUE rb_roaring32_min(VALUE self)
|
150
|
+
{
|
151
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
152
|
+
|
153
|
+
if (roaring_bitmap_is_empty(data)) {
|
154
|
+
return Qnil;
|
155
|
+
} else {
|
156
|
+
uint32_t val = roaring_bitmap_minimum(data);
|
157
|
+
return UINT2NUM(val);
|
158
|
+
}
|
159
|
+
}
|
160
|
+
|
161
|
+
static VALUE rb_roaring32_max(VALUE self)
|
162
|
+
{
|
163
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
164
|
+
|
165
|
+
if (roaring_bitmap_is_empty(data)) {
|
166
|
+
return Qnil;
|
167
|
+
} else {
|
168
|
+
uint32_t val = roaring_bitmap_maximum(data);
|
169
|
+
return UINT2NUM(val);
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
static VALUE rb_roaring32_run_optimize(VALUE self)
|
174
|
+
{
|
175
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
176
|
+
return RBOOL(roaring_bitmap_run_optimize(data));
|
177
|
+
}
|
178
|
+
|
179
|
+
static VALUE rb_roaring32_serialize(VALUE self)
|
180
|
+
{
|
181
|
+
roaring_bitmap_t *data = get_bitmap(self);
|
182
|
+
|
183
|
+
size_t size = roaring_bitmap_portable_size_in_bytes(data);
|
184
|
+
VALUE str = rb_str_buf_new(size);
|
185
|
+
|
186
|
+
size_t written = roaring_bitmap_portable_serialize(data, RSTRING_PTR(str));
|
187
|
+
rb_str_set_len(str, written);
|
188
|
+
|
189
|
+
return str;
|
190
|
+
}
|
191
|
+
|
192
|
+
static VALUE rb_roaring32_deserialize(VALUE self, VALUE str)
|
193
|
+
{
|
194
|
+
roaring_bitmap_t *bitmap = roaring_bitmap_portable_deserialize_safe(RSTRING_PTR(str), RSTRING_LEN(str));
|
195
|
+
|
196
|
+
return TypedData_Wrap_Struct(cRoaringBitmap32, &roaring_type, bitmap);
|
197
|
+
}
|
198
|
+
|
199
|
+
typedef roaring_bitmap_t *binary_func(const roaring_bitmap_t *, const roaring_bitmap_t *);
|
200
|
+
static VALUE rb_roaring32_binary_op(VALUE self, VALUE other, binary_func func) {
|
201
|
+
roaring_bitmap_t *self_data = get_bitmap(self);
|
202
|
+
roaring_bitmap_t *other_data = get_bitmap(other);
|
203
|
+
|
204
|
+
roaring_bitmap_t *result = func(self_data, other_data);
|
205
|
+
|
206
|
+
return TypedData_Wrap_Struct(cRoaringBitmap32, &roaring_type, result);
|
207
|
+
}
|
208
|
+
|
209
|
+
typedef bool binary_func_bool(const roaring_bitmap_t *, const roaring_bitmap_t *);
|
210
|
+
static VALUE rb_roaring32_binary_op_bool(VALUE self, VALUE other, binary_func_bool func) {
|
211
|
+
roaring_bitmap_t *self_data = get_bitmap(self);
|
212
|
+
roaring_bitmap_t *other_data = get_bitmap(other);
|
213
|
+
|
214
|
+
bool result = func(self_data, other_data);
|
215
|
+
return RBOOL(result);
|
216
|
+
}
|
217
|
+
|
218
|
+
|
219
|
+
static VALUE rb_roaring32_and(VALUE self, VALUE other)
|
220
|
+
{
|
221
|
+
return rb_roaring32_binary_op(self, other, roaring_bitmap_and);
|
222
|
+
}
|
223
|
+
|
224
|
+
static VALUE rb_roaring32_or(VALUE self, VALUE other)
|
225
|
+
{
|
226
|
+
return rb_roaring32_binary_op(self, other, roaring_bitmap_or);
|
227
|
+
}
|
228
|
+
|
229
|
+
static VALUE rb_roaring32_xor(VALUE self, VALUE other)
|
230
|
+
{
|
231
|
+
return rb_roaring32_binary_op(self, other, roaring_bitmap_xor);
|
232
|
+
}
|
233
|
+
|
234
|
+
static VALUE rb_roaring32_andnot(VALUE self, VALUE other)
|
235
|
+
{
|
236
|
+
return rb_roaring32_binary_op(self, other, roaring_bitmap_andnot);
|
237
|
+
}
|
238
|
+
|
239
|
+
static VALUE rb_roaring32_eq(VALUE self, VALUE other)
|
240
|
+
{
|
241
|
+
return rb_roaring32_binary_op_bool(self, other, roaring_bitmap_equals);
|
242
|
+
}
|
243
|
+
|
244
|
+
static VALUE rb_roaring32_lt(VALUE self, VALUE other)
|
245
|
+
{
|
246
|
+
return rb_roaring32_binary_op_bool(self, other, roaring_bitmap_is_strict_subset);
|
247
|
+
}
|
248
|
+
|
249
|
+
static VALUE rb_roaring32_lte(VALUE self, VALUE other)
|
250
|
+
{
|
251
|
+
return rb_roaring32_binary_op_bool(self, other, roaring_bitmap_is_subset);
|
252
|
+
}
|
253
|
+
|
254
|
+
static VALUE rb_roaring32_intersect_p(VALUE self, VALUE other)
|
255
|
+
{
|
256
|
+
return rb_roaring32_binary_op_bool(self, other, roaring_bitmap_intersect);
|
257
|
+
}
|
258
|
+
|
259
|
+
void
|
260
|
+
rb_roaring32_init(void)
|
261
|
+
{
|
262
|
+
cRoaringBitmap32 = rb_define_class_under(rb_mRoaring, "Bitmap32", rb_cObject);
|
263
|
+
rb_define_alloc_func(cRoaringBitmap32, rb_roaring32_alloc);
|
264
|
+
rb_define_method(cRoaringBitmap32, "replace", rb_roaring32_replace, 1);
|
265
|
+
rb_define_method(cRoaringBitmap32, "empty?", rb_roaring32_empty_p, 0);
|
266
|
+
rb_define_method(cRoaringBitmap32, "clear", rb_roaring32_clear, 0);
|
267
|
+
rb_define_method(cRoaringBitmap32, "cardinality", rb_roaring32_cardinality, 0);
|
268
|
+
rb_define_method(cRoaringBitmap32, "add", rb_roaring32_add, 1);
|
269
|
+
rb_define_method(cRoaringBitmap32, "add?", rb_roaring32_add_p, 1);
|
270
|
+
rb_define_method(cRoaringBitmap32, "<<", rb_roaring32_add, 1);
|
271
|
+
rb_define_method(cRoaringBitmap32, "remove", rb_roaring32_remove, 1);
|
272
|
+
rb_define_method(cRoaringBitmap32, "remove?", rb_roaring32_remove_p, 1);
|
273
|
+
rb_define_method(cRoaringBitmap32, "include?", rb_roaring32_include_p, 1);
|
274
|
+
rb_define_method(cRoaringBitmap32, "each", rb_roaring32_each, 0);
|
275
|
+
rb_define_method(cRoaringBitmap32, "[]", rb_roaring32_aref, 1);
|
276
|
+
|
277
|
+
rb_define_method(cRoaringBitmap32, "&", rb_roaring32_and, 1);
|
278
|
+
rb_define_method(cRoaringBitmap32, "|", rb_roaring32_or, 1);
|
279
|
+
rb_define_method(cRoaringBitmap32, "^", rb_roaring32_xor, 1);
|
280
|
+
rb_define_method(cRoaringBitmap32, "-", rb_roaring32_andnot, 1);
|
281
|
+
|
282
|
+
rb_define_method(cRoaringBitmap32, "==", rb_roaring32_eq, 1);
|
283
|
+
rb_define_method(cRoaringBitmap32, "<", rb_roaring32_lt, 1);
|
284
|
+
rb_define_method(cRoaringBitmap32, "<=", rb_roaring32_lte, 1);
|
285
|
+
rb_define_method(cRoaringBitmap32, "intersect?", rb_roaring32_intersect_p, 1);
|
286
|
+
|
287
|
+
rb_define_method(cRoaringBitmap32, "min", rb_roaring32_min, 0);
|
288
|
+
rb_define_method(cRoaringBitmap32, "max", rb_roaring32_max, 0);
|
289
|
+
|
290
|
+
rb_define_method(cRoaringBitmap32, "run_optimize", rb_roaring32_run_optimize, 0);
|
291
|
+
|
292
|
+
rb_define_method(cRoaringBitmap32, "serialize", rb_roaring32_serialize, 0);
|
293
|
+
rb_define_singleton_method(cRoaringBitmap32, "deserialize", rb_roaring32_deserialize, 1);
|
294
|
+
}
|
@@ -0,0 +1,300 @@
|
|
1
|
+
#include "roaring_ruby.h"
|
2
|
+
|
3
|
+
#include <stdio.h>
|
4
|
+
|
5
|
+
static VALUE cRoaringBitmap64;
|
6
|
+
|
7
|
+
static inline uint64_t
|
8
|
+
NUM2UINT64(VALUE num) {
|
9
|
+
if (!FIXNUM_P(num) && !RB_TYPE_P(num, T_BIGNUM)) {
|
10
|
+
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(num));
|
11
|
+
} else if ((SIGNED_VALUE)num < (SIGNED_VALUE)INT2FIX(0)) {
|
12
|
+
rb_raise(rb_eRangeError, "Integer %"PRIdVALUE " must be >= 0 to use with Roaring::Bitmap32", num);
|
13
|
+
} else {
|
14
|
+
return NUM2ULL(num);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
static void rb_roaring64_free(void *data)
|
19
|
+
{
|
20
|
+
roaring64_bitmap_free(data);
|
21
|
+
}
|
22
|
+
|
23
|
+
static size_t rb_roaring64_memsize(const void *data)
|
24
|
+
{
|
25
|
+
// This is probably an estimate, "frozen" refers to the "frozen"
|
26
|
+
// serialization format, which mimics the in-memory representation.
|
27
|
+
//return sizeof(roaring64_bitmap_t) + roaring64_bitmap_frozen_size_in_bytes(data);
|
28
|
+
return 0;
|
29
|
+
}
|
30
|
+
|
31
|
+
static const rb_data_type_t roaring64_type = {
|
32
|
+
.wrap_struct_name = "roaring/bitmap64",
|
33
|
+
.function = {
|
34
|
+
.dfree = rb_roaring64_free,
|
35
|
+
.dsize = rb_roaring64_memsize
|
36
|
+
},
|
37
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
38
|
+
};
|
39
|
+
|
40
|
+
static VALUE rb_roaring64_alloc(VALUE self)
|
41
|
+
{
|
42
|
+
roaring64_bitmap_t *data = roaring64_bitmap_create();
|
43
|
+
return TypedData_Wrap_Struct(self, &roaring64_type, data);
|
44
|
+
}
|
45
|
+
|
46
|
+
static roaring64_bitmap_t *get_bitmap(VALUE obj) {
|
47
|
+
roaring64_bitmap_t *bitmap;
|
48
|
+
TypedData_Get_Struct(obj, roaring64_bitmap_t, &roaring64_type, bitmap);
|
49
|
+
return bitmap;
|
50
|
+
}
|
51
|
+
|
52
|
+
static VALUE rb_roaring64_replace(VALUE self, VALUE other) {
|
53
|
+
roaring64_bitmap_t *self_data = get_bitmap(self);
|
54
|
+
roaring64_bitmap_t *other_data = get_bitmap(other);
|
55
|
+
|
56
|
+
// FIXME: Very likely a newer version of CRoaring will have
|
57
|
+
//roaring64_bitmap_overwrite(self_data, other_data);
|
58
|
+
|
59
|
+
roaring64_bitmap_remove_range_closed(self_data, 0, UINT64_MAX);
|
60
|
+
roaring64_bitmap_or_inplace(self_data, other_data);
|
61
|
+
|
62
|
+
return self;
|
63
|
+
}
|
64
|
+
|
65
|
+
static VALUE rb_roaring64_cardinality(VALUE self)
|
66
|
+
{
|
67
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
68
|
+
uint64_t cardinality = roaring64_bitmap_get_cardinality(data);
|
69
|
+
return ULONG2NUM(cardinality);
|
70
|
+
}
|
71
|
+
|
72
|
+
static VALUE rb_roaring64_add(VALUE self, VALUE val)
|
73
|
+
{
|
74
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
75
|
+
|
76
|
+
uint64_t num = NUM2UINT64(val);
|
77
|
+
roaring64_bitmap_add(data, num);
|
78
|
+
return self;
|
79
|
+
}
|
80
|
+
|
81
|
+
static VALUE rb_roaring64_add_p(VALUE self, VALUE val)
|
82
|
+
{
|
83
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
84
|
+
|
85
|
+
uint64_t num = NUM2UINT64(val);
|
86
|
+
return roaring64_bitmap_add_checked(data, num) ? self : Qnil;
|
87
|
+
}
|
88
|
+
|
89
|
+
static VALUE rb_roaring64_remove(VALUE self, VALUE val)
|
90
|
+
{
|
91
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
92
|
+
|
93
|
+
uint64_t num = NUM2UINT64(val);
|
94
|
+
roaring64_bitmap_remove(data, num);
|
95
|
+
return self;
|
96
|
+
}
|
97
|
+
|
98
|
+
static VALUE rb_roaring64_remove_p(VALUE self, VALUE val)
|
99
|
+
{
|
100
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
101
|
+
|
102
|
+
uint64_t num = NUM2UINT64(val);
|
103
|
+
return roaring64_bitmap_remove_checked(data, num) ? self : Qnil;
|
104
|
+
}
|
105
|
+
|
106
|
+
static VALUE rb_roaring64_include_p(VALUE self, VALUE val)
|
107
|
+
{
|
108
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
109
|
+
|
110
|
+
uint64_t num = NUM2UINT64(val);
|
111
|
+
return RBOOL(roaring64_bitmap_contains(data, num));
|
112
|
+
}
|
113
|
+
|
114
|
+
static VALUE rb_roaring64_empty_p(VALUE self)
|
115
|
+
{
|
116
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
117
|
+
return RBOOL(roaring64_bitmap_is_empty(data));
|
118
|
+
}
|
119
|
+
|
120
|
+
static VALUE rb_roaring64_clear(VALUE self)
|
121
|
+
{
|
122
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
123
|
+
// roaring64_bitmap_clear(data); // Doesn't exist yet in 4.0.0
|
124
|
+
roaring64_bitmap_remove_range_closed(data, 0, UINT64_MAX);
|
125
|
+
return self;
|
126
|
+
}
|
127
|
+
|
128
|
+
bool rb_roaring64_each_i(uint64_t value, void *param) {
|
129
|
+
rb_yield(ULL2NUM(value));
|
130
|
+
return true; // iterate till the end
|
131
|
+
}
|
132
|
+
|
133
|
+
static VALUE rb_roaring64_each(VALUE self)
|
134
|
+
{
|
135
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
136
|
+
roaring64_bitmap_iterate(data, rb_roaring64_each_i, NULL);
|
137
|
+
return self;
|
138
|
+
}
|
139
|
+
|
140
|
+
static VALUE rb_roaring64_aref(VALUE self, VALUE rankv)
|
141
|
+
{
|
142
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
143
|
+
|
144
|
+
uint64_t rank = NUM2UINT64(rankv);
|
145
|
+
uint64_t val;
|
146
|
+
|
147
|
+
if (roaring64_bitmap_select(data, rank, &val)) {
|
148
|
+
return ULL2NUM(val);
|
149
|
+
} else {
|
150
|
+
return Qnil;
|
151
|
+
}
|
152
|
+
return self;
|
153
|
+
}
|
154
|
+
|
155
|
+
static VALUE rb_roaring64_min(VALUE self)
|
156
|
+
{
|
157
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
158
|
+
|
159
|
+
if (roaring64_bitmap_is_empty(data)) {
|
160
|
+
return Qnil;
|
161
|
+
} else {
|
162
|
+
uint64_t val = roaring64_bitmap_minimum(data);
|
163
|
+
return ULL2NUM(val);
|
164
|
+
}
|
165
|
+
}
|
166
|
+
|
167
|
+
static VALUE rb_roaring64_max(VALUE self)
|
168
|
+
{
|
169
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
170
|
+
|
171
|
+
if (roaring64_bitmap_is_empty(data)) {
|
172
|
+
return Qnil;
|
173
|
+
} else {
|
174
|
+
uint64_t val = roaring64_bitmap_maximum(data);
|
175
|
+
return ULL2NUM(val);
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
static VALUE rb_roaring64_run_optimize(VALUE self)
|
180
|
+
{
|
181
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
182
|
+
return RBOOL(roaring64_bitmap_run_optimize(data));
|
183
|
+
}
|
184
|
+
|
185
|
+
static VALUE rb_roaring64_serialize(VALUE self)
|
186
|
+
{
|
187
|
+
roaring64_bitmap_t *data = get_bitmap(self);
|
188
|
+
|
189
|
+
size_t size = roaring64_bitmap_portable_size_in_bytes(data);
|
190
|
+
VALUE str = rb_str_buf_new(size);
|
191
|
+
|
192
|
+
size_t written = roaring64_bitmap_portable_serialize(data, RSTRING_PTR(str));
|
193
|
+
rb_str_set_len(str, written);
|
194
|
+
|
195
|
+
return str;
|
196
|
+
}
|
197
|
+
|
198
|
+
static VALUE rb_roaring64_deserialize(VALUE self, VALUE str)
|
199
|
+
{
|
200
|
+
roaring64_bitmap_t *bitmap = roaring64_bitmap_portable_deserialize_safe(RSTRING_PTR(str), RSTRING_LEN(str));
|
201
|
+
|
202
|
+
return TypedData_Wrap_Struct(cRoaringBitmap64, &roaring64_type, bitmap);
|
203
|
+
}
|
204
|
+
|
205
|
+
typedef roaring64_bitmap_t *binary_func(const roaring64_bitmap_t *, const roaring64_bitmap_t *);
|
206
|
+
static VALUE rb_roaring64_binary_op(VALUE self, VALUE other, binary_func func) {
|
207
|
+
roaring64_bitmap_t *self_data = get_bitmap(self);
|
208
|
+
roaring64_bitmap_t *other_data = get_bitmap(other);
|
209
|
+
|
210
|
+
roaring64_bitmap_t *result = func(self_data, other_data);
|
211
|
+
|
212
|
+
return TypedData_Wrap_Struct(cRoaringBitmap64, &roaring64_type, result);
|
213
|
+
}
|
214
|
+
|
215
|
+
typedef bool binary_func_bool(const roaring64_bitmap_t *, const roaring64_bitmap_t *);
|
216
|
+
static VALUE rb_roaring64_binary_op_bool(VALUE self, VALUE other, binary_func_bool func) {
|
217
|
+
roaring64_bitmap_t *self_data = get_bitmap(self);
|
218
|
+
roaring64_bitmap_t *other_data = get_bitmap(other);
|
219
|
+
|
220
|
+
bool result = func(self_data, other_data);
|
221
|
+
return RBOOL(result);
|
222
|
+
}
|
223
|
+
|
224
|
+
|
225
|
+
static VALUE rb_roaring64_and(VALUE self, VALUE other)
|
226
|
+
{
|
227
|
+
return rb_roaring64_binary_op(self, other, roaring64_bitmap_and);
|
228
|
+
}
|
229
|
+
|
230
|
+
static VALUE rb_roaring64_or(VALUE self, VALUE other)
|
231
|
+
{
|
232
|
+
return rb_roaring64_binary_op(self, other, roaring64_bitmap_or);
|
233
|
+
}
|
234
|
+
|
235
|
+
static VALUE rb_roaring64_xor(VALUE self, VALUE other)
|
236
|
+
{
|
237
|
+
return rb_roaring64_binary_op(self, other, roaring64_bitmap_xor);
|
238
|
+
}
|
239
|
+
|
240
|
+
static VALUE rb_roaring64_andnot(VALUE self, VALUE other)
|
241
|
+
{
|
242
|
+
return rb_roaring64_binary_op(self, other, roaring64_bitmap_andnot);
|
243
|
+
}
|
244
|
+
|
245
|
+
static VALUE rb_roaring64_eq(VALUE self, VALUE other)
|
246
|
+
{
|
247
|
+
return rb_roaring64_binary_op_bool(self, other, roaring64_bitmap_equals);
|
248
|
+
}
|
249
|
+
|
250
|
+
static VALUE rb_roaring64_lt(VALUE self, VALUE other)
|
251
|
+
{
|
252
|
+
return rb_roaring64_binary_op_bool(self, other, roaring64_bitmap_is_strict_subset);
|
253
|
+
}
|
254
|
+
|
255
|
+
static VALUE rb_roaring64_lte(VALUE self, VALUE other)
|
256
|
+
{
|
257
|
+
return rb_roaring64_binary_op_bool(self, other, roaring64_bitmap_is_subset);
|
258
|
+
}
|
259
|
+
|
260
|
+
static VALUE rb_roaring64_intersect_p(VALUE self, VALUE other)
|
261
|
+
{
|
262
|
+
return rb_roaring64_binary_op_bool(self, other, roaring64_bitmap_intersect);
|
263
|
+
}
|
264
|
+
|
265
|
+
void
|
266
|
+
rb_roaring64_init(void)
|
267
|
+
{
|
268
|
+
cRoaringBitmap64 = rb_define_class_under(rb_mRoaring, "Bitmap64", rb_cObject);
|
269
|
+
rb_define_alloc_func(cRoaringBitmap64, rb_roaring64_alloc);
|
270
|
+
rb_define_method(cRoaringBitmap64, "replace", rb_roaring64_replace, 1);
|
271
|
+
rb_define_method(cRoaringBitmap64, "empty?", rb_roaring64_empty_p, 0);
|
272
|
+
rb_define_method(cRoaringBitmap64, "clear", rb_roaring64_clear, 0);
|
273
|
+
rb_define_method(cRoaringBitmap64, "cardinality", rb_roaring64_cardinality, 0);
|
274
|
+
rb_define_method(cRoaringBitmap64, "add", rb_roaring64_add, 1);
|
275
|
+
rb_define_method(cRoaringBitmap64, "add?", rb_roaring64_add_p, 1);
|
276
|
+
rb_define_method(cRoaringBitmap64, "<<", rb_roaring64_add, 1);
|
277
|
+
rb_define_method(cRoaringBitmap64, "remove", rb_roaring64_remove, 1);
|
278
|
+
rb_define_method(cRoaringBitmap64, "remove?", rb_roaring64_remove_p, 1);
|
279
|
+
rb_define_method(cRoaringBitmap64, "include?", rb_roaring64_include_p, 1);
|
280
|
+
rb_define_method(cRoaringBitmap64, "each", rb_roaring64_each, 0);
|
281
|
+
rb_define_method(cRoaringBitmap64, "[]", rb_roaring64_aref, 1);
|
282
|
+
|
283
|
+
rb_define_method(cRoaringBitmap64, "&", rb_roaring64_and, 1);
|
284
|
+
rb_define_method(cRoaringBitmap64, "|", rb_roaring64_or, 1);
|
285
|
+
rb_define_method(cRoaringBitmap64, "^", rb_roaring64_xor, 1);
|
286
|
+
rb_define_method(cRoaringBitmap64, "-", rb_roaring64_andnot, 1);
|
287
|
+
|
288
|
+
rb_define_method(cRoaringBitmap64, "==", rb_roaring64_eq, 1);
|
289
|
+
rb_define_method(cRoaringBitmap64, "<", rb_roaring64_lt, 1);
|
290
|
+
rb_define_method(cRoaringBitmap64, "<=", rb_roaring64_lte, 1);
|
291
|
+
rb_define_method(cRoaringBitmap64, "intersect?", rb_roaring64_intersect_p, 1);
|
292
|
+
|
293
|
+
rb_define_method(cRoaringBitmap64, "min", rb_roaring64_min, 0);
|
294
|
+
rb_define_method(cRoaringBitmap64, "max", rb_roaring64_max, 0);
|
295
|
+
|
296
|
+
rb_define_method(cRoaringBitmap64, "run_optimize", rb_roaring64_run_optimize, 0);
|
297
|
+
|
298
|
+
rb_define_method(cRoaringBitmap64, "serialize", rb_roaring64_serialize, 0);
|
299
|
+
rb_define_singleton_method(cRoaringBitmap64, "deserialize", rb_roaring64_deserialize, 1);
|
300
|
+
}
|