[llvm] 89ecb80 - MapVector: add C++17-style try_emplace and insert_or_assign (#71969)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 12 20:45:40 PST 2023


Author: Fangrui Song
Date: 2023-11-12T20:45:37-08:00
New Revision: 89ecb8001afc38c2225f1fe7ab0d5fb729f25537

URL: https://github.com/llvm/llvm-project/commit/89ecb8001afc38c2225f1fe7ab0d5fb729f25537
DIFF: https://github.com/llvm/llvm-project/commit/89ecb8001afc38c2225f1fe7ab0d5fb729f25537.diff

LOG: MapVector: add C++17-style try_emplace and insert_or_assign (#71969)

Similar to https://wg21.link/n4279

For example, insert_or_assign can be used to simplify
CodeGenModule::AddDeferredUnusedCoverageMapping in
clang/lib/CodeGen/CodeGenModule.cpp

Added: 
    

Modified: 
    llvm/include/llvm/ADT/MapVector.h
    llvm/unittests/ADT/MapVectorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index c45779c0ce8e0cf..c11617a81c97d55 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -114,29 +114,50 @@ class MapVector {
     return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
   }
 
-  std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
-    std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
-    std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
-    auto &I = Result.first->second;
-    if (Result.second) {
-      Vector.push_back(std::make_pair(KV.first, KV.second));
-      I = Vector.size() - 1;
+  template <typename... Ts>
+  std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
+    auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
+    if (Inserted) {
+      It->second = Vector.size();
+      Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
+                          std::forward_as_tuple(std::forward<Ts>(Args)...));
       return std::make_pair(std::prev(end()), true);
     }
-    return std::make_pair(begin() + I, false);
+    return std::make_pair(begin() + It->second, false);
   }
-
-  std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
-    // Copy KV.first into the map, then move it into the vector.
-    std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
-    std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
-    auto &I = Result.first->second;
-    if (Result.second) {
-      Vector.push_back(std::move(KV));
-      I = Vector.size() - 1;
+  template <typename... Ts>
+  std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
+    auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
+    if (Inserted) {
+      It->second = Vector.size();
+      Vector.emplace_back(std::piecewise_construct,
+                          std::forward_as_tuple(std::move(Key)),
+                          std::forward_as_tuple(std::forward<Ts>(Args)...));
       return std::make_pair(std::prev(end()), true);
     }
-    return std::make_pair(begin() + I, false);
+    return std::make_pair(begin() + It->second, false);
+  }
+
+  std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
+    return try_emplace(KV.first, KV.second);
+  }
+  std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
+    return try_emplace(std::move(KV.first), std::move(KV.second));
+  }
+
+  template <typename V>
+  std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
+    auto Ret = try_emplace(Key, std::forward<V>(Val));
+    if (!Ret.second)
+      Ret.first->second = std::forward<V>(Val);
+    return Ret;
+  }
+  template <typename V>
+  std::pair<iterator, bool> insert_or_assign(KeyT &&Key, V &&Val) {
+    auto Ret = try_emplace(std::move(Key), std::forward<V>(Val));
+    if (!Ret.second)
+      Ret.first->second = std::forward<V>(Val);
+    return Ret;
   }
 
   bool contains(const KeyT &Key) const { return Map.find(Key) != Map.end(); }

diff  --git a/llvm/unittests/ADT/MapVectorTest.cpp b/llvm/unittests/ADT/MapVectorTest.cpp
index 1a371cbfba81e8e..e0f11b60a0223da 100644
--- a/llvm/unittests/ADT/MapVectorTest.cpp
+++ b/llvm/unittests/ADT/MapVectorTest.cpp
@@ -9,10 +9,38 @@
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/iterator_range.h"
 #include "gtest/gtest.h"
+#include <memory>
 #include <utility>
 
 using namespace llvm;
 
+namespace {
+struct CountCopyAndMove {
+  CountCopyAndMove() = default;
+  CountCopyAndMove(const CountCopyAndMove &) { copy = 1; }
+  CountCopyAndMove(CountCopyAndMove &&) { move = 1; }
+  void operator=(const CountCopyAndMove &) { ++copy; }
+  void operator=(CountCopyAndMove &&) { ++move; }
+  int copy = 0;
+  int move = 0;
+};
+
+struct A : CountCopyAndMove {
+  A(int v) : v(v) {}
+  int v;
+};
+} // namespace
+
+namespace llvm {
+template <> struct DenseMapInfo<A> {
+  static inline A getEmptyKey() { return 0x7fffffff; }
+  static inline A getTombstoneKey() { return -0x7fffffff - 1; }
+  static unsigned getHashValue(const A &Val) { return (unsigned)(Val.v * 37U); }
+  static bool isEqual(const A &LHS, const A &RHS) { return LHS.v == RHS.v; }
+};
+} // namespace llvm
+
+namespace {
 TEST(MapVectorTest, swap) {
   MapVector<int, int> MV1, MV2;
   std::pair<MapVector<int, int>::iterator, bool> R;
@@ -79,6 +107,87 @@ TEST(MapVectorTest, insert_pop) {
   EXPECT_EQ(MV[4], 7);
 }
 
+TEST(MapVectorTest, try_emplace) {
+  struct AAndU {
+    A a;
+    std::unique_ptr<int> b;
+    AAndU(A a, std::unique_ptr<int> b) : a(a), b(std::move(b)) {}
+  };
+  MapVector<A, AAndU> mv;
+
+  A zero(0);
+  auto try0 = mv.try_emplace(zero, zero, nullptr);
+  EXPECT_TRUE(try0.second);
+  EXPECT_EQ(0, try0.first->second.a.v);
+  EXPECT_EQ(1, try0.first->second.a.copy);
+  EXPECT_EQ(0, try0.first->second.a.move);
+
+  auto try1 = mv.try_emplace(zero, zero, nullptr);
+  EXPECT_FALSE(try1.second);
+  EXPECT_EQ(0, try1.first->second.a.v);
+  EXPECT_EQ(1, try1.first->second.a.copy);
+  EXPECT_EQ(0, try1.first->second.a.move);
+
+  EXPECT_EQ(try0.first, try1.first);
+  EXPECT_EQ(1, try1.first->first.copy);
+  EXPECT_EQ(0, try1.first->first.move);
+
+  A two(2);
+  auto try2 = mv.try_emplace(2, std::move(two), std::make_unique<int>(2));
+  EXPECT_TRUE(try2.second);
+  EXPECT_EQ(2, try2.first->second.a.v);
+  EXPECT_EQ(0, try2.first->second.a.move);
+
+  std::unique_ptr<int> p(new int(3));
+  auto try3 = mv.try_emplace(std::move(two), 3, std::move(p));
+  EXPECT_FALSE(try3.second);
+  EXPECT_EQ(2, try3.first->second.a.v);
+  EXPECT_EQ(1, try3.first->second.a.copy);
+  EXPECT_EQ(0, try3.first->second.a.move);
+
+  EXPECT_EQ(try2.first, try3.first);
+  EXPECT_EQ(0, try3.first->first.copy);
+  EXPECT_EQ(1, try3.first->first.move);
+  EXPECT_NE(nullptr, p);
+}
+
+TEST(MapVectorTest, insert_or_assign) {
+  MapVector<A, A> mv;
+
+  A zero(0);
+  auto try0 = mv.insert_or_assign(zero, zero);
+  EXPECT_TRUE(try0.second);
+  EXPECT_EQ(0, try0.first->second.v);
+  EXPECT_EQ(1, try0.first->second.copy);
+  EXPECT_EQ(0, try0.first->second.move);
+
+  auto try1 = mv.insert_or_assign(zero, zero);
+  EXPECT_FALSE(try1.second);
+  EXPECT_EQ(0, try1.first->second.v);
+  EXPECT_EQ(2, try1.first->second.copy);
+  EXPECT_EQ(0, try1.first->second.move);
+
+  EXPECT_EQ(try0.first, try1.first);
+  EXPECT_EQ(1, try1.first->first.copy);
+  EXPECT_EQ(0, try1.first->first.move);
+
+  A two(2);
+  auto try2 = mv.try_emplace(2, std::move(two));
+  EXPECT_TRUE(try2.second);
+  EXPECT_EQ(2, try2.first->second.v);
+  EXPECT_EQ(1, try2.first->second.move);
+
+  auto try3 = mv.insert_or_assign(std::move(two), 3);
+  EXPECT_FALSE(try3.second);
+  EXPECT_EQ(3, try3.first->second.v);
+  EXPECT_EQ(0, try3.first->second.copy);
+  EXPECT_EQ(2, try3.first->second.move);
+
+  EXPECT_EQ(try2.first, try3.first);
+  EXPECT_EQ(0, try3.first->first.copy);
+  EXPECT_EQ(1, try3.first->first.move);
+}
+
 TEST(MapVectorTest, erase) {
   MapVector<int, int> MV;
 
@@ -423,3 +532,4 @@ TEST(SmallMapVectorLargeTest, iteration_test) {
     count--;
   }
 }
+} // namespace


        


More information about the llvm-commits mailing list