/src/croaring/src/containers/containers.c
Line | Count | Source |
1 | | #include <roaring/containers/containers.h> |
2 | | #include <roaring/memory.h> |
3 | | |
4 | | #ifdef __cplusplus |
5 | | extern "C" { |
6 | | // In Windows MSVC C++ compiler, (type){init} does not compile, |
7 | | // it causes C4576: a parenthesized type followed by an initializer list is a |
8 | | // non-standard explicit type conversion syntax The correct syntax is type{init} |
9 | | #define ROARING_INIT_ROARING_CONTAINER_ITERATOR_T roaring_container_iterator_t |
10 | | namespace roaring { |
11 | | namespace internal { |
12 | | #else |
13 | 0 | #define ROARING_INIT_ROARING_CONTAINER_ITERATOR_T (roaring_container_iterator_t) |
14 | | #endif |
15 | | |
16 | 0 | static inline uint32_t minimum_uint32(uint32_t a, uint32_t b) { |
17 | 0 | return (a < b) ? a : b; |
18 | 0 | } |
19 | | |
20 | | extern inline const container_t *container_unwrap_shared( |
21 | | const container_t *candidate_shared_container, uint8_t *type); |
22 | | |
23 | | extern inline container_t *container_mutable_unwrap_shared( |
24 | | container_t *candidate_shared_container, uint8_t *type); |
25 | | |
26 | | extern inline int container_get_cardinality(const container_t *c, |
27 | | uint8_t typecode); |
28 | | |
29 | | extern inline container_t *container_iand(container_t *c1, uint8_t type1, |
30 | | const container_t *c2, uint8_t type2, |
31 | | uint8_t *result_type); |
32 | | |
33 | | extern inline container_t *container_ior(container_t *c1, uint8_t type1, |
34 | | const container_t *c2, uint8_t type2, |
35 | | uint8_t *result_type); |
36 | | |
37 | | extern inline container_t *container_ixor(container_t *c1, uint8_t type1, |
38 | | const container_t *c2, uint8_t type2, |
39 | | uint8_t *result_type); |
40 | | |
41 | | extern inline container_t *container_iandnot(container_t *c1, uint8_t type1, |
42 | | const container_t *c2, |
43 | | uint8_t type2, |
44 | | uint8_t *result_type); |
45 | | |
46 | | extern bool container_iterator_next(const container_t *c, uint8_t typecode, |
47 | | roaring_container_iterator_t *it, |
48 | | uint16_t *value); |
49 | | |
50 | | extern bool container_iterator_prev(const container_t *c, uint8_t typecode, |
51 | | roaring_container_iterator_t *it, |
52 | | uint16_t *value); |
53 | | extern bool container_contains( |
54 | | const container_t *c, uint16_t val, |
55 | | uint8_t typecode // !!! should be second argument? |
56 | | ); |
57 | | |
58 | 923k | void container_free(container_t *c, uint8_t type) { |
59 | 923k | switch (type) { |
60 | 618 | case BITSET_CONTAINER_TYPE: |
61 | 618 | bitset_container_free(CAST_bitset(c)); |
62 | 618 | break; |
63 | 851k | case ARRAY_CONTAINER_TYPE: |
64 | 851k | array_container_free(CAST_array(c)); |
65 | 851k | break; |
66 | 72.0k | case RUN_CONTAINER_TYPE: |
67 | 72.0k | run_container_free(CAST_run(c)); |
68 | 72.0k | break; |
69 | 0 | case SHARED_CONTAINER_TYPE: |
70 | 0 | shared_container_free(CAST_shared(c)); |
71 | 0 | break; |
72 | 0 | default: |
73 | 0 | assert(false); |
74 | 0 | roaring_unreachable; |
75 | 923k | } |
76 | 923k | } |
77 | | |
78 | 0 | void container_printf(const container_t *c, uint8_t type) { |
79 | 0 | c = container_unwrap_shared(c, &type); |
80 | 0 | switch (type) { |
81 | 0 | case BITSET_CONTAINER_TYPE: |
82 | 0 | bitset_container_printf(const_CAST_bitset(c)); |
83 | 0 | return; |
84 | 0 | case ARRAY_CONTAINER_TYPE: |
85 | 0 | array_container_printf(const_CAST_array(c)); |
86 | 0 | return; |
87 | 0 | case RUN_CONTAINER_TYPE: |
88 | 0 | run_container_printf(const_CAST_run(c)); |
89 | 0 | return; |
90 | 0 | default: |
91 | 0 | roaring_unreachable; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | | void container_printf_as_uint32_array(const container_t *c, uint8_t typecode, |
96 | 0 | uint32_t base) { |
97 | 0 | c = container_unwrap_shared(c, &typecode); |
98 | 0 | switch (typecode) { |
99 | 0 | case BITSET_CONTAINER_TYPE: |
100 | 0 | bitset_container_printf_as_uint32_array(const_CAST_bitset(c), base); |
101 | 0 | return; |
102 | 0 | case ARRAY_CONTAINER_TYPE: |
103 | 0 | array_container_printf_as_uint32_array(const_CAST_array(c), base); |
104 | 0 | return; |
105 | 0 | case RUN_CONTAINER_TYPE: |
106 | 0 | run_container_printf_as_uint32_array(const_CAST_run(c), base); |
107 | 0 | return; |
108 | 0 | default: |
109 | 0 | roaring_unreachable; |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | | bool container_internal_validate(const container_t *container, uint8_t typecode, |
114 | 23.5k | const char **reason) { |
115 | 23.5k | if (container == NULL) { |
116 | 0 | *reason = "container is NULL"; |
117 | 0 | return false; |
118 | 0 | } |
119 | | // Not using container_unwrap_shared because it asserts if shared containers |
120 | | // are nested |
121 | 23.5k | if (typecode == SHARED_CONTAINER_TYPE) { |
122 | 0 | const shared_container_t *shared_container = |
123 | 0 | const_CAST_shared(container); |
124 | 0 | if (croaring_refcount_get(&shared_container->counter) == 0) { |
125 | 0 | *reason = "shared container has zero refcount"; |
126 | 0 | return false; |
127 | 0 | } |
128 | 0 | if (shared_container->typecode == SHARED_CONTAINER_TYPE) { |
129 | 0 | *reason = "shared container is nested"; |
130 | 0 | return false; |
131 | 0 | } |
132 | 0 | if (shared_container->container == NULL) { |
133 | 0 | *reason = "shared container has NULL container"; |
134 | 0 | return false; |
135 | 0 | } |
136 | 0 | container = shared_container->container; |
137 | 0 | typecode = shared_container->typecode; |
138 | 0 | } |
139 | 23.5k | switch (typecode) { |
140 | 76 | case BITSET_CONTAINER_TYPE: |
141 | 76 | return bitset_container_validate(const_CAST_bitset(container), |
142 | 76 | reason); |
143 | 17.6k | case ARRAY_CONTAINER_TYPE: |
144 | 17.6k | return array_container_validate(const_CAST_array(container), |
145 | 17.6k | reason); |
146 | 5.86k | case RUN_CONTAINER_TYPE: |
147 | 5.86k | return run_container_validate(const_CAST_run(container), reason); |
148 | 0 | default: |
149 | 0 | *reason = "invalid typecode"; |
150 | 0 | return false; |
151 | 23.5k | } |
152 | 23.5k | } |
153 | | |
154 | | extern inline bool container_nonzero_cardinality(const container_t *c, |
155 | | uint8_t typecode); |
156 | | |
157 | | extern inline int container_to_uint32_array(uint32_t *output, |
158 | | const container_t *c, |
159 | | uint8_t typecode, uint32_t base); |
160 | | |
161 | | extern inline container_t *container_add(container_t *c, uint16_t val, |
162 | | uint8_t typecode, // !!! 2nd arg? |
163 | | uint8_t *new_typecode); |
164 | | |
165 | | extern inline bool container_contains(const container_t *c, uint16_t val, |
166 | | uint8_t typecode); // !!! 2nd arg? |
167 | | |
168 | | extern inline container_t *container_and(const container_t *c1, uint8_t type1, |
169 | | const container_t *c2, uint8_t type2, |
170 | | uint8_t *result_type); |
171 | | |
172 | | extern inline container_t *container_or(const container_t *c1, uint8_t type1, |
173 | | const container_t *c2, uint8_t type2, |
174 | | uint8_t *result_type); |
175 | | |
176 | | extern inline container_t *container_xor(const container_t *c1, uint8_t type1, |
177 | | const container_t *c2, uint8_t type2, |
178 | | uint8_t *result_type); |
179 | | |
180 | | container_t *get_copy_of_container(container_t *c, uint8_t *typecode, |
181 | 0 | bool copy_on_write) { |
182 | 0 | if (copy_on_write) { |
183 | 0 | shared_container_t *shared_container; |
184 | 0 | if (*typecode == SHARED_CONTAINER_TYPE) { |
185 | 0 | shared_container = CAST_shared(c); |
186 | 0 | croaring_refcount_inc(&shared_container->counter); |
187 | 0 | return shared_container; |
188 | 0 | } |
189 | 0 | assert(*typecode != SHARED_CONTAINER_TYPE); |
190 | |
|
191 | 0 | if ((shared_container = (shared_container_t *)roaring_malloc( |
192 | 0 | sizeof(shared_container_t))) == NULL) { |
193 | 0 | return NULL; |
194 | 0 | } |
195 | | |
196 | 0 | shared_container->container = c; |
197 | 0 | shared_container->typecode = *typecode; |
198 | | // At this point, we are creating new shared container |
199 | | // so there should be no other references, and setting |
200 | | // the counter to 2 - even non-atomically - is safe as |
201 | | // long as the value is set before the return statement. |
202 | 0 | shared_container->counter = 2; |
203 | 0 | *typecode = SHARED_CONTAINER_TYPE; |
204 | |
|
205 | 0 | return shared_container; |
206 | 0 | } // copy_on_write |
207 | | // otherwise, no copy on write... |
208 | 0 | const container_t *actual_container = container_unwrap_shared(c, typecode); |
209 | 0 | assert(*typecode != SHARED_CONTAINER_TYPE); |
210 | 0 | return container_clone(actual_container, *typecode); |
211 | 0 | } |
212 | | |
213 | | /** |
214 | | * Copies a container, requires a typecode. This allocates new memory, caller |
215 | | * is responsible for deallocation. |
216 | | */ |
217 | 0 | container_t *container_clone(const container_t *c, uint8_t typecode) { |
218 | | // We do not want to allow cloning of shared containers. |
219 | | // c = container_unwrap_shared(c, &typecode); |
220 | 0 | switch (typecode) { |
221 | 0 | case BITSET_CONTAINER_TYPE: |
222 | 0 | return bitset_container_clone(const_CAST_bitset(c)); |
223 | 0 | case ARRAY_CONTAINER_TYPE: |
224 | 0 | return array_container_clone(const_CAST_array(c)); |
225 | 0 | case RUN_CONTAINER_TYPE: |
226 | 0 | return run_container_clone(const_CAST_run(c)); |
227 | 0 | case SHARED_CONTAINER_TYPE: |
228 | | // Shared containers are not cloneable. Are you mixing COW and |
229 | | // non-COW bitmaps? |
230 | 0 | return NULL; |
231 | 0 | default: |
232 | 0 | assert(false); |
233 | 0 | roaring_unreachable; |
234 | 0 | return NULL; |
235 | 0 | } |
236 | 0 | } |
237 | | |
238 | | container_t *shared_container_extract_copy(shared_container_t *sc, |
239 | 0 | uint8_t *typecode) { |
240 | 0 | assert(sc->typecode != SHARED_CONTAINER_TYPE); |
241 | 0 | *typecode = sc->typecode; |
242 | 0 | container_t *answer; |
243 | 0 | if (croaring_refcount_dec(&sc->counter)) { |
244 | 0 | answer = sc->container; |
245 | 0 | sc->container = NULL; // paranoid |
246 | 0 | roaring_free(sc); |
247 | 0 | } else { |
248 | 0 | answer = container_clone(sc->container, *typecode); |
249 | 0 | } |
250 | 0 | assert(*typecode != SHARED_CONTAINER_TYPE); |
251 | 0 | return answer; |
252 | 0 | } |
253 | | |
254 | 0 | void shared_container_free(shared_container_t *container) { |
255 | 0 | if (croaring_refcount_dec(&container->counter)) { |
256 | 0 | assert(container->typecode != SHARED_CONTAINER_TYPE); |
257 | 0 | container_free(container->container, container->typecode); |
258 | 0 | container->container = NULL; // paranoid |
259 | 0 | roaring_free(container); |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | | extern inline container_t *container_not(const container_t *c1, uint8_t type1, |
264 | | uint8_t *result_type); |
265 | | |
266 | | extern inline container_t *container_not_range(const container_t *c1, |
267 | | uint8_t type1, |
268 | | uint32_t range_start, |
269 | | uint32_t range_end, |
270 | | uint8_t *result_type); |
271 | | |
272 | | extern inline container_t *container_inot(container_t *c1, uint8_t type1, |
273 | | uint8_t *result_type); |
274 | | |
275 | | extern inline container_t *container_inot_range(container_t *c1, uint8_t type1, |
276 | | uint32_t range_start, |
277 | | uint32_t range_end, |
278 | | uint8_t *result_type); |
279 | | |
280 | | extern inline container_t *container_range_of_ones(uint32_t range_start, |
281 | | uint32_t range_end, |
282 | | uint8_t *result_type); |
283 | | |
284 | | // where are the correponding things for union and intersection?? |
285 | | extern inline container_t *container_lazy_xor(const container_t *c1, |
286 | | uint8_t type1, |
287 | | const container_t *c2, |
288 | | uint8_t type2, |
289 | | uint8_t *result_type); |
290 | | |
291 | | extern inline container_t *container_lazy_ixor(container_t *c1, uint8_t type1, |
292 | | const container_t *c2, |
293 | | uint8_t type2, |
294 | | uint8_t *result_type); |
295 | | |
296 | | extern inline container_t *container_andnot(const container_t *c1, |
297 | | uint8_t type1, |
298 | | const container_t *c2, |
299 | | uint8_t type2, |
300 | | uint8_t *result_type); |
301 | | |
302 | | roaring_container_iterator_t container_init_iterator(const container_t *c, |
303 | | uint8_t typecode, |
304 | 0 | uint16_t *value) { |
305 | 0 | switch (typecode) { |
306 | 0 | case BITSET_CONTAINER_TYPE: { |
307 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
308 | 0 | uint32_t wordindex = 0; |
309 | 0 | uint64_t word; |
310 | 0 | while ((word = bc->words[wordindex]) == 0) { |
311 | 0 | wordindex++; |
312 | 0 | } |
313 | | // word is non-zero |
314 | 0 | int32_t index = wordindex * 64 + roaring_trailing_zeroes(word); |
315 | 0 | *value = index; |
316 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{ |
317 | 0 | .index = index, |
318 | 0 | }; |
319 | 0 | } |
320 | 0 | case ARRAY_CONTAINER_TYPE: { |
321 | 0 | const array_container_t *ac = const_CAST_array(c); |
322 | 0 | *value = ac->array[0]; |
323 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{ |
324 | 0 | .index = 0, |
325 | 0 | }; |
326 | 0 | } |
327 | 0 | case RUN_CONTAINER_TYPE: { |
328 | 0 | const run_container_t *rc = const_CAST_run(c); |
329 | 0 | *value = rc->runs[0].value; |
330 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{ |
331 | 0 | .index = 0, |
332 | 0 | }; |
333 | 0 | } |
334 | 0 | default: |
335 | 0 | assert(false); |
336 | 0 | roaring_unreachable; |
337 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{0}; |
338 | 0 | } |
339 | 0 | } |
340 | | |
341 | | roaring_container_iterator_t container_init_iterator_last(const container_t *c, |
342 | | uint8_t typecode, |
343 | 0 | uint16_t *value) { |
344 | 0 | switch (typecode) { |
345 | 0 | case BITSET_CONTAINER_TYPE: { |
346 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
347 | 0 | uint32_t wordindex = BITSET_CONTAINER_SIZE_IN_WORDS - 1; |
348 | 0 | uint64_t word; |
349 | 0 | while ((word = bc->words[wordindex]) == 0) { |
350 | 0 | wordindex--; |
351 | 0 | } |
352 | | // word is non-zero |
353 | 0 | int32_t index = |
354 | 0 | wordindex * 64 + (63 - roaring_leading_zeroes(word)); |
355 | 0 | *value = index; |
356 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{ |
357 | 0 | .index = index, |
358 | 0 | }; |
359 | 0 | } |
360 | 0 | case ARRAY_CONTAINER_TYPE: { |
361 | 0 | const array_container_t *ac = const_CAST_array(c); |
362 | 0 | int32_t index = ac->cardinality - 1; |
363 | 0 | *value = ac->array[index]; |
364 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{ |
365 | 0 | .index = index, |
366 | 0 | }; |
367 | 0 | } |
368 | 0 | case RUN_CONTAINER_TYPE: { |
369 | 0 | const run_container_t *rc = const_CAST_run(c); |
370 | 0 | int32_t run_index = rc->n_runs - 1; |
371 | 0 | const rle16_t *last_run = &rc->runs[run_index]; |
372 | 0 | *value = last_run->value + last_run->length; |
373 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{ |
374 | 0 | .index = run_index, |
375 | 0 | }; |
376 | 0 | } |
377 | 0 | default: |
378 | 0 | assert(false); |
379 | 0 | roaring_unreachable; |
380 | 0 | return ROARING_INIT_ROARING_CONTAINER_ITERATOR_T{0}; |
381 | 0 | } |
382 | 0 | } |
383 | | |
384 | | bool container_iterator_lower_bound(const container_t *c, uint8_t typecode, |
385 | | roaring_container_iterator_t *it, |
386 | 0 | uint16_t *value_out, uint16_t val) { |
387 | 0 | if (val > container_maximum(c, typecode)) { |
388 | 0 | return false; |
389 | 0 | } |
390 | 0 | switch (typecode) { |
391 | 0 | case BITSET_CONTAINER_TYPE: { |
392 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
393 | 0 | it->index = bitset_container_index_equalorlarger(bc, val); |
394 | 0 | *value_out = it->index; |
395 | 0 | return true; |
396 | 0 | } |
397 | 0 | case ARRAY_CONTAINER_TYPE: { |
398 | 0 | const array_container_t *ac = const_CAST_array(c); |
399 | 0 | it->index = array_container_index_equalorlarger(ac, val); |
400 | 0 | *value_out = ac->array[it->index]; |
401 | 0 | return true; |
402 | 0 | } |
403 | 0 | case RUN_CONTAINER_TYPE: { |
404 | 0 | const run_container_t *rc = const_CAST_run(c); |
405 | 0 | it->index = run_container_index_equalorlarger(rc, val); |
406 | 0 | if (rc->runs[it->index].value <= val) { |
407 | 0 | *value_out = val; |
408 | 0 | } else { |
409 | 0 | *value_out = rc->runs[it->index].value; |
410 | 0 | } |
411 | 0 | return true; |
412 | 0 | } |
413 | 0 | default: |
414 | 0 | assert(false); |
415 | 0 | roaring_unreachable; |
416 | 0 | return false; |
417 | 0 | } |
418 | 0 | } |
419 | | |
420 | | bool container_iterator_read_into_uint32(const container_t *c, uint8_t typecode, |
421 | | roaring_container_iterator_t *it, |
422 | | uint32_t high16, uint32_t *buf, |
423 | | uint32_t count, uint32_t *consumed, |
424 | 0 | uint16_t *value_out) { |
425 | 0 | *consumed = 0; |
426 | 0 | if (count == 0) { |
427 | 0 | return false; |
428 | 0 | } |
429 | 0 | switch (typecode) { |
430 | 0 | case BITSET_CONTAINER_TYPE: { |
431 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
432 | 0 | uint32_t wordindex = it->index / 64; |
433 | 0 | uint64_t word = |
434 | 0 | bc->words[wordindex] & (UINT64_MAX << (it->index % 64)); |
435 | 0 | do { |
436 | | // Read set bits. |
437 | 0 | while (word != 0 && *consumed < count) { |
438 | 0 | *buf = high16 | |
439 | 0 | (wordindex * 64 + roaring_trailing_zeroes(word)); |
440 | 0 | word = word & (word - 1); |
441 | 0 | buf++; |
442 | 0 | (*consumed)++; |
443 | 0 | } |
444 | | // Skip unset bits. |
445 | 0 | while (word == 0 && |
446 | 0 | wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS) { |
447 | 0 | wordindex++; |
448 | 0 | word = bc->words[wordindex]; |
449 | 0 | } |
450 | 0 | } while (word != 0 && *consumed < count); |
451 | |
|
452 | 0 | if (word != 0) { |
453 | 0 | it->index = wordindex * 64 + roaring_trailing_zeroes(word); |
454 | 0 | *value_out = it->index; |
455 | 0 | return true; |
456 | 0 | } |
457 | 0 | return false; |
458 | 0 | } |
459 | 0 | case ARRAY_CONTAINER_TYPE: { |
460 | 0 | const array_container_t *ac = const_CAST_array(c); |
461 | 0 | uint32_t num_values = |
462 | 0 | minimum_uint32(ac->cardinality - it->index, count); |
463 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
464 | 0 | buf[i] = high16 | ac->array[it->index + i]; |
465 | 0 | } |
466 | 0 | *consumed += num_values; |
467 | 0 | it->index += num_values; |
468 | 0 | if (it->index < ac->cardinality) { |
469 | 0 | *value_out = ac->array[it->index]; |
470 | 0 | return true; |
471 | 0 | } |
472 | 0 | return false; |
473 | 0 | } |
474 | 0 | case RUN_CONTAINER_TYPE: { |
475 | 0 | const run_container_t *rc = const_CAST_run(c); |
476 | 0 | do { |
477 | 0 | uint32_t largest_run_value = |
478 | 0 | rc->runs[it->index].value + rc->runs[it->index].length; |
479 | 0 | uint32_t num_values = minimum_uint32( |
480 | 0 | largest_run_value - *value_out + 1, count - *consumed); |
481 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
482 | 0 | buf[i] = high16 | (*value_out + i); |
483 | 0 | } |
484 | 0 | *value_out += num_values; |
485 | 0 | buf += num_values; |
486 | 0 | *consumed += num_values; |
487 | | |
488 | | // We check for `value == 0` because `it->value += num_values` |
489 | | // can overflow when `value == UINT16_MAX`, and `count > |
490 | | // length`. In this case `value` will overflow to 0. |
491 | 0 | if (*value_out > largest_run_value || *value_out == 0) { |
492 | 0 | it->index++; |
493 | 0 | if (it->index < rc->n_runs) { |
494 | 0 | *value_out = rc->runs[it->index].value; |
495 | 0 | } else { |
496 | 0 | return false; |
497 | 0 | } |
498 | 0 | } |
499 | 0 | } while (*consumed < count); |
500 | 0 | return true; |
501 | 0 | } |
502 | 0 | default: |
503 | 0 | assert(false); |
504 | 0 | roaring_unreachable; |
505 | 0 | return 0; |
506 | 0 | } |
507 | 0 | } |
508 | | |
509 | | bool container_iterator_read_into_uint64(const container_t *c, uint8_t typecode, |
510 | | roaring_container_iterator_t *it, |
511 | | uint64_t high48, uint64_t *buf, |
512 | | uint32_t count, uint32_t *consumed, |
513 | 0 | uint16_t *value_out) { |
514 | 0 | *consumed = 0; |
515 | 0 | if (count == 0) { |
516 | 0 | return false; |
517 | 0 | } |
518 | 0 | switch (typecode) { |
519 | 0 | case BITSET_CONTAINER_TYPE: { |
520 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
521 | 0 | uint32_t wordindex = it->index / 64; |
522 | 0 | uint64_t word = |
523 | 0 | bc->words[wordindex] & (UINT64_MAX << (it->index % 64)); |
524 | 0 | do { |
525 | | // Read set bits. |
526 | 0 | while (word != 0 && *consumed < count) { |
527 | 0 | *buf = high48 | |
528 | 0 | (wordindex * 64 + roaring_trailing_zeroes(word)); |
529 | 0 | word = word & (word - 1); |
530 | 0 | buf++; |
531 | 0 | (*consumed)++; |
532 | 0 | } |
533 | | // Skip unset bits. |
534 | 0 | while (word == 0 && |
535 | 0 | wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS) { |
536 | 0 | wordindex++; |
537 | 0 | word = bc->words[wordindex]; |
538 | 0 | } |
539 | 0 | } while (word != 0 && *consumed < count); |
540 | |
|
541 | 0 | if (word != 0) { |
542 | 0 | it->index = wordindex * 64 + roaring_trailing_zeroes(word); |
543 | 0 | *value_out = it->index; |
544 | 0 | return true; |
545 | 0 | } |
546 | 0 | return false; |
547 | 0 | } |
548 | 0 | case ARRAY_CONTAINER_TYPE: { |
549 | 0 | const array_container_t *ac = const_CAST_array(c); |
550 | 0 | uint32_t num_values = |
551 | 0 | minimum_uint32(ac->cardinality - it->index, count); |
552 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
553 | 0 | buf[i] = high48 | ac->array[it->index + i]; |
554 | 0 | } |
555 | 0 | *consumed += num_values; |
556 | 0 | it->index += num_values; |
557 | 0 | if (it->index < ac->cardinality) { |
558 | 0 | *value_out = ac->array[it->index]; |
559 | 0 | return true; |
560 | 0 | } |
561 | 0 | return false; |
562 | 0 | } |
563 | 0 | case RUN_CONTAINER_TYPE: { |
564 | 0 | const run_container_t *rc = const_CAST_run(c); |
565 | 0 | do { |
566 | 0 | uint32_t largest_run_value = |
567 | 0 | rc->runs[it->index].value + rc->runs[it->index].length; |
568 | 0 | uint32_t num_values = minimum_uint32( |
569 | 0 | largest_run_value - *value_out + 1, count - *consumed); |
570 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
571 | 0 | buf[i] = high48 | (*value_out + i); |
572 | 0 | } |
573 | 0 | *value_out += num_values; |
574 | 0 | buf += num_values; |
575 | 0 | *consumed += num_values; |
576 | | |
577 | | // We check for `value == 0` because `it->value += num_values` |
578 | | // can overflow when `value == UINT16_MAX`, and `count > |
579 | | // length`. In this case `value` will overflow to 0. |
580 | 0 | if (*value_out > largest_run_value || *value_out == 0) { |
581 | 0 | it->index++; |
582 | 0 | if (it->index < rc->n_runs) { |
583 | 0 | *value_out = rc->runs[it->index].value; |
584 | 0 | } else { |
585 | 0 | return false; |
586 | 0 | } |
587 | 0 | } |
588 | 0 | } while (*consumed < count); |
589 | 0 | return true; |
590 | 0 | } |
591 | 0 | default: |
592 | 0 | assert(false); |
593 | 0 | roaring_unreachable; |
594 | 0 | return 0; |
595 | 0 | } |
596 | 0 | } |
597 | | |
598 | | bool container_iterator_read_backward_into_uint32( |
599 | | const container_t *c, uint8_t typecode, roaring_container_iterator_t *it, |
600 | | uint32_t high16, uint32_t *buf, uint32_t count, uint32_t *consumed, |
601 | 0 | uint16_t *value_out) { |
602 | 0 | *consumed = 0; |
603 | 0 | if (count == 0) { |
604 | 0 | return false; |
605 | 0 | } |
606 | 0 | switch (typecode) { |
607 | 0 | case BITSET_CONTAINER_TYPE: { |
608 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
609 | 0 | uint32_t wordindex = it->index / 64; |
610 | 0 | uint64_t word = |
611 | 0 | bc->words[wordindex] & (UINT64_MAX >> (63 - (it->index % 64))); |
612 | 0 | do { |
613 | | // Read set bits. |
614 | 0 | while (word != 0 && *consumed < count) { |
615 | 0 | uint32_t bit = 63 - roaring_leading_zeroes(word); |
616 | 0 | *buf = high16 | (wordindex * 64 + bit); |
617 | 0 | word &= ~(UINT64_C(1) << bit); |
618 | 0 | buf++; |
619 | 0 | (*consumed)++; |
620 | 0 | } |
621 | | // Skip unset bits. |
622 | 0 | while (word == 0 && wordindex > 0) { |
623 | 0 | wordindex--; |
624 | 0 | word = bc->words[wordindex]; |
625 | 0 | } |
626 | 0 | } while (word != 0 && *consumed < count); |
627 | |
|
628 | 0 | if (word != 0) { |
629 | 0 | it->index = |
630 | 0 | wordindex * 64 + (63 - roaring_leading_zeroes(word)); |
631 | 0 | *value_out = it->index; |
632 | 0 | return true; |
633 | 0 | } |
634 | 0 | return false; |
635 | 0 | } |
636 | 0 | case ARRAY_CONTAINER_TYPE: { |
637 | 0 | const array_container_t *ac = const_CAST_array(c); |
638 | 0 | uint32_t num_values = |
639 | 0 | minimum_uint32((uint32_t)(it->index + 1), count); |
640 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
641 | 0 | buf[i] = high16 | ac->array[it->index - i]; |
642 | 0 | } |
643 | 0 | *consumed += num_values; |
644 | 0 | it->index -= num_values; |
645 | 0 | if (it->index >= 0) { |
646 | 0 | *value_out = ac->array[it->index]; |
647 | 0 | return true; |
648 | 0 | } |
649 | 0 | return false; |
650 | 0 | } |
651 | 0 | case RUN_CONTAINER_TYPE: { |
652 | 0 | const run_container_t *rc = const_CAST_run(c); |
653 | 0 | do { |
654 | 0 | uint32_t run_start = rc->runs[it->index].value; |
655 | 0 | uint32_t num_values = minimum_uint32(*value_out - run_start + 1, |
656 | 0 | count - *consumed); |
657 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
658 | 0 | buf[i] = high16 | (*value_out - i); |
659 | 0 | } |
660 | 0 | *value_out -= num_values; |
661 | 0 | buf += num_values; |
662 | 0 | *consumed += num_values; |
663 | | |
664 | | // We check for `value == UINT16_MAX` because |
665 | | // `*value_out -= num_values` can underflow when |
666 | | // `value == 0` (run_start == 0). In this case `value` |
667 | | // will underflow to UINT16_MAX. |
668 | 0 | if (*value_out < run_start || *value_out == UINT16_MAX) { |
669 | 0 | it->index--; |
670 | 0 | if (it->index >= 0) { |
671 | 0 | *value_out = rc->runs[it->index].value + |
672 | 0 | rc->runs[it->index].length; |
673 | 0 | } else { |
674 | 0 | return false; |
675 | 0 | } |
676 | 0 | } |
677 | 0 | } while (*consumed < count); |
678 | 0 | return true; |
679 | 0 | } |
680 | 0 | default: |
681 | 0 | assert(false); |
682 | 0 | roaring_unreachable; |
683 | 0 | return 0; |
684 | 0 | } |
685 | 0 | } |
686 | | |
687 | | bool container_iterator_read_backward_into_uint64( |
688 | | const container_t *c, uint8_t typecode, roaring_container_iterator_t *it, |
689 | | uint64_t high48, uint64_t *buf, uint32_t count, uint32_t *consumed, |
690 | 0 | uint16_t *value_out) { |
691 | 0 | *consumed = 0; |
692 | 0 | if (count == 0) { |
693 | 0 | return false; |
694 | 0 | } |
695 | 0 | switch (typecode) { |
696 | 0 | case BITSET_CONTAINER_TYPE: { |
697 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
698 | 0 | uint32_t wordindex = it->index / 64; |
699 | 0 | uint64_t word = |
700 | 0 | bc->words[wordindex] & (UINT64_MAX >> (63 - (it->index % 64))); |
701 | 0 | do { |
702 | | // Read set bits. |
703 | 0 | while (word != 0 && *consumed < count) { |
704 | 0 | uint32_t bit = 63 - roaring_leading_zeroes(word); |
705 | 0 | *buf = high48 | (wordindex * 64 + bit); |
706 | 0 | word &= ~(UINT64_C(1) << bit); |
707 | 0 | buf++; |
708 | 0 | (*consumed)++; |
709 | 0 | } |
710 | | // Skip unset bits. |
711 | 0 | while (word == 0 && wordindex > 0) { |
712 | 0 | wordindex--; |
713 | 0 | word = bc->words[wordindex]; |
714 | 0 | } |
715 | 0 | } while (word != 0 && *consumed < count); |
716 | |
|
717 | 0 | if (word != 0) { |
718 | 0 | it->index = |
719 | 0 | wordindex * 64 + (63 - roaring_leading_zeroes(word)); |
720 | 0 | *value_out = it->index; |
721 | 0 | return true; |
722 | 0 | } |
723 | 0 | return false; |
724 | 0 | } |
725 | 0 | case ARRAY_CONTAINER_TYPE: { |
726 | 0 | const array_container_t *ac = const_CAST_array(c); |
727 | 0 | uint32_t num_values = |
728 | 0 | minimum_uint32((uint32_t)(it->index + 1), count); |
729 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
730 | 0 | buf[i] = high48 | ac->array[it->index - i]; |
731 | 0 | } |
732 | 0 | *consumed += num_values; |
733 | 0 | it->index -= num_values; |
734 | 0 | if (it->index >= 0) { |
735 | 0 | *value_out = ac->array[it->index]; |
736 | 0 | return true; |
737 | 0 | } |
738 | 0 | return false; |
739 | 0 | } |
740 | 0 | case RUN_CONTAINER_TYPE: { |
741 | 0 | const run_container_t *rc = const_CAST_run(c); |
742 | 0 | do { |
743 | 0 | uint32_t run_start = rc->runs[it->index].value; |
744 | 0 | uint32_t num_values = minimum_uint32(*value_out - run_start + 1, |
745 | 0 | count - *consumed); |
746 | 0 | for (uint32_t i = 0; i < num_values; i++) { |
747 | 0 | buf[i] = high48 | (*value_out - i); |
748 | 0 | } |
749 | 0 | *value_out -= num_values; |
750 | 0 | buf += num_values; |
751 | 0 | *consumed += num_values; |
752 | |
|
753 | 0 | if (*value_out < run_start || *value_out == UINT16_MAX) { |
754 | 0 | it->index--; |
755 | 0 | if (it->index >= 0) { |
756 | 0 | *value_out = rc->runs[it->index].value + |
757 | 0 | rc->runs[it->index].length; |
758 | 0 | } else { |
759 | 0 | return false; |
760 | 0 | } |
761 | 0 | } |
762 | 0 | } while (*consumed < count); |
763 | 0 | return true; |
764 | 0 | } |
765 | 0 | default: |
766 | 0 | assert(false); |
767 | 0 | roaring_unreachable; |
768 | 0 | return 0; |
769 | 0 | } |
770 | 0 | } |
771 | | |
772 | | bool container_iterator_skip(const container_t *c, uint8_t typecode, |
773 | | roaring_container_iterator_t *it, |
774 | | uint32_t skip_count, uint32_t *consumed_count, |
775 | 0 | uint16_t *value_out) { |
776 | 0 | uint32_t actually_skipped; |
777 | 0 | bool has_value; |
778 | 0 | skip_count = minimum_uint32(skip_count, (uint32_t)UINT16_MAX + 1); |
779 | 0 | switch (typecode) { |
780 | 0 | case ARRAY_CONTAINER_TYPE: { |
781 | 0 | const array_container_t *ac = const_CAST_array(c); |
782 | 0 | actually_skipped = |
783 | 0 | minimum_uint32(ac->cardinality - it->index, skip_count); |
784 | 0 | it->index += actually_skipped; |
785 | 0 | has_value = it->index < ac->cardinality; |
786 | 0 | if (has_value) { |
787 | 0 | *value_out = ac->array[it->index]; |
788 | 0 | } |
789 | 0 | break; |
790 | 0 | } |
791 | 0 | case BITSET_CONTAINER_TYPE: { |
792 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
793 | |
|
794 | 0 | uint32_t remaining_skip = skip_count; |
795 | 0 | uint32_t current_index = it->index; |
796 | 0 | uint64_t word_mask = UINT64_MAX << (current_index % 64); |
797 | 0 | has_value = false; |
798 | |
|
799 | 0 | for (uint32_t word_index = current_index / 64; |
800 | 0 | word_index < BITSET_CONTAINER_SIZE_IN_WORDS; word_index++) { |
801 | 0 | uint64_t word = bc->words[word_index] & word_mask; |
802 | 0 | word_mask = ~0; // Only apply mask for the first word |
803 | |
|
804 | 0 | uint32_t bits_in_word = roaring_hamming(word); |
805 | 0 | if (bits_in_word > remaining_skip) { |
806 | | // Unset the lowest bit `remaining_skip` times |
807 | 0 | for (; remaining_skip > 0; --remaining_skip) { |
808 | 0 | word &= word - 1; |
809 | 0 | } |
810 | 0 | has_value = true; |
811 | 0 | *value_out = it->index = |
812 | 0 | roaring_trailing_zeroes(word) + word_index * 64; |
813 | 0 | break; |
814 | 0 | } |
815 | | // Skip all set bits in this word |
816 | 0 | remaining_skip -= bits_in_word; |
817 | 0 | } |
818 | 0 | actually_skipped = skip_count - remaining_skip; |
819 | 0 | break; |
820 | 0 | } |
821 | 0 | case RUN_CONTAINER_TYPE: { |
822 | 0 | const run_container_t *rc = const_CAST_run(c); |
823 | |
|
824 | 0 | uint16_t current_value = *value_out; |
825 | 0 | uint32_t remaining_skip = skip_count; |
826 | 0 | int32_t run_index; |
827 | | |
828 | | // Process skips by iterating through runs |
829 | 0 | for (run_index = it->index; |
830 | 0 | remaining_skip > 0 && run_index < rc->n_runs; run_index++) { |
831 | | // max value (inclusive) in current run |
832 | 0 | uint32_t run_max_inc = |
833 | 0 | rc->runs[run_index].value + rc->runs[run_index].length; |
834 | | // Max to skip in this run (we can skip from the current value |
835 | | // to the last value in the run, plus one to move past this run) |
836 | 0 | uint32_t max_skip_this_run = run_max_inc - current_value + 1; |
837 | 0 | uint32_t consume = |
838 | 0 | minimum_uint32(remaining_skip, max_skip_this_run); |
839 | 0 | remaining_skip -= consume; |
840 | 0 | if (consume < max_skip_this_run) { |
841 | 0 | current_value += consume; |
842 | 0 | break; |
843 | 0 | } |
844 | | // Skip past the end of this run, to the next if there is one |
845 | 0 | if (run_index + 1 < rc->n_runs) { |
846 | 0 | current_value = rc->runs[run_index + 1].value; |
847 | 0 | } |
848 | 0 | } |
849 | | |
850 | | // Update final state |
851 | 0 | it->index = run_index; |
852 | 0 | actually_skipped = skip_count - remaining_skip; |
853 | 0 | has_value = run_index < rc->n_runs; |
854 | 0 | if (has_value) { |
855 | 0 | *value_out = current_value; |
856 | 0 | } |
857 | 0 | break; |
858 | 0 | } |
859 | 0 | default: |
860 | 0 | assert(false); |
861 | 0 | roaring_unreachable; |
862 | 0 | return false; |
863 | 0 | } |
864 | 0 | *consumed_count = actually_skipped; |
865 | 0 | return has_value; |
866 | 0 | } |
867 | | |
868 | | bool container_iterator_skip_backward(const container_t *c, uint8_t typecode, |
869 | | roaring_container_iterator_t *it, |
870 | | uint32_t skip_count, |
871 | | uint32_t *consumed_count, |
872 | 0 | uint16_t *value_out) { |
873 | 0 | uint32_t actually_skipped; |
874 | 0 | bool has_value; |
875 | 0 | skip_count = minimum_uint32(skip_count, (uint32_t)UINT16_MAX + 1); |
876 | 0 | switch (typecode) { |
877 | 0 | case ARRAY_CONTAINER_TYPE: { |
878 | 0 | const array_container_t *ac = const_CAST_array(c); |
879 | | // Allow skipping back to -1 |
880 | 0 | actually_skipped = minimum_uint32(it->index + 1, skip_count); |
881 | 0 | it->index -= actually_skipped; |
882 | 0 | has_value = it->index >= 0; |
883 | 0 | if (has_value) { |
884 | 0 | *value_out = ac->array[it->index]; |
885 | 0 | } |
886 | 0 | break; |
887 | 0 | } |
888 | 0 | case BITSET_CONTAINER_TYPE: { |
889 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
890 | |
|
891 | 0 | uint32_t remaining_skip = skip_count; |
892 | 0 | uint32_t current_index = it->index; |
893 | 0 | uint64_t word_mask = UINT64_MAX >> (63 - (current_index % 64)); |
894 | 0 | has_value = false; |
895 | | |
896 | | // Start from the word containing current index and go backwards |
897 | 0 | for (int32_t word_index = current_index / 64; word_index >= 0; |
898 | 0 | word_index--) { |
899 | 0 | uint64_t word = bc->words[word_index] & word_mask; |
900 | 0 | word_mask = ~0; // Only apply mask for the first word |
901 | |
|
902 | 0 | uint32_t bits_in_word = roaring_hamming(word); |
903 | 0 | if (bits_in_word > remaining_skip) { |
904 | | // Unset the highest bit `remaining_skip` times |
905 | 0 | for (; remaining_skip > 0; --remaining_skip) { |
906 | 0 | uint64_t high_bit = |
907 | 0 | UINT64_C(1) << (63 - roaring_leading_zeroes(word)); |
908 | | // Clear the highest set bit |
909 | 0 | word &= ~high_bit; |
910 | 0 | } |
911 | 0 | has_value = true; |
912 | 0 | *value_out = it->index = |
913 | 0 | (63 - roaring_leading_zeroes(word)) + word_index * 64; |
914 | 0 | break; |
915 | 0 | } |
916 | | // Skip all set bits in this word |
917 | 0 | remaining_skip -= bits_in_word; |
918 | 0 | } |
919 | 0 | actually_skipped = skip_count - remaining_skip; |
920 | 0 | break; |
921 | 0 | } |
922 | 0 | case RUN_CONTAINER_TYPE: { |
923 | 0 | const run_container_t *rc = const_CAST_run(c); |
924 | |
|
925 | 0 | uint16_t current_value = *value_out; |
926 | 0 | uint32_t remaining_skip = skip_count; |
927 | 0 | int32_t run_index; |
928 | | |
929 | | // Process skips by iterating through runs backwards |
930 | 0 | for (run_index = it->index; remaining_skip > 0 && run_index >= 0; |
931 | 0 | run_index--) { |
932 | | // min value (inclusive) in current run |
933 | 0 | uint32_t run_min_inc = rc->runs[run_index].value; |
934 | | // Max to skip in this run (we can skip from the current value |
935 | | // back to the first value in the run, plus one to move before |
936 | | // this run) |
937 | 0 | uint32_t max_skip_this_run = current_value - run_min_inc + 1; |
938 | 0 | uint32_t consume = |
939 | 0 | minimum_uint32(remaining_skip, max_skip_this_run); |
940 | 0 | remaining_skip -= consume; |
941 | 0 | if (consume < max_skip_this_run) { |
942 | 0 | current_value -= consume; |
943 | 0 | break; |
944 | 0 | } |
945 | | // Skip past the beginning of this run, to the previous if there |
946 | | // is one |
947 | 0 | if (run_index - 1 >= 0) { |
948 | 0 | current_value = rc->runs[run_index - 1].value + |
949 | 0 | rc->runs[run_index - 1].length; |
950 | 0 | } |
951 | 0 | } |
952 | | |
953 | | // Update final state |
954 | 0 | it->index = run_index; |
955 | 0 | actually_skipped = skip_count - remaining_skip; |
956 | 0 | has_value = run_index >= 0; |
957 | 0 | if (has_value) { |
958 | 0 | *value_out = current_value; |
959 | 0 | } |
960 | 0 | break; |
961 | 0 | } |
962 | 0 | default: |
963 | 0 | assert(false); |
964 | 0 | roaring_unreachable; |
965 | 0 | return false; |
966 | 0 | } |
967 | 0 | *consumed_count = actually_skipped; |
968 | 0 | return has_value; |
969 | 0 | } |
970 | | |
971 | | uint16_t container_iterator_find_run_end(const container_t *c, uint8_t typecode, |
972 | | roaring_container_iterator_t *it, |
973 | 0 | uint16_t *value, bool *has_more) { |
974 | 0 | switch (typecode) { |
975 | 0 | case RUN_CONTAINER_TYPE: { |
976 | 0 | const run_container_t *rc = const_CAST_run(c); |
977 | 0 | uint16_t run_end = |
978 | 0 | rc->runs[it->index].value + rc->runs[it->index].length; |
979 | 0 | it->index++; |
980 | 0 | if (it->index < rc->n_runs) { |
981 | 0 | *has_more = true; |
982 | 0 | *value = rc->runs[it->index].value; |
983 | 0 | } else { |
984 | 0 | *has_more = false; |
985 | 0 | } |
986 | 0 | return run_end; |
987 | 0 | } |
988 | 0 | case ARRAY_CONTAINER_TYPE: { |
989 | 0 | const array_container_t *ac = const_CAST_array(c); |
990 | 0 | uint16_t v = *value; |
991 | 0 | while (it->index + 1 < ac->cardinality && |
992 | 0 | ac->array[it->index + 1] == (uint16_t)(v + 1)) { |
993 | 0 | it->index++; |
994 | 0 | v++; |
995 | 0 | } |
996 | 0 | it->index++; |
997 | 0 | if (it->index < ac->cardinality) { |
998 | 0 | *has_more = true; |
999 | 0 | *value = ac->array[it->index]; |
1000 | 0 | } else { |
1001 | 0 | *has_more = false; |
1002 | 0 | } |
1003 | 0 | return v; |
1004 | 0 | } |
1005 | 0 | case BITSET_CONTAINER_TYPE: { |
1006 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
1007 | 0 | uint32_t pos = (uint32_t)*value + 1; |
1008 | 0 | uint16_t run_end; |
1009 | 0 | if (pos >= (1 << 16)) { |
1010 | 0 | *has_more = false; |
1011 | 0 | return UINT16_MAX; |
1012 | 0 | } |
1013 | 0 | uint32_t wordindex = pos / 64; |
1014 | 0 | uint64_t word = ~bc->words[wordindex] & (UINT64_MAX << (pos % 64)); |
1015 | 0 | while (word == 0 && |
1016 | 0 | wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS) { |
1017 | 0 | wordindex++; |
1018 | 0 | word = ~bc->words[wordindex]; |
1019 | 0 | } |
1020 | 0 | if (word != 0) { |
1021 | 0 | run_end = (uint16_t)(wordindex * 64 + |
1022 | 0 | roaring_trailing_zeroes(word) - 1); |
1023 | 0 | } else { |
1024 | 0 | run_end = UINT16_MAX; |
1025 | 0 | } |
1026 | 0 | uint32_t next_pos = (uint32_t)run_end + 1; |
1027 | 0 | if (next_pos >= (1 << 16)) { |
1028 | 0 | *has_more = false; |
1029 | 0 | } else { |
1030 | 0 | wordindex = next_pos / 64; |
1031 | 0 | word = bc->words[wordindex] & (UINT64_MAX << (next_pos % 64)); |
1032 | 0 | while (word == 0 && |
1033 | 0 | wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS) { |
1034 | 0 | wordindex++; |
1035 | 0 | word = bc->words[wordindex]; |
1036 | 0 | } |
1037 | 0 | if (word != 0) { |
1038 | 0 | *has_more = true; |
1039 | 0 | it->index = wordindex * 64 + roaring_trailing_zeroes(word); |
1040 | 0 | *value = (uint16_t)it->index; |
1041 | 0 | } else { |
1042 | 0 | *has_more = false; |
1043 | 0 | } |
1044 | 0 | } |
1045 | 0 | return run_end; |
1046 | 0 | } |
1047 | 0 | default: |
1048 | 0 | assert(false); |
1049 | 0 | roaring_unreachable; |
1050 | 0 | return 0; |
1051 | 0 | } |
1052 | 0 | } |
1053 | | |
1054 | | uint16_t container_iterator_find_run_start(const container_t *c, |
1055 | | uint8_t typecode, |
1056 | | roaring_container_iterator_t *it, |
1057 | 0 | uint16_t *value, bool *has_more) { |
1058 | 0 | switch (typecode) { |
1059 | 0 | case RUN_CONTAINER_TYPE: { |
1060 | 0 | const run_container_t *rc = const_CAST_run(c); |
1061 | 0 | uint16_t run_start = rc->runs[it->index].value; |
1062 | 0 | it->index--; |
1063 | 0 | if (it->index >= 0) { |
1064 | 0 | *has_more = true; |
1065 | 0 | *value = rc->runs[it->index].value + rc->runs[it->index].length; |
1066 | 0 | } else { |
1067 | 0 | *has_more = false; |
1068 | 0 | } |
1069 | 0 | return run_start; |
1070 | 0 | } |
1071 | 0 | case ARRAY_CONTAINER_TYPE: { |
1072 | 0 | const array_container_t *ac = const_CAST_array(c); |
1073 | 0 | uint16_t v = *value; |
1074 | 0 | while (it->index > 0 && |
1075 | 0 | ac->array[it->index - 1] == (uint16_t)(v - 1)) { |
1076 | 0 | it->index--; |
1077 | 0 | v--; |
1078 | 0 | } |
1079 | 0 | it->index--; |
1080 | 0 | if (it->index >= 0) { |
1081 | 0 | *has_more = true; |
1082 | 0 | *value = ac->array[it->index]; |
1083 | 0 | } else { |
1084 | 0 | *has_more = false; |
1085 | 0 | } |
1086 | 0 | return v; |
1087 | 0 | } |
1088 | 0 | case BITSET_CONTAINER_TYPE: { |
1089 | 0 | const bitset_container_t *bc = const_CAST_bitset(c); |
1090 | 0 | if (*value == 0) { |
1091 | 0 | *has_more = false; |
1092 | 0 | return 0; |
1093 | 0 | } |
1094 | 0 | uint32_t pos = (uint32_t)*value - 1; |
1095 | 0 | int32_t wordindex = (int32_t)(pos / 64); |
1096 | 0 | uint64_t word = |
1097 | 0 | ~bc->words[wordindex] & (UINT64_MAX >> (63 - (pos % 64))); |
1098 | 0 | while (word == 0 && --wordindex >= 0) { |
1099 | 0 | word = ~bc->words[wordindex]; |
1100 | 0 | } |
1101 | 0 | uint16_t run_start; |
1102 | 0 | if (word != 0) { |
1103 | 0 | run_start = (uint16_t)(wordindex * 64 + |
1104 | 0 | (63 - roaring_leading_zeroes(word)) + 1); |
1105 | 0 | } else { |
1106 | 0 | run_start = 0; |
1107 | 0 | } |
1108 | 0 | if (run_start == 0) { |
1109 | 0 | *has_more = false; |
1110 | 0 | } else { |
1111 | 0 | int32_t prev_pos = (int32_t)run_start - 1; |
1112 | 0 | wordindex = prev_pos / 64; |
1113 | 0 | word = bc->words[wordindex] & |
1114 | 0 | (UINT64_MAX >> (63 - (prev_pos % 64))); |
1115 | 0 | while (word == 0 && --wordindex >= 0) { |
1116 | 0 | word = bc->words[wordindex]; |
1117 | 0 | } |
1118 | 0 | if (word != 0) { |
1119 | 0 | *has_more = true; |
1120 | 0 | it->index = |
1121 | 0 | wordindex * 64 + (63 - roaring_leading_zeroes(word)); |
1122 | 0 | *value = (uint16_t)it->index; |
1123 | 0 | } else { |
1124 | 0 | *has_more = false; |
1125 | 0 | } |
1126 | 0 | } |
1127 | 0 | return run_start; |
1128 | 0 | } |
1129 | 0 | default: |
1130 | 0 | assert(false); |
1131 | 0 | roaring_unreachable; |
1132 | 0 | return 0; |
1133 | 0 | } |
1134 | 0 | } |
1135 | | |
1136 | | #ifdef __cplusplus |
1137 | | } |
1138 | | } |
1139 | | } // extern "C" { namespace roaring { namespace internal { |
1140 | | #endif |
1141 | | |
1142 | | #undef ROARING_INIT_ROARING_CONTAINER_ITERATOR_T |