[PATCH] D25404: [ADT] Let MapVector handle non-copyable values.

Justin Lebar via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 9 11:14:24 PDT 2016


jlebar created this revision.
jlebar added a reviewer: timshen.
jlebar added a subscriber: llvm-commits.

The keys must still be copyable, because we store two copies of them.


https://reviews.llvm.org/D25404

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


Index: llvm/unittests/ADT/MapVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/MapVectorTest.cpp
+++ llvm/unittests/ADT/MapVectorTest.cpp
@@ -148,6 +148,15 @@
   }
 }
 
+TEST(MapVectorTest, NonCopyable) {
+  MapVector<int, std::unique_ptr<int>> MV;
+  MV.insert(std::make_pair(1, llvm::make_unique<int>(1)));
+  MV.insert(std::make_pair(2, llvm::make_unique<int>(2)));
+
+  ASSERT_EQ(MV.count(1), 1u);
+  ASSERT_EQ(*MV.find(2)->second, 2);
+}
+
 TEST(SmallMapVectorSmallTest, insert_pop) {
   SmallMapVector<int, int, 32> MV;
   std::pair<SmallMapVector<int, int, 32>::iterator, bool> R;
@@ -257,6 +266,15 @@
   }
 }
 
+TEST(SmallMapVectorSmallTest, NonCopyable) {
+  SmallMapVector<int, std::unique_ptr<int>, 8> MV;
+  MV.insert(std::make_pair(1, llvm::make_unique<int>(1)));
+  MV.insert(std::make_pair(2, llvm::make_unique<int>(2)));
+
+  ASSERT_EQ(MV.count(1), 1u);
+  ASSERT_EQ(*MV.find(2)->second, 2);
+}
+
 TEST(SmallMapVectorLargeTest, insert_pop) {
   SmallMapVector<int, int, 1> MV;
   std::pair<SmallMapVector<int, int, 1>::iterator, bool> R;
Index: llvm/include/llvm/ADT/MapVector.h
===================================================================
--- llvm/include/llvm/ADT/MapVector.h
+++ llvm/include/llvm/ADT/MapVector.h
@@ -83,7 +83,10 @@
     return Vector[I].second;
   }
 
+  // Returns a copy of the value.  Only allowed if ValueT is copyable.
   ValueT lookup(const KeyT &Key) const {
+    static_assert(std::is_copy_constructible<ValueT>::value,
+                  "Cannot call lookup() if ValueT is not copyable.");
     typename MapType::const_iterator Pos = Map.find(Key);
     return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
   }
@@ -100,6 +103,20 @@
     return std::make_pair(begin() + I, 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, unsigned> Pair = std::make_pair(KV.first, 0);
+    std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
+    unsigned &I = Result.first->second;
+    if (Result.second) {
+      Vector.push_back(
+          std::make_pair(std::move(KV.first), std::move(KV.second)));
+      I = Vector.size() - 1;
+      return std::make_pair(std::prev(end()), true);
+    }
+    return std::make_pair(begin() + I, false);
+  }
+
   size_type count(const KeyT &Key) const {
     typename MapType::const_iterator Pos = Map.find(Key);
     return Pos == Map.end()? 0 : 1;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25404.74075.patch
Type: text/x-patch
Size: 2533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161009/03627ab0/attachment.bin>


More information about the llvm-commits mailing list