roaring 0.1.0 → 0.3.0

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