This commit is contained in:
Gregory Burd 2024-04-29 17:48:01 -04:00
parent 1ab6c90257
commit 90c81c7363
2 changed files with 23 additions and 24 deletions

View file

@ -1269,11 +1269,12 @@ sparsemap_merge(sparsemap_t *map, sparsemap_t *other)
/* Foreach destination chunk, is there a source chunk with the same starting
offset? If so, if so let's merge them. */
for (size_t i = 0; i < dst_count + src_count; i++) {
next_iteration:;
__sm_chunk_t dst_chunk;
sm_idx_t dst_start = *(sm_idx_t *)dst;
__sm_chunk_map_init(&dst_chunk, dst + sizeof(sm_idx_t));
size_t j = 0;
if (i < dst_count) {
size_t j = 0;
while (j < src_count) {
__sm_chunk_t src_chunk;
sm_idx_t src_start = *(sm_idx_t *)src;
@ -1281,16 +1282,15 @@ sparsemap_merge(sparsemap_t *map, sparsemap_t *other)
if (src_start == dst_start) {
/* Chunks overlap, merge them. */
__sm_chunk_map_merge(map, src_start, src_chunk);
dst = SM_NEXT_CHUNK_ADDR(dst);
src = SM_NEXT_CHUNK_ADDR(src);
j = SIZE_MAX;
break;
}
dst = SM_NEXT_CHUNK_ADDR(dst);
i++;
goto next_iteration;
}
src = __sm_get_chunk_map_data(other, 0);
j++;
}
}
if (j != SIZE_MAX) {
__sm_chunk_t src_chunk;
sm_idx_t src_start = *(sm_idx_t *)src;
__sm_chunk_map_init(&src_chunk, src + sizeof(sm_idx_t));
@ -1307,7 +1307,6 @@ sparsemap_merge(sparsemap_t *map, sparsemap_t *other)
src = SM_NEXT_CHUNK_ADDR(src);
}
}
}
void
sparsemap_split(sparsemap_t *map, sparsemap_idx_t offset, sparsemap_t *other)

View file

@ -648,22 +648,22 @@ test_api_merge(const MunitParameter params[], void *data)
sparsemap_clear(map);
sparsemap_clear(other);
sparsemap_set(map, 0, true);
sparsemap_set(other, 1, true);
sparsemap_set(other, 2049, true);
sparsemap_set(map, 2050, true);
sparsemap_set(other, 4097, true);
sparsemap_set(other, 8193, true);
sparsemap_set(other, 8194, true);
sparsemap_merge(map,other);
assert_true(sparsemap_is_set(map, 0));
assert_true(sparsemap_is_set(map, 1));
assert_true(sparsemap_is_set(map, 2049));
assert_true(sparsemap_is_set(map, 2050));
assert_true(sparsemap_is_set(map, 4097));
assert_true(sparsemap_is_set(map, 8193));
assert_true(sparsemap_is_set(map, 8194));
for (int i = 0; i < 10000; i++) {
if (i == 0 || i == 1 || i == 2049 || i == 8193 || i == 8194)
if (i == 2050 || i == 1 || i == 2049 || i == 4097 || i == 8193)
continue;
else
assert_false(sparsemap_is_set(map, i));