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;
}
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);
+ }
+
size_type count(const KeyT &Key) const {
typename MapType::const_iterator Pos = Map.find(Key);
return Pos == Map.end()? 0 : 1;
}
}
+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;
}
}
+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;