[PATCH] D117114: [llvm][ADT] Implement `BitVector(std::initializer_list)`

Jan Svoboda via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 21 04:23:49 PST 2022


jansvoboda11 updated this revision to Diff 401943.
jansvoboda11 added a comment.

Use named constructor instead


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117114/new/

https://reviews.llvm.org/D117114

Files:
  llvm/include/llvm/ADT/BitVector.h
  llvm/include/llvm/ADT/SmallBitVector.h
  llvm/unittests/ADT/BitVectorTest.cpp


Index: llvm/unittests/ADT/BitVectorTest.cpp
===================================================================
--- llvm/unittests/ADT/BitVectorTest.cpp
+++ llvm/unittests/ADT/BitVectorTest.cpp
@@ -23,6 +23,15 @@
 typedef ::testing::Types<BitVector, SmallBitVector> BitVectorTestTypes;
 TYPED_TEST_SUITE(BitVectorTest, BitVectorTestTypes, );
 
+TYPED_TEST(BitVectorTest, Make) {
+  auto Vec = TypeParam::make({true, false, true, true});
+  EXPECT_EQ(4U, Vec.size());
+  EXPECT_EQ(true, Vec[0]);
+  EXPECT_EQ(false, Vec[1]);
+  EXPECT_EQ(true, Vec[2]);
+  EXPECT_EQ(true, Vec[3]);
+}
+
 TYPED_TEST(BitVectorTest, TrivialOperation) {
   TypeParam Vec;
   EXPECT_EQ(0U, Vec.count());
Index: llvm/include/llvm/ADT/SmallBitVector.h
===================================================================
--- llvm/include/llvm/ADT/SmallBitVector.h
+++ llvm/include/llvm/ADT/SmallBitVector.h
@@ -21,6 +21,7 @@
 #include <climits>
 #include <cstddef>
 #include <cstdint>
+#include <initializer_list>
 #include <limits>
 #include <utility>
 
@@ -168,6 +169,15 @@
       delete getPointer();
   }
 
+  /// Creates a bitvector with the specified values.
+  static SmallBitVector make(std::initializer_list<bool> Values) {
+    SmallBitVector Result(Values.size());
+    unsigned Idx = 0;
+    for (bool Value : Values)
+      Result[Idx++] = Value;
+    return Result;
+  }
+
   using const_set_bits_iterator = const_set_bits_iterator_impl<SmallBitVector>;
   using set_iterator = const_set_bits_iterator;
 
Index: llvm/include/llvm/ADT/BitVector.h
===================================================================
--- llvm/include/llvm/ADT/BitVector.h
+++ llvm/include/llvm/ADT/BitVector.h
@@ -23,6 +23,7 @@
 #include <cstdint>
 #include <cstdlib>
 #include <cstring>
+#include <initializer_list>
 #include <utility>
 
 namespace llvm {
@@ -144,6 +145,15 @@
       clear_unused_bits();
   }
 
+  /// Creates a bitvector with the specified values.
+  static BitVector make(std::initializer_list<bool> Values) {
+    BitVector Result(Values.size());
+    unsigned Idx = 0;
+    for (bool Value : Values)
+      Result[Idx++] = Value;
+    return Result;
+  }
+
   /// empty - Tests whether there are no bits in this bitvector.
   bool empty() const { return Size == 0; }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117114.401943.patch
Type: text/x-patch
Size: 2259 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220121/bd561c6a/attachment.bin>


More information about the llvm-commits mailing list