[PATCH] D67668: [ADT] Add StringMap::insert_or_assign

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 22:15:23 PDT 2019


MaskRay updated this revision to Diff 220947.
MaskRay marked an inline comment as done.
MaskRay added a comment.

> You could use a noisy type here to test that only the appropriate operations were executed

Add `struct NoDefaultConstructible {...}`


Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D67668/new/

https://reviews.llvm.org/D67668

Files:
  include/llvm/ADT/StringMap.h
  unittests/ADT/StringMapTest.cpp


Index: unittests/ADT/StringMapTest.cpp
===================================================================
--- unittests/ADT/StringMapTest.cpp
+++ unittests/ADT/StringMapTest.cpp
@@ -75,6 +75,11 @@
 size_t StringMapTest::testKeyLength = sizeof(testKey) - 1;
 const std::string StringMapTest::testKeyStr(testKey);
 
+struct NotDefaultConstructible {
+  NotDefaultConstructible() = delete;
+  int v;
+};
+
 // Empty map tests.
 TEST_F(StringMapTest, EmptyMapTest) {
   assertEmptyMap();
@@ -269,6 +274,17 @@
   EXPECT_EQ(42u, It->second);
 }
 
+TEST_F(StringMapTest, InsertOrAssignTest) {
+  StringMap<NotDefaultConstructible> t(0);
+  auto try1 = t.insert_or_assign("A", NotDefaultConstructible{1});
+  EXPECT_TRUE(try1.second);
+  EXPECT_EQ(1, try1.first->second.v);
+  auto try2 = t.insert_or_assign("A", NotDefaultConstructible{2});
+  EXPECT_FALSE(try2.second);
+  EXPECT_EQ(2, try2.first->second.v);
+  EXPECT_EQ(try1.first, try2.first);
+}
+
 TEST_F(StringMapTest, IterMapKeys) {
   StringMap<int> Map;
   Map["A"] = 1;
Index: include/llvm/ADT/StringMap.h
===================================================================
--- include/llvm/ADT/StringMap.h
+++ include/llvm/ADT/StringMap.h
@@ -391,6 +391,16 @@
     return try_emplace(KV.first, std::move(KV.second));
   }
 
+  /// Inserts an element or assigns to the current element if the key already
+  /// exists. The return type is the same as try_emplace.
+  template <typename V>
+  std::pair<iterator, bool> insert_or_assign(StringRef Key, V &&Val) {
+    auto Ret = try_emplace(Key, std::forward<V>(Val));
+    if (!Ret.second)
+      Ret.first->second = std::forward<V>(Val);
+    return Ret;
+  }
+
   /// Emplace a new element for the specified key into the map if the key isn't
   /// already in the map. The bool component of the returned pair is true
   /// if and only if the insertion takes place, and the iterator component of


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67668.220947.patch
Type: text/x-patch
Size: 1901 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190920/64e68231/attachment.bin>


More information about the llvm-commits mailing list