diff options
author | Benjamin Kramer <benny.kra@googlemail.com> | 2016-07-21 13:37:53 +0000 |
---|---|---|
committer | Benjamin Kramer <benny.kra@googlemail.com> | 2016-07-21 13:37:53 +0000 |
commit | 857754a1cb73b3b71aef847ba8d7e983d4d77e2e (patch) | |
tree | a38891319c9da94e5940f9b8ac067246dac47789 /llvm/unittests/ADT/DenseMapTest.cpp | |
parent | eab3d367538030e2fd4feb0c94795e1bdb566451 (diff) | |
download | llvm-857754a1cb73b3b71aef847ba8d7e983d4d77e2e.zip llvm-857754a1cb73b3b71aef847ba8d7e983d4d77e2e.tar.gz llvm-857754a1cb73b3b71aef847ba8d7e983d4d77e2e.tar.bz2 |
[DenseMap] Add a C++17-style try_emplace method.
This provides an elegant pattern to solve the "construct if not in map
already" problem we have many times in LLVM. Without try_emplace we
either have to rely on a sentinel value (nullptr) or do two lookups.
llvm-svn: 276277
Diffstat (limited to 'llvm/unittests/ADT/DenseMapTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/DenseMapTest.cpp | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp index db00f8c..cbf1a44 100644 --- a/llvm/unittests/ADT/DenseMapTest.cpp +++ b/llvm/unittests/ADT/DenseMapTest.cpp @@ -619,4 +619,14 @@ TEST(DenseMapCustomTest, SmallDenseMapGrowTest) { EXPECT_TRUE(map.find(32) == map.end()); } +TEST(DenseMapCustomTest, TryEmplaceTest) { + DenseMap<int, std::unique_ptr<int>> Map; + std::unique_ptr<int> P(new int(2)); + auto Try1 = Map.try_emplace(0, new int(1)); + EXPECT_TRUE(Try1.second); + auto Try2 = Map.try_emplace(0, std::move(P)); + EXPECT_FALSE(Try2.second); + EXPECT_EQ(Try1.first, Try2.first); + EXPECT_NE(nullptr, P); +} } |