[PATCH] D106363: [ADT] Add initializer_list constructor to SmallDenseMap
Jakub Kuderski via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 20 07:27:21 PDT 2021
kuhar created this revision.
kuhar added reviewers: MaskRay, dexonsmith, dblaikie.
kuhar added a project: LLVM.
Herald added a reviewer: grosser.
kuhar requested review of this revision.
Make it easier to initialize small maps inline. Note that DenseMap already has an initializer_list constructor.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D106363
Files:
llvm/include/llvm/ADT/DenseMap.h
llvm/unittests/ADT/DenseMapTest.cpp
Index: llvm/unittests/ADT/DenseMapTest.cpp
===================================================================
--- llvm/unittests/ADT/DenseMapTest.cpp
+++ llvm/unittests/ADT/DenseMapTest.cpp
@@ -547,6 +547,15 @@
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; }
Index: llvm/include/llvm/ADT/DenseMap.h
===================================================================
--- llvm/include/llvm/ADT/DenseMap.h
+++ llvm/include/llvm/ADT/DenseMap.h
@@ -924,6 +924,9 @@
this->insert(I, E);
}
+ SmallDenseMap(std::initializer_list<typename BaseT::value_type> Vals)
+ : SmallDenseMap(Vals.begin(), Vals.end()) {}
+
~SmallDenseMap() {
this->destroyAll();
deallocateBuckets();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106363.360122.patch
Type: text/x-patch
Size: 1103 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210720/95e076f1/attachment.bin>
More information about the llvm-commits
mailing list