Skip to content

Commit 9924846

Browse files
committed
simplifying inlining
1 parent f451e37 commit 9924846

File tree

7 files changed

+138
-173
lines changed

7 files changed

+138
-173
lines changed

amalgamation.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ echo "Creating ${AMAL_C}..."
162162

163163
echo "#include \"roaring.h\" /* include public API definitions */"
164164

165-
for h in ${ALL_PRIVATE_H} src/roaring_internal_inline.h ${ALL_PRIVATE_C}; do
165+
for h in ${ALL_PRIVATE_H} ${ALL_PRIVATE_C}; do
166166
dofile $h
167167
done
168168
} > "${DESTINATION}/${AMAL_C}"

include/roaring/containers/containers.h

Lines changed: 120 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,15 +2434,131 @@ roaring_container_iterator_t container_init_iterator_last(const container_t *c,
24342434
* Moves the iterator to the next entry. Returns true and sets `value` if a
24352435
* value is present.
24362436
*/
2437-
bool container_iterator_next(const container_t *c, uint8_t typecode,
2438-
roaring_container_iterator_t *it, uint16_t *value);
2437+
inline bool container_iterator_next(const container_t *c, uint8_t typecode,
2438+
roaring_container_iterator_t *it,
2439+
uint16_t *value) {
2440+
switch (typecode) {
2441+
case BITSET_CONTAINER_TYPE: {
2442+
const bitset_container_t *bc = const_CAST_bitset(c);
2443+
it->index++;
2444+
2445+
uint32_t wordindex = it->index / 64;
2446+
if (wordindex >= BITSET_CONTAINER_SIZE_IN_WORDS) {
2447+
return false;
2448+
}
2449+
2450+
uint64_t word =
2451+
bc->words[wordindex] & (UINT64_MAX << (it->index % 64));
2452+
// next part could be optimized/simplified
2453+
while (word == 0 &&
2454+
(wordindex + 1 < BITSET_CONTAINER_SIZE_IN_WORDS)) {
2455+
wordindex++;
2456+
word = bc->words[wordindex];
2457+
}
2458+
if (word != 0) {
2459+
it->index = wordindex * 64 + roaring_trailing_zeroes(word);
2460+
*value = it->index;
2461+
return true;
2462+
}
2463+
return false;
2464+
}
2465+
case ARRAY_CONTAINER_TYPE: {
2466+
const array_container_t *ac = const_CAST_array(c);
2467+
it->index++;
2468+
if (it->index < ac->cardinality) {
2469+
*value = ac->array[it->index];
2470+
return true;
2471+
}
2472+
return false;
2473+
}
2474+
case RUN_CONTAINER_TYPE: {
2475+
if (*value == UINT16_MAX) { // Avoid overflow to zero
2476+
return false;
2477+
}
2478+
2479+
const run_container_t *rc = const_CAST_run(c);
2480+
uint32_t limit =
2481+
rc->runs[it->index].value + rc->runs[it->index].length;
2482+
if (*value < limit) {
2483+
(*value)++;
2484+
return true;
2485+
}
2486+
2487+
it->index++;
2488+
if (it->index < rc->n_runs) {
2489+
*value = rc->runs[it->index].value;
2490+
return true;
2491+
}
2492+
return false;
2493+
}
2494+
default:
2495+
assert(false);
2496+
roaring_unreachable;
2497+
return false;
2498+
}
2499+
}
24392500

24402501
/**
24412502
* Moves the iterator to the previous entry. Returns true and sets `value` if a
24422503
* value is present.
24432504
*/
2444-
bool container_iterator_prev(const container_t *c, uint8_t typecode,
2445-
roaring_container_iterator_t *it, uint16_t *value);
2505+
inline bool container_iterator_prev(const container_t *c, uint8_t typecode,
2506+
roaring_container_iterator_t *it,
2507+
uint16_t *value) {
2508+
switch (typecode) {
2509+
case BITSET_CONTAINER_TYPE: {
2510+
if (--it->index < 0) {
2511+
return false;
2512+
}
2513+
2514+
const bitset_container_t *bc = const_CAST_bitset(c);
2515+
int32_t wordindex = it->index / 64;
2516+
uint64_t word =
2517+
bc->words[wordindex] & (UINT64_MAX >> (63 - (it->index % 64)));
2518+
2519+
while (word == 0 && --wordindex >= 0) {
2520+
word = bc->words[wordindex];
2521+
}
2522+
if (word == 0) {
2523+
return false;
2524+
}
2525+
2526+
it->index = (wordindex * 64) + (63 - roaring_leading_zeroes(word));
2527+
*value = it->index;
2528+
return true;
2529+
}
2530+
case ARRAY_CONTAINER_TYPE: {
2531+
if (--it->index < 0) {
2532+
return false;
2533+
}
2534+
const array_container_t *ac = const_CAST_array(c);
2535+
*value = ac->array[it->index];
2536+
return true;
2537+
}
2538+
case RUN_CONTAINER_TYPE: {
2539+
if (*value == 0) {
2540+
return false;
2541+
}
2542+
2543+
const run_container_t *rc = const_CAST_run(c);
2544+
(*value)--;
2545+
if (*value >= rc->runs[it->index].value) {
2546+
return true;
2547+
}
2548+
2549+
if (--it->index < 0) {
2550+
return false;
2551+
}
2552+
2553+
*value = rc->runs[it->index].value + rc->runs[it->index].length;
2554+
return true;
2555+
}
2556+
default:
2557+
assert(false);
2558+
roaring_unreachable;
2559+
return false;
2560+
}
2561+
}
24462562

24472563
/**
24482564
* Moves the iterator to the smallest entry that is greater than or equal to

include/roaring/roaring.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t x);
464464
/**
465465
* Check if value is present
466466
*/
467-
inline bool roaring_bitmap_contains(const roaring_bitmap_t *r,
468-
uint32_t val) {
467+
inline bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) {
469468
// For performance reasons, this function is inline and uses internal
470469
// functions directly.
471470
#ifdef __cplusplus

src/containers/containers.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <roaring/containers/containers.h>
22
#include <roaring/memory.h>
33

4-
#include "roaring_internal_inline.h"
5-
64
#ifdef __cplusplus
75
extern "C" {
86
// In Windows MSVC C++ compiler, (type){init} does not compile,
@@ -45,6 +43,14 @@ extern inline container_t *container_iandnot(container_t *c1, uint8_t type1,
4543
uint8_t type2,
4644
uint8_t *result_type);
4745

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+
4854
void container_free(container_t *c, uint8_t type) {
4955
switch (type) {
5056
case BITSET_CONTAINER_TYPE:
@@ -371,18 +377,6 @@ roaring_container_iterator_t container_init_iterator_last(const container_t *c,
371377
}
372378
}
373379

374-
bool container_iterator_next(const container_t *c, uint8_t typecode,
375-
roaring_container_iterator_t *it,
376-
uint16_t *value) {
377-
return container_iterator_next_inline(c, typecode, it, value);
378-
}
379-
380-
bool container_iterator_prev(const container_t *c, uint8_t typecode,
381-
roaring_container_iterator_t *it,
382-
uint16_t *value) {
383-
return container_iterator_prev_inline(c, typecode, it, value);
384-
}
385-
386380
bool container_iterator_lower_bound(const container_t *c, uint8_t typecode,
387381
roaring_container_iterator_t *it,
388382
uint16_t *value_out, uint16_t val) {

src/roaring.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include <roaring/containers/containers.h>
1515
#include <roaring/roaring_array.h>
1616

17-
#include "roaring_internal_inline.h"
18-
1917
#ifdef __cplusplus
2018
using namespace ::roaring::internal;
2119

@@ -1832,8 +1830,8 @@ bool roaring_uint32_iterator_advance(roaring_uint32_iterator_t *it) {
18321830
return (it->has_value = loadfirstvalue(it));
18331831
}
18341832
uint16_t low16 = (uint16_t)it->current_value;
1835-
if (container_iterator_next_inline(it->container, it->typecode,
1836-
&it->container_it, &low16)) {
1833+
if (container_iterator_next(it->container, it->typecode, &it->container_it,
1834+
&low16)) {
18371835
it->current_value = it->highbits | low16;
18381836
return (it->has_value = true);
18391837
}
@@ -1850,8 +1848,8 @@ bool roaring_uint32_iterator_previous(roaring_uint32_iterator_t *it) {
18501848
return (it->has_value = loadlastvalue(it));
18511849
}
18521850
uint16_t low16 = (uint16_t)it->current_value;
1853-
if (container_iterator_prev_inline(it->container, it->typecode,
1854-
&it->container_it, &low16)) {
1851+
if (container_iterator_prev(it->container, it->typecode, &it->container_it,
1852+
&low16)) {
18551853
it->current_value = it->highbits | low16;
18561854
return (it->has_value = true);
18571855
}

src/roaring64.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
// containers.h last to avoid conflict with ROARING_CONTAINER_T.
1818
#include <roaring/containers/containers.h>
1919

20-
#include "roaring_internal_inline.h"
21-
2220
#define CROARING_ALIGN_BUF(buf, alignment) \
2321
(char *)(((uintptr_t)(buf) + ((alignment)-1)) & \
2422
(ptrdiff_t)(~((alignment)-1)))
@@ -2610,9 +2608,8 @@ bool roaring64_iterator_advance(roaring64_iterator_t *it) {
26102608
}
26112609
leaf_t leaf = (leaf_t)*it->art_it.value;
26122610
uint16_t low16 = (uint16_t)it->value;
2613-
if (container_iterator_next_inline(get_container(it->r, leaf),
2614-
get_typecode(leaf), &it->container_it,
2615-
&low16)) {
2611+
if (container_iterator_next(get_container(it->r, leaf), get_typecode(leaf),
2612+
&it->container_it, &low16)) {
26162613
it->value = it->high48 | low16;
26172614
return (it->has_value = true);
26182615
}
@@ -2634,9 +2631,8 @@ bool roaring64_iterator_previous(roaring64_iterator_t *it) {
26342631
}
26352632
leaf_t leaf = (leaf_t)*it->art_it.value;
26362633
uint16_t low16 = (uint16_t)it->value;
2637-
if (container_iterator_prev_inline(get_container(it->r, leaf),
2638-
get_typecode(leaf), &it->container_it,
2639-
&low16)) {
2634+
if (container_iterator_prev(get_container(it->r, leaf), get_typecode(leaf),
2635+
&it->container_it, &low16)) {
26402636
it->value = it->high48 | low16;
26412637
return (it->has_value = true);
26422638
}

0 commit comments

Comments
 (0)