diff --git a/include/sg14/plf_hive.h b/include/sg14/plf_hive.h index 3706534..bea1b6f 100644 --- a/include/sg14/plf_hive.h +++ b/include/sg14/plf_hive.h @@ -1912,13 +1912,16 @@ class hive { GroupPtr g = groups_with_erasures_; assert(g->free_list_head != std::numeric_limits::max()); skipfield_type skipblock_length = g->skipfield(g->free_list_head); - if (skipblock_length >= n) { + if (skipblock_length > n) { callback_fill_skipblock(n, cb, g); assert_invariants(); return; } else { callback_fill_skipblock(skipblock_length, cb, g); n -= skipblock_length; + if (n == 0) { + return; + } } } assert_invariants(); diff --git a/test/hive_test.cpp b/test/hive_test.cpp index cd5ea71..bdb00b5 100644 --- a/test/hive_test.cpp +++ b/test/hive_test.cpp @@ -2811,4 +2811,19 @@ TEST(hive, PmrCorrectAllocAwareCtors) } #endif // __cpp_lib_memory_resource >= 201603 +TEST(hive, RangeInsertRegressionTest) +{ + auto h = plf::hive(100, 42); + h.erase(std::next(h.begin())); + h.erase(std::next(h.begin())); + EXPECT_EQ(h.size(), 98); + h.insert(2, 42); // 42 copies of "42" + EXPECT_EQ(h.size(), 100); + int sum = 0; + for (int i : h) { + sum += i; + } + EXPECT_EQ(sum, 4200); +} + #endif // __cplusplus >= 201703