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,31 +1282,29 @@ 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;
}
dst = SM_NEXT_CHUNK_ADDR(dst);
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));
__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));
/* Copy the chunk data to the buffer. */
dst_tail = __sm_get_chunk_map_end(map);
*(sm_idx_t *)dst_tail = src_start;
size_t size = __sm_chunk_map_get_size(&src_chunk);
memcpy(dst_tail + sizeof(sm_idx_t), src + sizeof(sm_idx_t), size);
/* Copy the chunk data to the buffer. */
dst_tail = __sm_get_chunk_map_end(map);
*(sm_idx_t *)dst_tail = src_start;
size_t size = __sm_chunk_map_get_size(&src_chunk);
memcpy(dst_tail + sizeof(sm_idx_t), src + sizeof(sm_idx_t), size);
/* Update the chunk count and data_used. */
map->m_data_used = 0;
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) + 1);
src = SM_NEXT_CHUNK_ADDR(src);
}
/* Update the chunk count and data_used. */
map->m_data_used = 0;
__sm_set_chunk_map_count(map, __sm_get_chunk_map_count(map) + 1);
src = SM_NEXT_CHUNK_ADDR(src);
}
}

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));