This commit is contained in:
Gregory Burd 2024-04-09 22:12:07 -04:00
parent 1ea0871506
commit 8079276343
2 changed files with 18 additions and 24 deletions

View file

@ -426,10 +426,10 @@ __sm_chunk_map_select(__sm_chunk_t *map, size_t n, ssize_t *pnew_n)
} }
/** /**
* Counts the set bits in the range [first, last] inclusive. * Counts the set bits in the range [first, idx] inclusive.
*/ */
static size_t static size_t
__sm_chunk_map_rank(__sm_chunk_t *map, size_t last, size_t *after) __sm_chunk_map_rank(__sm_chunk_t *map, size_t idx, size_t *after)
{ {
size_t ret = 0; size_t ret = 0;
@ -441,50 +441,42 @@ __sm_chunk_map_rank(__sm_chunk_t *map, size_t last, size_t *after)
continue; continue;
} }
if (flags == SM_PAYLOAD_ZEROS) { if (flags == SM_PAYLOAD_ZEROS) {
if (last > SM_BITS_PER_VECTOR) { if (idx > SM_BITS_PER_VECTOR) {
if (*after > SM_BITS_PER_VECTOR) { if (*after > SM_BITS_PER_VECTOR) {
*after = *after - SM_BITS_PER_VECTOR; *after = *after - SM_BITS_PER_VECTOR;
} else { } else {
last -= SM_BITS_PER_VECTOR - *after; idx -= SM_BITS_PER_VECTOR - *after;
*after = 0; *after = 0;
} }
} else { } else {
return (ret); return (ret);
} }
} else if (flags == SM_PAYLOAD_ONES) { } else if (flags == SM_PAYLOAD_ONES) {
if (last > SM_BITS_PER_VECTOR) { if (idx > SM_BITS_PER_VECTOR) {
if (*after > SM_BITS_PER_VECTOR) { if (*after > SM_BITS_PER_VECTOR) {
*after = *after - SM_BITS_PER_VECTOR; *after = *after - SM_BITS_PER_VECTOR;
} else { } else {
last -= SM_BITS_PER_VECTOR - *after; idx -= SM_BITS_PER_VECTOR - *after;
if (*after == 0) { if (*after == 0) {
ret += SM_BITS_PER_VECTOR; ret += SM_BITS_PER_VECTOR;
} }
*after = 0; *after = 0;
} }
} else { } else {
return (ret + last); return (ret + idx);
} }
} else if (flags == SM_PAYLOAD_MIXED) { } else if (flags == SM_PAYLOAD_MIXED) {
sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)]; sm_bitvec_t w = map->m_data[1 + __sm_chunk_map_get_position(map, i * SM_FLAGS_PER_INDEX_BYTE + j)];
if (last > SM_BITS_PER_VECTOR) { uint64_t after_mask = *after > 63 ? 0 : (((uint64_t)1 << *after) - 1);
last -= SM_BITS_PER_VECTOR; if (idx > SM_BITS_PER_VECTOR) {
/* Create a mask for the range of bits except those we don't want to consider. */ idx -= SM_BITS_PER_VECTOR;
uint64_t mask = ~(UINT64_MAX >> (SM_BITS_PER_VECTOR - *after)); ret += popcountll(w & after_mask);
uint64_t mw = w & mask; *after = (*after > SM_BITS_PER_VECTOR) ? *after - SM_BITS_PER_VECTOR : 0;
ret += popcountll(mw);
if (*after > SM_BITS_PER_VECTOR) {
*after -= SM_BITS_PER_VECTOR;
} else {
*after = 0;
}
} else { } else {
uint64_t mask; /* Create a mask for the range between after and idx inclusive [*after, idx]. */
/* Create a mask for the range between after and last inclusive [*after, last]. */ uint64_t idx_mask = ((uint64_t)1 << (idx + 1)) - 1;
mask = ((uint64_t)1 << (last + 1)) - 1 - (((uint64_t)1 << *after) - 1); ret += popcountll(w & (idx_mask - after_mask));
*after -= (*after > last) ? last : *after; *after = *after > idx ? *after - idx : 0;
uint64_t mw = w & mask;
ret += popcountll(mw);
return (ret); return (ret);
} }
} }

View file

@ -597,6 +597,8 @@ test_api_span(const MunitParameter params[], void *data)
for (int j = 1; j < amt / 10; j++) { for (int j = 1; j < amt / 10; j++) {
sparsemap_clear(map); sparsemap_clear(map);
placed_at = create_sequential_set_in_empty_map(map, amt, j); placed_at = create_sequential_set_in_empty_map(map, amt, j);
//logf("i = %d, j = %d\tplaced_at %d\n", i, j, placed_at);
//whats_set(map, 5000);
located_at = sparsemap_span(map, 0, j); located_at = sparsemap_span(map, 0, j);
assert_true(located_at == placed_at); assert_true(located_at == placed_at);
} }