Skip to content

[ADT] Add DenseMap::insert_range #133600

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 3 commits into from
Mar 30, 2025
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
6 changes: 6 additions & 0 deletions llvm/include/llvm/ADT/DenseMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -302,6 +303,11 @@ class DenseMapBase : public DebugEpochBase {
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));
Expand Down
22 changes: 22 additions & 0 deletions llvm/unittests/ADT/DenseMapTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,28 @@ TEST(DenseMapCustomTest, EqualityComparison) {
EXPECT_NE(M1, M3);
}

TEST(DenseMapCustomTest, InsertRange) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also have a test for SmallDenseSet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in llvm/unittests/ADT/DenseSetTest.cpp

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant SmallDenseMap

Copy link
Contributor Author

@vbvictor vbvictor Mar 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added SmallDenseMap, should I leave test for SmallDenseSet as is because it was absent in DenseSetTest.cpp?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I think it's fine either way.

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
Expand Down
7 changes: 7 additions & 0 deletions llvm/unittests/ADT/DenseSetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down