[PATCH] D52236: [ADT][BitVector] Add push_back()

Simon Pilgrim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 19 04:10:56 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL342535: [ADT][BitVector] Add push_back() (authored by RKSimon, committed by ).
Herald added a subscriber: kristina.

Changed prior to commit:
  https://reviews.llvm.org/D52236?vs=165977&id=166085#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D52236

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


Index: llvm/trunk/unittests/ADT/BitVectorTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/BitVectorTest.cpp
+++ llvm/trunk/unittests/ADT/BitVectorTest.cpp
@@ -836,5 +836,36 @@
   for (unsigned Bit : ToFill.set_bits())
     EXPECT_EQ(List[i++], Bit);
 }
+
+TYPED_TEST(BitVectorTest, PushBack) {
+  TypeParam Vec(10, false);
+  EXPECT_EQ(-1, Vec.find_first());
+  EXPECT_EQ(10, Vec.size());
+  EXPECT_EQ(0, Vec.count());
+
+  Vec.push_back(true);
+  EXPECT_EQ(10, Vec.find_first());
+  EXPECT_EQ(11, Vec.size());
+  EXPECT_EQ(1, Vec.count());
+
+  Vec.push_back(false);
+  EXPECT_EQ(10, Vec.find_first());
+  EXPECT_EQ(12, Vec.size());
+  EXPECT_EQ(1, Vec.count());
+
+  Vec.push_back(true);
+  EXPECT_EQ(10, Vec.find_first());
+  EXPECT_EQ(13, Vec.size());
+  EXPECT_EQ(2, Vec.count());
+
+  // Add a lot of values to cause reallocation.
+  for (int i = 0; i != 100; ++i) {
+    Vec.push_back(true);
+    Vec.push_back(false);
+  }
+  EXPECT_EQ(10, Vec.find_first());
+  EXPECT_EQ(213, Vec.size());
+  EXPECT_EQ(102, Vec.count());
+}
 }
 #endif
Index: llvm/trunk/include/llvm/ADT/SmallBitVector.h
===================================================================
--- llvm/trunk/include/llvm/ADT/SmallBitVector.h
+++ llvm/trunk/include/llvm/ADT/SmallBitVector.h
@@ -465,6 +465,11 @@
     return (*this)[Idx];
   }
 
+  // Push single bit to end of vector.
+  void push_back(bool Val) {
+    resize(size() + 1, Val);
+  }
+
   /// Test if any common bits are set.
   bool anyCommon(const SmallBitVector &RHS) const {
     if (isSmall() && RHS.isSmall())
Index: llvm/trunk/include/llvm/ADT/BitVector.h
===================================================================
--- llvm/trunk/include/llvm/ADT/BitVector.h
+++ llvm/trunk/include/llvm/ADT/BitVector.h
@@ -503,6 +503,23 @@
     return (*this)[Idx];
   }
 
+  // Push single bit to end of vector.
+  void push_back(bool Val) {
+    unsigned OldSize = Size;
+    unsigned NewSize = Size + 1;
+
+    // Resize, which will insert zeros.
+    // If we already fit then the unused bits will be already zero.
+    if (NewSize > getBitCapacity())
+      resize(NewSize, false);
+    else
+      Size = NewSize;
+
+    // If true, set single bit.
+    if (Val)
+      set(OldSize);
+  }
+
   /// Test if any common bits are set.
   bool anyCommon(const BitVector &RHS) const {
     unsigned ThisWords = NumBitWords(size());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52236.166085.patch
Type: text/x-patch
Size: 2426 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180919/2241095c/attachment.bin>


More information about the llvm-commits mailing list