Skip to content

[DenseMap] Introduce emplace_or_assign #138886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions llvm/include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,22 @@ class DenseMapBase : public DebugEpochBase {
return Ret;
}

template <typename... Ts>
std::pair<iterator, bool> emplace_or_assign(const KeyT &Key, Ts &&...Args) {
auto Ret = try_emplace(Key, std::forward<Ts>(Args)...);
if (!Ret.second)
Ret.first->second = ValueT(std::forward<Ts>(Args)...);
return Ret;
}

template <typename... Ts>
std::pair<iterator, bool> emplace_or_assign(KeyT &&Key, Ts &&...Args) {
auto Ret = try_emplace(std::move(Key), std::forward<Ts>(Args)...);
if (!Ret.second)
Ret.first->second = ValueT(std::forward<Ts>(Args)...);
return Ret;
}

bool erase(const KeyT &Val) {
BucketT *TheBucket = doFind(Val);
if (!TheBucket)
Expand Down
35 changes: 35 additions & 0 deletions llvm/unittests/ADT/DenseMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,41 @@ TEST(DenseMapCustomTest, InsertOrAssignTest) {
EXPECT_EQ(1, CountCopyAndMove::MoveAssignments);
}

TEST(DenseMapCustomTest, EmplaceOrAssign) {
DenseMap<int, CountCopyAndMove> Map;

CountCopyAndMove::ResetCounts();
auto Try0 = Map.emplace_or_assign(3, 3);
EXPECT_TRUE(Try0.second);
EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
EXPECT_EQ(0, CountCopyAndMove::TotalMoves());
EXPECT_EQ(1, CountCopyAndMove::ValueConstructions);

CountCopyAndMove::ResetCounts();
auto Try1 = Map.emplace_or_assign(3, 4);
EXPECT_FALSE(Try1.second);
EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
EXPECT_EQ(1, CountCopyAndMove::ValueConstructions);
EXPECT_EQ(0, CountCopyAndMove::MoveConstructions);
EXPECT_EQ(1, CountCopyAndMove::MoveAssignments);

int Key = 5;
CountCopyAndMove::ResetCounts();
auto Try2 = Map.emplace_or_assign(Key, 3);
EXPECT_TRUE(Try2.second);
EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
EXPECT_EQ(0, CountCopyAndMove::TotalMoves());
EXPECT_EQ(1, CountCopyAndMove::ValueConstructions);

CountCopyAndMove::ResetCounts();
auto Try3 = Map.emplace_or_assign(Key, 4);
EXPECT_FALSE(Try3.second);
EXPECT_EQ(0, CountCopyAndMove::TotalCopies());
EXPECT_EQ(1, CountCopyAndMove::ValueConstructions);
EXPECT_EQ(0, CountCopyAndMove::MoveConstructions);
EXPECT_EQ(1, CountCopyAndMove::MoveAssignments);
}

// Make sure DenseMap works with StringRef keys.
TEST(DenseMapCustomTest, StringRefTest) {
DenseMap<StringRef, int> M;
Expand Down