1 module roaring.c;
2 
3 import core.stdc.inttypes: uint8_t, uint16_t, int32_t, uint32_t, uint64_t;
4 
5 extern(C):
6 
7 struct roaring_array_t {
8     int32_t size;
9     int32_t allocation_size;
10     void **containers;
11     uint16_t *keys;
12     uint8_t *typecodes;
13 }
14 
15 struct roaring_bitmap_t {
16     roaring_array_t high_low_container;
17     bool copy_on_write;
18 }
19 
20 struct roaring_uint32_iterator_t {
21     const roaring_bitmap_t *parent;  // owner
22     int32_t container_index;         // point to the current container index
23     int32_t in_container_index;  // for bitset and array container, this is out
24                                  // index
25     int32_t run_index;           // for run container, this points  at the run
26 
27     uint32_t current_value;
28     bool has_value;
29 
30     const void *container;  // should be:
31                      // parent->high_low_container.containers[container_index];
32     uint8_t typecode;  // should be:
33                        // parent->high_low_container.typecodes[container_index];
34     uint32_t highbits;  // should be:
35                         // parent->high_low_container.keys[container_index]) <<
36                         // 16;
37 }
38 
39 extern(C) alias roaring_iterator = bool function(uint32_t value, void* param);
40 
41 @nogc @safe
42 void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t x);
43 
44 /**
45  * Add value n_args from pointer vals, faster than repeatedly calling
46  * roaring_bitmap_add
47  *
48  */
49 void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args, const uint32_t *vals);
50 
51 /**
52  * Add value x
53  * Returns true if a new value was added, false if the value was already existing.
54  */
55 @nogc @safe
56 bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t x);
57 
58 /**
59  * Add all values in range [min, max]
60  */
61 @nogc @safe
62 void roaring_bitmap_add_range_closed(roaring_bitmap_t *ra, uint32_t min, uint32_t max);
63 
64 /**
65  * Add all values in range [min, max)
66  */
67 @nogc @safe
68 void roaring_bitmap_add_range(roaring_bitmap_t *ra, uint64_t min, uint64_t max);
69 
70 /**
71  * Computes the intersection between two bitmaps and returns new bitmap. The
72  * caller is
73  * responsible for memory management.
74  *
75  */
76 @nogc @safe
77 roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1, const roaring_bitmap_t *x2);
78 
79 /**
80  * Inplace version modifies x1, x1 == x2 is allowed
81  */
82 @nogc @safe
83 void roaring_bitmap_and_inplace(roaring_bitmap_t *x1,
84                                 const roaring_bitmap_t *x2);
85 
86 /**
87  * Computes the union between two bitmaps and returns new bitmap. The caller is
88  * responsible for memory management.
89  */
90 @nogc @safe
91 roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *x1, const roaring_bitmap_t *x2);
92 
93 /**
94  * Inplace version of roaring_bitmap_or, modifies x1. TDOO: decide whether x1 ==
95  *x2 ok
96  *
97  */
98 @nogc @safe
99 void roaring_bitmap_or_inplace(roaring_bitmap_t *x1,
100                                const roaring_bitmap_t *x2);
101 
102 /**
103  * Computes the symmetric difference (xor) between two bitmaps
104  * and returns new bitmap. The caller is responsible for memory management.
105  */
106 @nogc @safe
107 roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *x1, const roaring_bitmap_t *x2);
108 
109 /**
110  * Inplace version of roaring_bitmap_xor, modifies x1. x1 != x2.
111  *
112  */
113 @nogc @safe
114 void roaring_bitmap_xor_inplace(roaring_bitmap_t *x1,
115                                 const roaring_bitmap_t *x2);
116 /**
117  * Computes the  difference (andnot) between two bitmaps
118  * and returns new bitmap. The caller is responsible for memory management.
119  */
120 roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *x1,
121                                         const roaring_bitmap_t *x2);
122 
123 /**
124  * Inplace version of roaring_bitmap_andnot, modifies x1. x1 != x2.
125  *
126  */
127 void roaring_bitmap_andnot_inplace(roaring_bitmap_t *x1,
128                                    const roaring_bitmap_t *x2);
129 
130 
131 @nogc @safe
132 bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) pure;
133 
134 /**
135  * Check whether a range of values from range_start (included) to range_end (excluded) is present
136  */
137 @nogc @safe
138 bool roaring_bitmap_contains_range(const roaring_bitmap_t *r, uint64_t range_start, uint64_t range_end) pure;
139 
140 
141 @nogc
142 roaring_bitmap_t *roaring_bitmap_create();
143 
144 @nogc
145 roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap);
146 
147 /**
148  * Return true if all the elements of ra1 are also in ra2.
149  */
150 bool roaring_bitmap_is_subset(const roaring_bitmap_t *ra1, const roaring_bitmap_t *ra2);
151 
152 
153 /**  use with roaring_bitmap_serialize
154 * see roaring_bitmap_portable_deserialize if you want a format that's
155 * compatible with Java and Go implementations
156 */
157 roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf);
158 
159 /**
160  * read a bitmap from a serialized version. This is meant to be compatible with
161  * the Java and Go versions. See format specification at
162  * https://github.com/RoaringBitmap/RoaringFormatSpec
163  * In case of failure, a null pointer is returned.
164  * This function is unsafe in the sense that if there is no valid serialized
165  * bitmap at the pointer, then many bytes could be read, possibly causing a buffer
166  * overflow. For a safer approach,
167  * call roaring_bitmap_portable_deserialize_safe.
168  */
169 roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf);
170 
171 /**
172  * read a bitmap from a serialized version in a safe manner (reading up to maxbytes).
173  * This is meant to be compatible with
174  * the Java and Go versions. See format specification at
175  * https://github.com/RoaringBitmap/RoaringFormatSpec
176  * In case of failure, a null pointer is returned.
177  */
178 roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf, size_t maxbytes);
179 
180 /**
181  * Check how many bytes would be read (up to maxbytes) at this pointer if there
182  * is a bitmap, returns zero if there is no valid bitmap.
183  * This is meant to be compatible with
184  * the Java and Go versions. See format specification at
185  * https://github.com/RoaringBitmap/RoaringFormatSpec
186  */
187 size_t roaring_bitmap_portable_deserialize_size(const char *buf, size_t maxbytes);
188 
189 /**
190  * Return true if the two bitmaps contain the same elements.
191  */
192 bool roaring_bitmap_equals(const roaring_bitmap_t *ra1, const roaring_bitmap_t *ra2);
193 
194 void roaring_bitmap_free(roaring_bitmap_t *r);
195 roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max, uint32_t step);
196 
197 @nogc @safe
198 uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *ra) pure;
199 
200 /**
201  * Returns number of elements in range [range_start, range_end).
202  */
203 @nogc @safe
204 uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *ra,
205                                           uint64_t range_start, uint64_t range_end) pure;
206 
207 @nogc @safe
208 uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *bm) pure;
209 
210 @nogc @safe
211 uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *bm) pure;
212 
213 /**
214 * roaring_bitmap_rank returns the number of integers that are smaller or equal
215 * to x.
216 */
217 @nogc @safe
218 uint64_t roaring_bitmap_rank(const roaring_bitmap_t *bm, uint32_t x) pure;
219 
220 /**
221  * write a bitmap to a char buffer.  The output buffer should refer to at least
222  *  roaring_bitmap_portable_size_in_bytes(ra) bytes of allocated memory.
223  * This is meant to be compatible with
224  * the
225  * Java and Go versions. Returns how many bytes were written which should be
226  * roaring_bitmap_portable_size_in_bytes(ra).  See format specification at
227  * https://github.com/RoaringBitmap/RoaringFormatSpec
228  */
229 size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *ra, char *buf);
230 
231 /**
232  * How many bytes are required to serialize this bitmap (meant to be compatible
233  * with Java and Go versions).  See format specification at
234  * https://github.com/RoaringBitmap/RoaringFormatSpec
235  */
236 @nogc @safe
237 size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *ra) pure;
238 
239 @nogc @safe
240 void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x);
241 
242 @nogc @safe
243 bool roaring_bitmap_run_optimize(roaring_bitmap_t *r);
244 
245 bool roaring_bitmap_select(const roaring_bitmap_t *bm, uint32_t rank, uint32_t *element);
246 
247 /**
248 * write the bitmap to an output pointer, this output buffer should refer to
249 * at least roaring_bitmap_size_in_bytes(ra) allocated bytes.
250 *
251 * see roaring_bitmap_portable_serialize if you want a format that's compatible
252 * with Java and Go implementations
253 *
254 * this format has the benefit of being sometimes more space efficient than
255 * roaring_bitmap_portable_serialize
256 * e.g., when the data is sparse.
257 *
258 * Returns how many bytes were written which should be
259 * roaring_bitmap_size_in_bytes(ra).
260 */
261 size_t roaring_bitmap_serialize(const roaring_bitmap_t *ra, char *buf);
262 
263 /**
264  * How many bytes are required to serialize this bitmap (NOT compatible
265  * with Java and Go versions)
266  */
267  @nogc @safe
268 size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *ra) pure;
269 
270 /**
271  * Convert the bitmap to an array. Write the output to "ans",
272  * caller is responsible to ensure that there is enough memory
273  * allocated
274  * (e.g., ans = malloc(roaring_bitmap_get_cardinality(mybitmap)
275  *   * sizeof(uint32_t)) )
276  */
277 void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *ra, uint32_t *ans);
278 
279 /**
280 * Advance the iterator. If there is a new value, then it->has_value is true.
281 * The new value is in it->current_value. Values are traversed in increasing
282 * orders. For convenience, returns it->has_value.
283 */
284 @nogc
285 bool roaring_advance_uint32_iterator(roaring_uint32_iterator_t *it);
286 
287 /**
288 * Create an iterator object that can be used to iterate through the
289 * values. Caller is responsible for calling roaring_free_iterator.
290 * The iterator is initialized. If there is a  value, then it->has_value is true.
291 * The first value is in it->current_value. The iterator traverses the values
292 * in increasing order.
293 *
294 * This function calls roaring_init_iterator.
295 */
296 @nogc
297 roaring_uint32_iterator_t *roaring_create_iterator(const roaring_bitmap_t *ra);
298 
299 /**
300 * Free memory following roaring_create_iterator
301 */
302 @nogc
303 void roaring_free_uint32_iterator(roaring_uint32_iterator_t *it);
304 
305 
306 /**
307 * Copies a  bitmap. This does memory allocation. The caller is responsible for
308 * memory management.
309 *
310 */
311 @nogc
312 roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r);
313