Skip to content

Commit 89ecb80

Browse files
authored
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
1 parent 868007a commit 89ecb80

File tree

2 files changed

+149
-18
lines changed

2 files changed

+149
-18
lines changed

llvm/include/llvm/ADT/MapVector.h

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,29 +114,50 @@ class MapVector {
114114
return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
115115
}
116116

117-
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
118-
std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
119-
std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
120-
auto &I = Result.first->second;
121-
if (Result.second) {
122-
Vector.push_back(std::make_pair(KV.first, KV.second));
123-
I = Vector.size() - 1;
117+
template <typename... Ts>
118+
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
119+
auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
120+
if (Inserted) {
121+
It->second = Vector.size();
122+
Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
123+
std::forward_as_tuple(std::forward<Ts>(Args)...));
124124
return std::make_pair(std::prev(end()), true);
125125
}
126-
return std::make_pair(begin() + I, false);
126+
return std::make_pair(begin() + It->second, false);
127127
}
128-
129-
std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
130-
// Copy KV.first into the map, then move it into the vector.
131-
std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0);
132-
std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
133-
auto &I = Result.first->second;
134-
if (Result.second) {
135-
Vector.push_back(std::move(KV));
136-
I = Vector.size() - 1;
128+
template <typename... Ts>
129+
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
130+
auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
131+
if (Inserted) {
132+
It->second = Vector.size();
133+
Vector.emplace_back(std::piecewise_construct,
134+
std::forward_as_tuple(std::move(Key)),
135+
std::forward_as_tuple(std::forward<Ts>(Args)...));
137136
return std::make_pair(std::prev(end()), true);
138137
}
139-
return std::make_pair(begin() + I, false);
138+
return std::make_pair(begin() + It->second, false);
139+
}
140+
141+
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
142+
return try_emplace(KV.first, KV.second);
143+
}
144+
std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
145+
return try_emplace(std::move(KV.first), std::move(KV.second));
146+
}
147+
148+
template <typename V>
149+
std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
150+
auto Ret = try_emplace(Key, std::forward<V>(Val));
151+
if (!Ret.second)
152+
Ret.first->second = std::forward<V>(Val);
153+
return Ret;
154+
}
155+
template <typename V>
156+
std::pair<iterator, bool> insert_or_assign(KeyT &&Key, V &&Val) {
157+
auto Ret = try_emplace(std::move(Key), std::forward<V>(Val));
158+
if (!Ret.second)
159+
Ret.first->second = std::forward<V>(Val);
160+
return Ret;
140161
}
141162

142163
bool contains(const KeyT &Key) const { return Map.find(Key) != Map.end(); }

llvm/unittests/ADT/MapVectorTest.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,38 @@
99
#include "llvm/ADT/MapVector.h"
1010
#include "llvm/ADT/iterator_range.h"
1111
#include "gtest/gtest.h"
12+
#include <memory>
1213
#include <utility>
1314

1415
using namespace llvm;
1516

17+
namespace {
18+
struct CountCopyAndMove {
19+
CountCopyAndMove() = default;
20+
CountCopyAndMove(const CountCopyAndMove &) { copy = 1; }
21+
CountCopyAndMove(CountCopyAndMove &&) { move = 1; }
22+
void operator=(const CountCopyAndMove &) { ++copy; }
23+
void operator=(CountCopyAndMove &&) { ++move; }
24+
int copy = 0;
25+
int move = 0;
26+
};
27+
28+
struct A : CountCopyAndMove {
29+
A(int v) : v(v) {}
30+
int v;
31+
};
32+
} // namespace
33+
34+
namespace llvm {
35+
template <> struct DenseMapInfo<A> {
36+
static inline A getEmptyKey() { return 0x7fffffff; }
37+
static inline A getTombstoneKey() { return -0x7fffffff - 1; }
38+
static unsigned getHashValue(const A &Val) { return (unsigned)(Val.v * 37U); }
39+
static bool isEqual(const A &LHS, const A &RHS) { return LHS.v == RHS.v; }
40+
};
41+
} // namespace llvm
42+
43+
namespace {
1644
TEST(MapVectorTest, swap) {
1745
MapVector<int, int> MV1, MV2;
1846
std::pair<MapVector<int, int>::iterator, bool> R;
@@ -79,6 +107,87 @@ TEST(MapVectorTest, insert_pop) {
79107
EXPECT_EQ(MV[4], 7);
80108
}
81109

110+
TEST(MapVectorTest, try_emplace) {
111+
struct AAndU {
112+
A a;
113+
std::unique_ptr<int> b;
114+
AAndU(A a, std::unique_ptr<int> b) : a(a), b(std::move(b)) {}
115+
};
116+
MapVector<A, AAndU> mv;
117+
118+
A zero(0);
119+
auto try0 = mv.try_emplace(zero, zero, nullptr);
120+
EXPECT_TRUE(try0.second);
121+
EXPECT_EQ(0, try0.first->second.a.v);
122+
EXPECT_EQ(1, try0.first->second.a.copy);
123+
EXPECT_EQ(0, try0.first->second.a.move);
124+
125+
auto try1 = mv.try_emplace(zero, zero, nullptr);
126+
EXPECT_FALSE(try1.second);
127+
EXPECT_EQ(0, try1.first->second.a.v);
128+
EXPECT_EQ(1, try1.first->second.a.copy);
129+
EXPECT_EQ(0, try1.first->second.a.move);
130+
131+
EXPECT_EQ(try0.first, try1.first);
132+
EXPECT_EQ(1, try1.first->first.copy);
133+
EXPECT_EQ(0, try1.first->first.move);
134+
135+
A two(2);
136+
auto try2 = mv.try_emplace(2, std::move(two), std::make_unique<int>(2));
137+
EXPECT_TRUE(try2.second);
138+
EXPECT_EQ(2, try2.first->second.a.v);
139+
EXPECT_EQ(0, try2.first->second.a.move);
140+
141+
std::unique_ptr<int> p(new int(3));
142+
auto try3 = mv.try_emplace(std::move(two), 3, std::move(p));
143+
EXPECT_FALSE(try3.second);
144+
EXPECT_EQ(2, try3.first->second.a.v);
145+
EXPECT_EQ(1, try3.first->second.a.copy);
146+
EXPECT_EQ(0, try3.first->second.a.move);
147+
148+
EXPECT_EQ(try2.first, try3.first);
149+
EXPECT_EQ(0, try3.first->first.copy);
150+
EXPECT_EQ(1, try3.first->first.move);
151+
EXPECT_NE(nullptr, p);
152+
}
153+
154+
TEST(MapVectorTest, insert_or_assign) {
155+
MapVector<A, A> mv;
156+
157+
A zero(0);
158+
auto try0 = mv.insert_or_assign(zero, zero);
159+
EXPECT_TRUE(try0.second);
160+
EXPECT_EQ(0, try0.first->second.v);
161+
EXPECT_EQ(1, try0.first->second.copy);
162+
EXPECT_EQ(0, try0.first->second.move);
163+
164+
auto try1 = mv.insert_or_assign(zero, zero);
165+
EXPECT_FALSE(try1.second);
166+
EXPECT_EQ(0, try1.first->second.v);
167+
EXPECT_EQ(2, try1.first->second.copy);
168+
EXPECT_EQ(0, try1.first->second.move);
169+
170+
EXPECT_EQ(try0.first, try1.first);
171+
EXPECT_EQ(1, try1.first->first.copy);
172+
EXPECT_EQ(0, try1.first->first.move);
173+
174+
A two(2);
175+
auto try2 = mv.try_emplace(2, std::move(two));
176+
EXPECT_TRUE(try2.second);
177+
EXPECT_EQ(2, try2.first->second.v);
178+
EXPECT_EQ(1, try2.first->second.move);
179+
180+
auto try3 = mv.insert_or_assign(std::move(two), 3);
181+
EXPECT_FALSE(try3.second);
182+
EXPECT_EQ(3, try3.first->second.v);
183+
EXPECT_EQ(0, try3.first->second.copy);
184+
EXPECT_EQ(2, try3.first->second.move);
185+
186+
EXPECT_EQ(try2.first, try3.first);
187+
EXPECT_EQ(0, try3.first->first.copy);
188+
EXPECT_EQ(1, try3.first->first.move);
189+
}
190+
82191
TEST(MapVectorTest, erase) {
83192
MapVector<int, int> MV;
84193

@@ -423,3 +532,4 @@ TEST(SmallMapVectorLargeTest, iteration_test) {
423532
count--;
424533
}
425534
}
535+
} // namespace

0 commit comments

Comments
 (0)