[ADT] Add DenseMap::insert_range (#133600)
This PR add `DenseMap::insert_range` to `DenseMap` for consistency with existing `DenseSet::insert_range`, `SmallSet::insert_range` and `std::map::insert_range`.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
#ifndef LLVM_ADT_DENSEMAP_H
|
||||
#define LLVM_ADT_DENSEMAP_H
|
||||
|
||||
#include "llvm/ADT/ADL.h"
|
||||
#include "llvm/ADT/DenseMapInfo.h"
|
||||
#include "llvm/ADT/EpochTracker.h"
|
||||
#include "llvm/Support/AlignOf.h"
|
||||
@@ -302,6 +303,11 @@ public:
|
||||
insert(*I);
|
||||
}
|
||||
|
||||
/// Inserts range of 'std::pair<KeyT, ValueT>' values into the map.
|
||||
template <typename Range> void insert_range(Range &&R) {
|
||||
insert(adl_begin(R), adl_end(R));
|
||||
}
|
||||
|
||||
template <typename V>
|
||||
std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
|
||||
auto Ret = try_emplace(Key, std::forward<V>(Val));
|
||||
|
||||
@@ -379,6 +379,28 @@ TEST(DenseMapCustomTest, EqualityComparison) {
|
||||
EXPECT_NE(M1, M3);
|
||||
}
|
||||
|
||||
TEST(DenseMapCustomTest, InsertRange) {
|
||||
DenseMap<int, int> M;
|
||||
|
||||
std::pair<int, int> InputVals[3] = {{0, 0}, {0, 1}, {1, 2}};
|
||||
M.insert_range(InputVals);
|
||||
|
||||
EXPECT_EQ(M.size(), 2u);
|
||||
EXPECT_THAT(M, testing::UnorderedElementsAre(testing::Pair(0, 0),
|
||||
testing::Pair(1, 2)));
|
||||
}
|
||||
|
||||
TEST(SmallDenseMapCustomTest, InsertRange) {
|
||||
SmallDenseMap<int, int> M;
|
||||
|
||||
std::pair<int, int> InputVals[3] = {{0, 0}, {0, 1}, {1, 2}};
|
||||
M.insert_range(InputVals);
|
||||
|
||||
EXPECT_EQ(M.size(), 2u);
|
||||
EXPECT_THAT(M, testing::UnorderedElementsAre(testing::Pair(0, 0),
|
||||
testing::Pair(1, 2)));
|
||||
}
|
||||
|
||||
// Test for the default minimum size of a DenseMap
|
||||
TEST(DenseMapCustomTest, DefaultMinReservedSizeTest) {
|
||||
// IF THIS VALUE CHANGE, please update InitialSizeTest, InitFromIterator, and
|
||||
|
||||
@@ -58,6 +58,13 @@ TEST(DenseSetTest, InsertRange) {
|
||||
EXPECT_THAT(set, ::testing::UnorderedElementsAre(1, 2, 3));
|
||||
}
|
||||
|
||||
TEST(SmallDenseSetTest, InsertRange) {
|
||||
llvm::SmallDenseSet<unsigned> set;
|
||||
constexpr unsigned Args[] = {9, 7, 8};
|
||||
set.insert_range(Args);
|
||||
EXPECT_THAT(set, ::testing::UnorderedElementsAre(7, 8, 9));
|
||||
}
|
||||
|
||||
struct TestDenseSetInfo {
|
||||
static inline unsigned getEmptyKey() { return ~0; }
|
||||
static inline unsigned getTombstoneKey() { return ~0U - 1; }
|
||||
|
||||
Reference in New Issue
Block a user