Skip to content

Commit 797694b

Browse files
authored
Merge pull request #30645 from gottesmm/pr-61aed6aa9d21ec70e2c27e6e16e3c5edf2daed0b
[blotmapvector] Add support for try_emplace.
2 parents e06661d + c5f6038 commit 797694b

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

include/swift/Basic/BlotMapVector.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,40 @@ class BlotMapVector {
6666
return Vector[Pair.first->second].getValue().second;
6767
}
6868

69-
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &InsertPair) {
70-
auto Pair = Map.insert(std::make_pair(InsertPair.first, size_t(0)));
71-
if (Pair.second) {
72-
size_t Num = Vector.size();
73-
Pair.first->second = Num;
74-
Vector.push_back(InsertPair);
75-
return std::make_pair(Vector.begin() + Num, true);
69+
template <typename... Ts>
70+
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&... Args) {
71+
auto Pair = Map.insert(std::make_pair(std::move(Key), size_t(0)));
72+
if (!Pair.second) {
73+
return std::make_pair(Vector.begin() + Pair.first->second, false);
74+
}
75+
76+
size_t Num = Vector.size();
77+
Pair.first->second = Num;
78+
Vector.emplace_back(
79+
std::make_pair(Pair.first->first, ValueT(std::forward<Ts>(Args)...)));
80+
return std::make_pair(Vector.begin() + Num, true);
81+
}
82+
83+
template <typename... Ts>
84+
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&... Args) {
85+
auto Pair = Map.insert(std::make_pair(std::move(Key), size_t(0)));
86+
if (!Pair.second) {
87+
return std::make_pair(Vector.begin() + Pair.first->second, false);
7688
}
77-
return std::make_pair(Vector.begin() + Pair.first->second, false);
89+
90+
size_t Num = Vector.size();
91+
Pair.first->second = Num;
92+
Vector.emplace_back(
93+
std::make_pair(Pair.first->first, ValueT(std::forward<Ts>(Args)...)));
94+
return std::make_pair(Vector.begin() + Num, true);
95+
}
96+
97+
std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &InsertPair) {
98+
return try_emplace(InsertPair.first, InsertPair.second);
99+
}
100+
101+
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &InsertPair) {
102+
return try_emplace(InsertPair.first, InsertPair.second);
78103
}
79104

80105
iterator find(const KeyT &Key) {

unittests/Basic/BlotMapVectorTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,19 @@ TYPED_TEST(BlotMapVectorTest, InsertTest) {
464464
this->NumExpectedLiveTesters = 1;
465465
}
466466

467+
// Test try_emplace() method
468+
TYPED_TEST(BlotMapVectorTest, TryEmplaceTest) {
469+
this->Map.try_emplace(this->getKey(), this->getValue());
470+
EXPECT_EQ(1u, this->Map.size());
471+
EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
472+
EXPECT_EQ(1u, this->Map.size());
473+
this->Map.try_emplace(this->getKey(), this->getValue());
474+
EXPECT_EQ(1u, this->Map.size());
475+
EXPECT_EQ(this->getValue(), this->Map[this->getKey()]);
476+
EXPECT_EQ(1u, this->Map.size());
477+
this->NumExpectedLiveTesters = 1;
478+
}
479+
467480
// Test copy constructor method
468481
TYPED_TEST(BlotMapVectorTest, CopyConstructorTest) {
469482
this->Map[this->getKey()] = this->getValue();

0 commit comments

Comments
 (0)