From fab4c9e9df1aeb33b55cfcfa174fac8d61df96fd Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Mon, 27 Sep 2010 15:48:37 +0000 Subject: [PATCH] Add an all() method to BitVector, for testing whether all bits are set. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@114830 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/BitVector.h | 6 ++++++ include/llvm/ADT/SmallBitVector.h | 7 +++++++ unittests/ADT/BitVectorTest.cpp | 7 +++++++ unittests/ADT/SmallBitVectorTest.cpp | 7 +++++++ 4 files changed, 27 insertions(+) diff --git a/include/llvm/ADT/BitVector.h b/include/llvm/ADT/BitVector.h index 9dcb9e106f2..1940fd3900e 100644 --- a/include/llvm/ADT/BitVector.h +++ b/include/llvm/ADT/BitVector.h @@ -127,6 +127,12 @@ public: return false; } + /// all - Returns true if all bits are set. + bool all() const { + // TODO: Optimize this. + return count() == size(); + } + /// none - Returns true if none of the bits are set. bool none() const { return !any(); diff --git a/include/llvm/ADT/SmallBitVector.h b/include/llvm/ADT/SmallBitVector.h index 3441d0a90c9..b15b3ee0418 100644 --- a/include/llvm/ADT/SmallBitVector.h +++ b/include/llvm/ADT/SmallBitVector.h @@ -187,6 +187,13 @@ public: return getPointer()->any(); } + /// all - Returns true if all bits are set. + bool all() const { + if (isSmall()) + return getSmallBits() == (uintptr_t(1) << getSmallSize()) - 1; + return getPointer()->all(); + } + /// none - Returns true if none of the bits are set. bool none() const { if (isSmall()) diff --git a/unittests/ADT/BitVectorTest.cpp b/unittests/ADT/BitVectorTest.cpp index a9fc133c72e..fa663121a8a 100644 --- a/unittests/ADT/BitVectorTest.cpp +++ b/unittests/ADT/BitVectorTest.cpp @@ -22,6 +22,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Vec.count()); EXPECT_EQ(0U, Vec.size()); EXPECT_FALSE(Vec.any()); + EXPECT_TRUE(Vec.all()); EXPECT_TRUE(Vec.none()); EXPECT_TRUE(Vec.empty()); @@ -29,6 +30,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(5U, Vec.count()); EXPECT_EQ(5U, Vec.size()); EXPECT_TRUE(Vec.any()); + EXPECT_TRUE(Vec.all()); EXPECT_FALSE(Vec.none()); EXPECT_FALSE(Vec.empty()); @@ -36,6 +38,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(5U, Vec.count()); EXPECT_EQ(11U, Vec.size()); EXPECT_TRUE(Vec.any()); + EXPECT_FALSE(Vec.all()); EXPECT_FALSE(Vec.none()); EXPECT_FALSE(Vec.empty()); @@ -43,6 +46,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(6U, Inv.count()); EXPECT_EQ(11U, Inv.size()); EXPECT_TRUE(Inv.any()); + EXPECT_FALSE(Inv.all()); EXPECT_FALSE(Inv.none()); EXPECT_FALSE(Inv.empty()); @@ -123,6 +127,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Vec.count()); EXPECT_EQ(130U, Vec.size()); EXPECT_FALSE(Vec.any()); + EXPECT_FALSE(Vec.all()); EXPECT_TRUE(Vec.none()); EXPECT_FALSE(Vec.empty()); @@ -130,6 +135,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Inv.count()); EXPECT_EQ(0U, Inv.size()); EXPECT_FALSE(Inv.any()); + EXPECT_TRUE(Inv.all()); EXPECT_TRUE(Inv.none()); EXPECT_TRUE(Inv.empty()); @@ -137,6 +143,7 @@ TEST(BitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Vec.count()); EXPECT_EQ(0U, Vec.size()); EXPECT_FALSE(Vec.any()); + EXPECT_TRUE(Vec.all()); EXPECT_TRUE(Vec.none()); EXPECT_TRUE(Vec.empty()); } diff --git a/unittests/ADT/SmallBitVectorTest.cpp b/unittests/ADT/SmallBitVectorTest.cpp index 9c69aad5a97..c4dda9e88d4 100644 --- a/unittests/ADT/SmallBitVectorTest.cpp +++ b/unittests/ADT/SmallBitVectorTest.cpp @@ -19,6 +19,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Vec.count()); EXPECT_EQ(0U, Vec.size()); EXPECT_FALSE(Vec.any()); + EXPECT_TRUE(Vec.all()); EXPECT_TRUE(Vec.none()); EXPECT_TRUE(Vec.empty()); @@ -26,6 +27,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(5U, Vec.count()); EXPECT_EQ(5U, Vec.size()); EXPECT_TRUE(Vec.any()); + EXPECT_TRUE(Vec.all()); EXPECT_FALSE(Vec.none()); EXPECT_FALSE(Vec.empty()); @@ -33,6 +35,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(5U, Vec.count()); EXPECT_EQ(11U, Vec.size()); EXPECT_TRUE(Vec.any()); + EXPECT_FALSE(Vec.all()); EXPECT_FALSE(Vec.none()); EXPECT_FALSE(Vec.empty()); @@ -40,6 +43,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(6U, Inv.count()); EXPECT_EQ(11U, Inv.size()); EXPECT_TRUE(Inv.any()); + EXPECT_FALSE(Inv.all()); EXPECT_FALSE(Inv.none()); EXPECT_FALSE(Inv.empty()); @@ -120,6 +124,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Vec.count()); EXPECT_EQ(130U, Vec.size()); EXPECT_FALSE(Vec.any()); + EXPECT_FALSE(Vec.all()); EXPECT_TRUE(Vec.none()); EXPECT_FALSE(Vec.empty()); @@ -127,6 +132,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Inv.count()); EXPECT_EQ(0U, Inv.size()); EXPECT_FALSE(Inv.any()); + EXPECT_TRUE(Inv.all()); EXPECT_TRUE(Inv.none()); EXPECT_TRUE(Inv.empty()); @@ -134,6 +140,7 @@ TEST(SmallBitVectorTest, TrivialOperation) { EXPECT_EQ(0U, Vec.count()); EXPECT_EQ(0U, Vec.size()); EXPECT_FALSE(Vec.any()); + EXPECT_TRUE(Vec.all()); EXPECT_TRUE(Vec.none()); EXPECT_TRUE(Vec.empty()); } -- 2.34.1