From 90c81c73630e992235dd5eb7982f41b7eb8d7799 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Mon, 29 Apr 2024 17:48:01 -0400 Subject: [PATCH] WIP --- src/sparsemap.c | 37 ++++++++++++++++++------------------- tests/test.c | 10 +++++----- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/sparsemap.c b/src/sparsemap.c index 874c7cf..4a1fc55 100644 --- a/src/sparsemap.c +++ b/src/sparsemap.c @@ -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); } } diff --git a/tests/test.c b/tests/test.c index 6494d31..ab4e834 100644 --- a/tests/test.c +++ b/tests/test.c @@ -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));