[llvm] r283764 - [ADT] Let MapVector handle non-copyable values.
Justin Lebar via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 10 09:26:00 PDT 2016
Author: jlebar
Date: Mon Oct 10 11:25:59 2016
New Revision: 283764
URL: http://llvm.org/viewvc/llvm-project?rev=283764&view=rev
Log:
[ADT] Let MapVector handle non-copyable values.
Summary: The keys must still be copyable, because we store two copies of them.
Reviewers: timshen
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D25404
Modified:
llvm/trunk/include/llvm/ADT/MapVector.h
llvm/trunk/unittests/ADT/MapVectorTest.cpp
Modified: llvm/trunk/include/llvm/ADT/MapVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/MapVector.h?rev=283764&r1=283763&r2=283764&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/MapVector.h (original)
+++ llvm/trunk/include/llvm/ADT/MapVector.h Mon Oct 10 11:25:59 2016
@@ -83,7 +83,10 @@ public:
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;
}
@@ -97,6 +100,19 @@ public:
I = Vector.size() - 1;
return std::make_pair(std::prev(end()), true);
}
+ 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::move(KV));
+ I = Vector.size() - 1;
+ return std::make_pair(std::prev(end()), true);
+ }
return std::make_pair(begin() + I, false);
}
Modified: llvm/trunk/unittests/ADT/MapVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/MapVectorTest.cpp?rev=283764&r1=283763&r2=283764&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/MapVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/MapVectorTest.cpp Mon Oct 10 11:25:59 2016
@@ -148,6 +148,15 @@ TEST(MapVectorTest, iteration_test) {
}
}
+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, iteration_
}
}
+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;
More information about the llvm-commits
mailing list