From 89be05da5c7aee4645933ace1b1194642591d3a5 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Fri, 1 May 2020 09:57:18 -0700 Subject: [PATCH] [ADT] Add DenseSetImpl(begin, end) --- include/llvm/ADT/DenseSet.h | 6 ++++++ unittests/ADT/DenseSetTest.cpp | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/include/llvm/ADT/DenseSet.h b/include/llvm/ADT/DenseSet.h index 9afb715ae1d..07edc3d8e4e 100644 --- a/include/llvm/ADT/DenseSet.h +++ b/include/llvm/ADT/DenseSet.h @@ -66,6 +66,12 @@ public: explicit DenseSetImpl(unsigned InitialReserve = 0) : TheMap(InitialReserve) {} + template + DenseSetImpl(const InputIt &I, const InputIt &E) + : DenseSetImpl(PowerOf2Ceil(std::distance(I, E))) { + insert(I, E); + } + DenseSetImpl(std::initializer_list Elems) : DenseSetImpl(PowerOf2Ceil(Elems.size())) { insert(Elems.begin(), Elems.end()); diff --git a/unittests/ADT/DenseSetTest.cpp b/unittests/ADT/DenseSetTest.cpp index b080aa7f0c6..556bd46537d 100644 --- a/unittests/ADT/DenseSetTest.cpp +++ b/unittests/ADT/DenseSetTest.cpp @@ -70,6 +70,15 @@ typedef ::testing::Types, DenseSetTestTypes; TYPED_TEST_CASE(DenseSetTest, DenseSetTestTypes); +TYPED_TEST(DenseSetTest, Constructor) { + constexpr unsigned a[] = {1, 2, 4}; + TypeParam set(std::begin(a), std::end(a)); + EXPECT_EQ(3u, set.size()); + EXPECT_EQ(1u, set.count(1)); + EXPECT_EQ(1u, set.count(2)); + EXPECT_EQ(1u, set.count(4)); +} + TYPED_TEST(DenseSetTest, InitializerList) { TypeParam set({1, 2, 1, 4}); EXPECT_EQ(3u, set.size());