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
data/ext/roaring/cext.c
CHANGED
@@ -1,302 +1,11 @@
|
|
1
|
-
#include "
|
2
|
-
#include "roaring.h"
|
1
|
+
#include "roaring_ruby.h"
|
3
2
|
|
4
|
-
#include <stdio.h>
|
5
|
-
|
6
|
-
static VALUE cRoaringBitmap;
|
7
3
|
VALUE rb_mRoaring;
|
8
4
|
|
9
|
-
|
10
|
-
#define RBOOL(x) ((x) ? Qtrue : Qfalse)
|
11
|
-
#endif
|
12
|
-
|
13
|
-
static inline uint32_t
|
14
|
-
NUM2UINT32(VALUE num) {
|
15
|
-
if (!FIXNUM_P(num) && !RB_TYPE_P(num, T_BIGNUM)) {
|
16
|
-
rb_raise(rb_eTypeError, "wrong argument type %s (expected Integer)", rb_obj_classname(num));
|
17
|
-
} else if ((SIGNED_VALUE)num < (SIGNED_VALUE)INT2FIX(0)) {
|
18
|
-
rb_raise(rb_eRangeError, "Integer %"PRIdVALUE " must be >= 0 to use with Roaring::Bitmap", num);
|
19
|
-
} else {
|
20
|
-
return FIX2UINT(num);
|
21
|
-
}
|
22
|
-
}
|
23
|
-
|
24
|
-
static void rb_roaring_free(void *data)
|
25
|
-
{
|
26
|
-
roaring_bitmap_free(data);
|
27
|
-
}
|
28
|
-
|
29
|
-
static size_t rb_roaring_memsize(const void *data)
|
30
|
-
{
|
31
|
-
// This is probably an estimate, "frozen" refers to the "frozen"
|
32
|
-
// serialization format, which mimics the in-memory representation.
|
33
|
-
return sizeof(roaring_bitmap_t) + roaring_bitmap_frozen_size_in_bytes(data);
|
34
|
-
}
|
35
|
-
|
36
|
-
static const rb_data_type_t roaring_type = {
|
37
|
-
.wrap_struct_name = "roaring/bitmap",
|
38
|
-
.function = {
|
39
|
-
.dfree = rb_roaring_free,
|
40
|
-
.dsize = rb_roaring_memsize
|
41
|
-
},
|
42
|
-
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
|
43
|
-
};
|
44
|
-
|
45
|
-
static VALUE rb_roaring_alloc(VALUE self)
|
46
|
-
{
|
47
|
-
roaring_bitmap_t *data = roaring_bitmap_create();
|
48
|
-
return TypedData_Wrap_Struct(self, &roaring_type, data);
|
49
|
-
}
|
50
|
-
|
51
|
-
static roaring_bitmap_t *get_bitmap(VALUE obj) {
|
52
|
-
roaring_bitmap_t *bitmap;
|
53
|
-
TypedData_Get_Struct(obj, roaring_bitmap_t, &roaring_type, bitmap);
|
54
|
-
return bitmap;
|
55
|
-
}
|
56
|
-
|
57
|
-
static VALUE rb_roaring_initialize_copy(VALUE self, VALUE other) {
|
58
|
-
roaring_bitmap_t *self_data = get_bitmap(self);
|
59
|
-
roaring_bitmap_t *other_data = get_bitmap(other);
|
60
|
-
|
61
|
-
roaring_bitmap_overwrite(self_data, other_data);
|
62
|
-
|
63
|
-
return self;
|
64
|
-
}
|
65
|
-
|
66
|
-
static VALUE rb_roaring_cardinality(VALUE self)
|
67
|
-
{
|
68
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
69
|
-
uint64_t cardinality = roaring_bitmap_get_cardinality(data);
|
70
|
-
return ULONG2NUM(cardinality);
|
71
|
-
}
|
72
|
-
|
73
|
-
static VALUE rb_roaring_add(VALUE self, VALUE val)
|
74
|
-
{
|
75
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
76
|
-
|
77
|
-
uint32_t num = NUM2UINT32(val);
|
78
|
-
roaring_bitmap_add(data, num);
|
79
|
-
return self;
|
80
|
-
}
|
81
|
-
|
82
|
-
static VALUE rb_roaring_add_p(VALUE self, VALUE val)
|
83
|
-
{
|
84
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
85
|
-
|
86
|
-
uint32_t num = NUM2UINT32(val);
|
87
|
-
return roaring_bitmap_add_checked(data, num) ? self : Qnil;
|
88
|
-
}
|
89
|
-
|
90
|
-
static VALUE rb_roaring_remove(VALUE self, VALUE val)
|
91
|
-
{
|
92
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
93
|
-
|
94
|
-
uint32_t num = NUM2UINT32(val);
|
95
|
-
roaring_bitmap_remove(data, num);
|
96
|
-
return self;
|
97
|
-
}
|
98
|
-
|
99
|
-
static VALUE rb_roaring_remove_p(VALUE self, VALUE val)
|
100
|
-
{
|
101
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
102
|
-
|
103
|
-
uint32_t num = NUM2UINT32(val);
|
104
|
-
return roaring_bitmap_remove_checked(data, num) ? self : Qnil;
|
105
|
-
}
|
106
|
-
|
107
|
-
static VALUE rb_roaring_include_p(VALUE self, VALUE val)
|
108
|
-
{
|
109
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
110
|
-
|
111
|
-
uint32_t num = NUM2UINT32(val);
|
112
|
-
return RBOOL(roaring_bitmap_contains(data, num));
|
113
|
-
}
|
114
|
-
|
115
|
-
static VALUE rb_roaring_empty_p(VALUE self)
|
116
|
-
{
|
117
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
118
|
-
return RBOOL(roaring_bitmap_is_empty(data));
|
119
|
-
}
|
120
|
-
|
121
|
-
static VALUE rb_roaring_clear(VALUE self)
|
122
|
-
{
|
123
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
124
|
-
roaring_bitmap_clear(data);
|
125
|
-
return self;
|
126
|
-
}
|
127
|
-
|
128
|
-
bool rb_roaring_each_i(uint32_t value, void *param) {
|
129
|
-
rb_yield(UINT2NUM(value));
|
130
|
-
return true; // iterate till the end
|
131
|
-
}
|
132
|
-
|
133
|
-
static VALUE rb_roaring_each(VALUE self)
|
134
|
-
{
|
135
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
136
|
-
roaring_iterate(data, rb_roaring_each_i, NULL);
|
137
|
-
return self;
|
138
|
-
}
|
139
|
-
|
140
|
-
static VALUE rb_roaring_aref(VALUE self, VALUE rankv)
|
141
|
-
{
|
142
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
143
|
-
|
144
|
-
uint32_t rank = NUM2UINT32(rankv);
|
145
|
-
uint32_t val;
|
146
|
-
|
147
|
-
if (roaring_bitmap_select(data, rank, &val)) {
|
148
|
-
return UINT2NUM(val);
|
149
|
-
} else {
|
150
|
-
return Qnil;
|
151
|
-
}
|
152
|
-
return self;
|
153
|
-
}
|
154
|
-
|
155
|
-
static VALUE rb_roaring_min(VALUE self)
|
156
|
-
{
|
157
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
158
|
-
|
159
|
-
if (roaring_bitmap_is_empty(data)) {
|
160
|
-
return Qnil;
|
161
|
-
} else {
|
162
|
-
uint32_t val = roaring_bitmap_minimum(data);
|
163
|
-
return UINT2NUM(val);
|
164
|
-
}
|
165
|
-
}
|
166
|
-
|
167
|
-
static VALUE rb_roaring_max(VALUE self)
|
168
|
-
{
|
169
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
170
|
-
|
171
|
-
if (roaring_bitmap_is_empty(data)) {
|
172
|
-
return Qnil;
|
173
|
-
} else {
|
174
|
-
uint32_t val = roaring_bitmap_maximum(data);
|
175
|
-
return UINT2NUM(val);
|
176
|
-
}
|
177
|
-
}
|
178
|
-
|
179
|
-
static VALUE rb_roaring_run_optimize(VALUE self)
|
180
|
-
{
|
181
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
182
|
-
return RBOOL(roaring_bitmap_run_optimize(data));
|
183
|
-
}
|
184
|
-
|
185
|
-
static VALUE rb_roaring_serialize(VALUE self)
|
186
|
-
{
|
187
|
-
roaring_bitmap_t *data = get_bitmap(self);
|
188
|
-
|
189
|
-
size_t size = roaring_bitmap_portable_size_in_bytes(data);
|
190
|
-
VALUE str = rb_str_buf_new(size);
|
191
|
-
|
192
|
-
size_t written = roaring_bitmap_portable_serialize(data, RSTRING_PTR(str));
|
193
|
-
rb_str_set_len(str, written);
|
194
|
-
|
195
|
-
return str;
|
196
|
-
}
|
197
|
-
|
198
|
-
static VALUE rb_roaring_deserialize(VALUE self, VALUE str)
|
199
|
-
{
|
200
|
-
roaring_bitmap_t *bitmap = roaring_bitmap_portable_deserialize_safe(RSTRING_PTR(str), RSTRING_LEN(str));
|
201
|
-
|
202
|
-
return TypedData_Wrap_Struct(cRoaringBitmap, &roaring_type, bitmap);
|
203
|
-
}
|
204
|
-
|
205
|
-
typedef roaring_bitmap_t *binary_func(const roaring_bitmap_t *, const roaring_bitmap_t *);
|
206
|
-
static VALUE rb_roaring_binary_op(VALUE self, VALUE other, binary_func func) {
|
207
|
-
roaring_bitmap_t *self_data = get_bitmap(self);
|
208
|
-
roaring_bitmap_t *other_data = get_bitmap(other);
|
209
|
-
|
210
|
-
roaring_bitmap_t *result = func(self_data, other_data);
|
211
|
-
|
212
|
-
return TypedData_Wrap_Struct(cRoaringBitmap, &roaring_type, result);
|
213
|
-
}
|
214
|
-
|
215
|
-
typedef bool binary_func_bool(const roaring_bitmap_t *, const roaring_bitmap_t *);
|
216
|
-
static VALUE rb_roaring_binary_op_bool(VALUE self, VALUE other, binary_func_bool func) {
|
217
|
-
roaring_bitmap_t *self_data = get_bitmap(self);
|
218
|
-
roaring_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_roaring_and(VALUE self, VALUE other)
|
226
|
-
{
|
227
|
-
return rb_roaring_binary_op(self, other, roaring_bitmap_and);
|
228
|
-
}
|
229
|
-
|
230
|
-
static VALUE rb_roaring_or(VALUE self, VALUE other)
|
231
|
-
{
|
232
|
-
return rb_roaring_binary_op(self, other, roaring_bitmap_or);
|
233
|
-
}
|
234
|
-
|
235
|
-
static VALUE rb_roaring_xor(VALUE self, VALUE other)
|
236
|
-
{
|
237
|
-
return rb_roaring_binary_op(self, other, roaring_bitmap_xor);
|
238
|
-
}
|
239
|
-
|
240
|
-
static VALUE rb_roaring_andnot(VALUE self, VALUE other)
|
241
|
-
{
|
242
|
-
return rb_roaring_binary_op(self, other, roaring_bitmap_andnot);
|
243
|
-
}
|
244
|
-
|
245
|
-
static VALUE rb_roaring_eq(VALUE self, VALUE other)
|
246
|
-
{
|
247
|
-
return rb_roaring_binary_op_bool(self, other, roaring_bitmap_equals);
|
248
|
-
}
|
249
|
-
|
250
|
-
static VALUE rb_roaring_lt(VALUE self, VALUE other)
|
251
|
-
{
|
252
|
-
return rb_roaring_binary_op_bool(self, other, roaring_bitmap_is_strict_subset);
|
253
|
-
}
|
254
|
-
|
255
|
-
static VALUE rb_roaring_lte(VALUE self, VALUE other)
|
256
|
-
{
|
257
|
-
return rb_roaring_binary_op_bool(self, other, roaring_bitmap_is_subset);
|
258
|
-
}
|
259
|
-
|
260
|
-
static VALUE rb_roaring_intersect_p(VALUE self, VALUE other)
|
261
|
-
{
|
262
|
-
return rb_roaring_binary_op_bool(self, other, roaring_bitmap_intersect);
|
263
|
-
}
|
264
|
-
|
265
|
-
void
|
5
|
+
RUBY_FUNC_EXPORTED void
|
266
6
|
Init_roaring(void)
|
267
7
|
{
|
268
8
|
rb_mRoaring = rb_define_module("Roaring");
|
269
|
-
|
270
|
-
|
271
|
-
rb_define_alloc_func(cRoaringBitmap, rb_roaring_alloc);
|
272
|
-
rb_define_method(cRoaringBitmap, "initialize_copy", rb_roaring_initialize_copy, 1);
|
273
|
-
rb_define_method(cRoaringBitmap, "empty?", rb_roaring_empty_p, 0);
|
274
|
-
rb_define_method(cRoaringBitmap, "clear", rb_roaring_clear, 0);
|
275
|
-
rb_define_method(cRoaringBitmap, "cardinality", rb_roaring_cardinality, 0);
|
276
|
-
rb_define_method(cRoaringBitmap, "add", rb_roaring_add, 1);
|
277
|
-
rb_define_method(cRoaringBitmap, "add?", rb_roaring_add_p, 1);
|
278
|
-
rb_define_method(cRoaringBitmap, "<<", rb_roaring_add, 1);
|
279
|
-
rb_define_method(cRoaringBitmap, "remove", rb_roaring_remove, 1);
|
280
|
-
rb_define_method(cRoaringBitmap, "remove?", rb_roaring_remove_p, 1);
|
281
|
-
rb_define_method(cRoaringBitmap, "include?", rb_roaring_include_p, 1);
|
282
|
-
rb_define_method(cRoaringBitmap, "each", rb_roaring_each, 0);
|
283
|
-
rb_define_method(cRoaringBitmap, "[]", rb_roaring_aref, 1);
|
284
|
-
|
285
|
-
rb_define_method(cRoaringBitmap, "&", rb_roaring_and, 1);
|
286
|
-
rb_define_method(cRoaringBitmap, "|", rb_roaring_or, 1);
|
287
|
-
rb_define_method(cRoaringBitmap, "^", rb_roaring_xor, 1);
|
288
|
-
rb_define_method(cRoaringBitmap, "-", rb_roaring_andnot, 1);
|
289
|
-
|
290
|
-
rb_define_method(cRoaringBitmap, "==", rb_roaring_eq, 1);
|
291
|
-
rb_define_method(cRoaringBitmap, "<", rb_roaring_lt, 1);
|
292
|
-
rb_define_method(cRoaringBitmap, "<=", rb_roaring_lte, 1);
|
293
|
-
rb_define_method(cRoaringBitmap, "intersect?", rb_roaring_intersect_p, 1);
|
294
|
-
|
295
|
-
rb_define_method(cRoaringBitmap, "min", rb_roaring_min, 0);
|
296
|
-
rb_define_method(cRoaringBitmap, "max", rb_roaring_max, 0);
|
297
|
-
|
298
|
-
rb_define_method(cRoaringBitmap, "run_optimize", rb_roaring_run_optimize, 0);
|
299
|
-
|
300
|
-
rb_define_method(cRoaringBitmap, "serialize", rb_roaring_serialize, 0);
|
301
|
-
rb_define_singleton_method(cRoaringBitmap, "deserialize", rb_roaring_deserialize, 1);
|
9
|
+
rb_roaring32_init();
|
10
|
+
rb_roaring64_init();
|
302
11
|
}
|