[llvm] 3c3165c - [ADT] Add initializer_list constructor to SmallDenseMap
Jakub Kuderski via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 21 06:32:51 PDT 2021
Author: Jakub Kuderski
Date: 2021-07-21T09:32:16-04:00
New Revision: 3c3165cfa0a4ff4f6c687b4ab4c141803d37009d
URL: https://github.com/llvm/llvm-project/commit/3c3165cfa0a4ff4f6c687b4ab4c141803d37009d
DIFF: https://github.com/llvm/llvm-project/commit/3c3165cfa0a4ff4f6c687b4ab4c141803d37009d.diff
LOG: [ADT] Add initializer_list constructor to SmallDenseMap
Make it easier to initialize small maps inline. Note that DenseMap already has an initializer_list constructor.
Reviewed By: dblaikie
Differential Revision: https://reviews.llvm.org/D106363
Added:
Modified:
llvm/include/llvm/ADT/DenseMap.h
llvm/unittests/ADT/DenseMapTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index ce0b05db840c..595eabd0ffb4 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -924,6 +924,9 @@ class SmallDenseMap
this->insert(I, E);
}
+ SmallDenseMap(std::initializer_list<typename BaseT::value_type> Vals)
+ : SmallDenseMap(Vals.begin(), Vals.end()) {}
+
~SmallDenseMap() {
this->destroyAll();
deallocateBuckets();
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index 58eac978157e..352270adec0f 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -547,6 +547,15 @@ TEST(DenseMapCustomTest, FindAsTest) {
EXPECT_TRUE(map.find_as("d") == map.end());
}
+TEST(DenseMapCustomTest, SmallDenseMapInitializerList) {
+ SmallDenseMap<int, int> M = {{0, 0}, {0, 1}, {1, 2}};
+ EXPECT_EQ(2u, M.size());
+ EXPECT_EQ(1u, M.count(0));
+ EXPECT_EQ(0, M[0]);
+ EXPECT_EQ(1u, M.count(1));
+ EXPECT_EQ(2, M[1]);
+}
+
struct ContiguousDenseMapInfo {
static inline unsigned getEmptyKey() { return ~0; }
static inline unsigned getTombstoneKey() { return ~0U - 1; }
More information about the llvm-commits
mailing list