Skip to content

Commit

Permalink
Merge pull request #2 from qnzhou/main
Browse files Browse the repository at this point in the history
Iterator bug fix
  • Loading branch information
SergeyMakeev authored Jan 10, 2024
2 parents 4d6c2ba + 4a411e0 commit b8ac8eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
31 changes: 27 additions & 4 deletions SlotMapTest01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,34 @@ TEST(SlotMapTest, ConstantGetter)
TEST(SlotMapTest, IteratorsComparison)
{
dod::slot_map<std::string> slotMap;
EXPECT_FALSE(slotMap.begin() == slotMap.end());
EXPECT_TRUE(slotMap.begin() != slotMap.end());
EXPECT_FALSE(slotMap.begin() != slotMap.end());
EXPECT_TRUE(slotMap.begin() == slotMap.end());

EXPECT_FALSE(slotMap.items().begin() == slotMap.items().end());
EXPECT_TRUE(slotMap.items().begin() != slotMap.items().end());
EXPECT_FALSE(slotMap.items().begin() != slotMap.items().end());
EXPECT_TRUE(slotMap.items().begin() == slotMap.items().end());
}

TEST(SlotMapTest, Iterator)
{
dod::slot_map<int> slotMap;
ASSERT_EQ(0u, slotMap.size());

auto id1 = slotMap.emplace(12);
auto id2 = slotMap.emplace(13);

ASSERT_TRUE(slotMap.has_key(id1));
ASSERT_TRUE(slotMap.has_key(id2));

slotMap.erase(id2);
ASSERT_FALSE(slotMap.has_key(id2));

for (const auto& [key, val] : slotMap.items()) {
EXPECT_TRUE(slotMap.has_key(key));
EXPECT_EQ(key, id1);
}
for (const auto& val : slotMap) {
EXPECT_EQ(val, 12);
}
}

std::atomic<int> ctorCount = 0;
Expand Down
11 changes: 7 additions & 4 deletions slot_map/slot_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,7 @@ template <typename T, typename TKeyType = slot_map_key64<T>, size_t PAGESIZE = 4
do
{
currentIndex++;
} while (currentIndex < slotMap->getMaxValidIndex() && slotMap->isTombstone(currentIndex));
} while (currentIndex <= slotMap->getMaxValidIndex() && slotMap->isTombstone(currentIndex));
return *this;
}

Expand All @@ -1172,8 +1172,10 @@ template <typename T, typename TKeyType = slot_map_key64<T>, size_t PAGESIZE = 4

const_values_iterator begin() const noexcept
{
if (pages.empty()) return end();

size_type index = 0;
while (index < getMaxValidIndex() && isTombstone(index))
while (index <= getMaxValidIndex() && isTombstone(index))
{
index++;
}
Expand Down Expand Up @@ -1261,7 +1263,7 @@ template <typename T, typename TKeyType = slot_map_key64<T>, size_t PAGESIZE = 4
do
{
currentIndex++;
} while (currentIndex < slotMap->getMaxValidIndex() && slotMap->isTombstone(currentIndex));
} while (currentIndex <= slotMap->getMaxValidIndex() && slotMap->isTombstone(currentIndex));
return *this;
}

Expand Down Expand Up @@ -1292,8 +1294,9 @@ template <typename T, typename TKeyType = slot_map_key64<T>, size_t PAGESIZE = 4

const_kv_iterator begin() const noexcept
{
if (slotMap->pages.empty()) return end();
size_type index = 0;
while (index < slotMap->getMaxValidIndex() && slotMap->isTombstone(index))
while (index <= slotMap->getMaxValidIndex() && slotMap->isTombstone(index))
{
index++;
}
Expand Down

0 comments on commit b8ac8eb

Please sign in to comment.