[ADT] Add from_range constructor for (Small)DenseMap (#153515)

This follows how we support range construction for (Small)DenseSet.
This commit is contained in:
Jakub Kuderski 2025-08-14 08:53:52 -04:00 committed by GitHub
parent e3dcdb64ee
commit 1633e0ba8b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 4 deletions

View File

@ -18,6 +18,7 @@
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/EpochTracker.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/Support/AlignOf.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
@ -811,10 +812,12 @@ public:
this->insert(I, E);
}
DenseMap(std::initializer_list<typename BaseT::value_type> Vals) {
init(Vals.size());
this->insert(Vals.begin(), Vals.end());
}
template <typename RangeT>
DenseMap(llvm::from_range_t, const RangeT &Range)
: DenseMap(adl_begin(Range), adl_end(Range)) {}
DenseMap(std::initializer_list<typename BaseT::value_type> Vals)
: DenseMap(Vals.begin(), Vals.end()) {}
~DenseMap() {
this->destroyAll();
@ -985,6 +988,10 @@ public:
this->insert(I, E);
}
template <typename RangeT>
SmallDenseMap(llvm::from_range_t, const RangeT &Range)
: SmallDenseMap(adl_begin(Range), adl_end(Range)) {}
SmallDenseMap(std::initializer_list<typename BaseT::value_type> Vals)
: SmallDenseMap(Vals.begin(), Vals.end()) {}

View File

@ -10,6 +10,7 @@
#include "CountCopyAndMove.h"
#include "llvm/ADT/DenseMapInfo.h"
#include "llvm/ADT/DenseMapInfoVariant.h"
#include "llvm/ADT/STLForwardCompat.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/StringRef.h"
#include "gmock/gmock.h"
@ -249,6 +250,25 @@ TYPED_TEST(DenseMapTest, CopyConstructorNotSmallTest) {
EXPECT_EQ(this->getValue(Key), copyMap[this->getKey(Key)]);
}
// Test range constructors.
TYPED_TEST(DenseMapTest, RangeConstructorTest) {
using KeyAndValue =
std::pair<typename TypeParam::key_type, typename TypeParam::mapped_type>;
KeyAndValue PlainArray[] = {{this->getKey(0), this->getValue(0)},
{this->getKey(1), this->getValue(1)}};
TypeParam MapFromRange(llvm::from_range, PlainArray);
EXPECT_EQ(2u, MapFromRange.size());
EXPECT_EQ(this->getValue(0), MapFromRange[this->getKey(0)]);
EXPECT_EQ(this->getValue(1), MapFromRange[this->getKey(1)]);
TypeParam MapFromInitList({{this->getKey(0), this->getValue(1)},
{this->getKey(1), this->getValue(2)}});
EXPECT_EQ(2u, MapFromInitList.size());
EXPECT_EQ(this->getValue(1), MapFromInitList[this->getKey(0)]);
EXPECT_EQ(this->getValue(2), MapFromInitList[this->getKey(1)]);
}
// Test copying from a default-constructed map.
TYPED_TEST(DenseMapTest, CopyConstructorFromDefaultTest) {
TypeParam copyMap(this->Map);
@ -726,6 +746,15 @@ TEST(DenseMapCustomTest, FindAsTest) {
EXPECT_TRUE(map.find_as("d") == map.end());
}
TEST(DenseMapCustomTest, SmallDenseMapFromRange) {
std::pair<int, StringRef> PlainArray[] = {{0, "0"}, {1, "1"}, {2, "2"}};
SmallDenseMap<int, StringRef> M(llvm::from_range, PlainArray);
EXPECT_EQ(3u, M.size());
using testing::Pair;
EXPECT_THAT(M, testing::UnorderedElementsAre(Pair(0, "0"), Pair(1, "1"),
Pair(2, "2")));
}
TEST(DenseMapCustomTest, SmallDenseMapInitializerList) {
SmallDenseMap<int, int> M = {{0, 0}, {0, 1}, {1, 2}};
EXPECT_EQ(2u, M.size());