Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions llvm/include/llvm/ADT/MapVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
110 changes: 110 additions & 0 deletions llvm/unittests/ADT/MapVectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -423,3 +532,4 @@ TEST(SmallMapVectorLargeTest, iteration_test) {
count--;
}
}
} // namespace