llvm.org GIT mirror llvm / ab1155a
[ADT][BitVector] Add push_back() Add a higher performance alternative to calling resize() every time which performs a lot of clearing to zero - when we're adding a single bit most of the time this will be completely unnecessary. Differential Revision: https://reviews.llvm.org/D52236 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@342535 91177308-0d34-0410-b5e6-96231b3b80d8 Simon Pilgrim 1 year, 30 days ago
3 changed file(s) with 53 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
500500
501501 bool test(unsigned Idx) const {
502502 return (*this)[Idx];
503 }
504
505 // Push single bit to end of vector.
506 void push_back(bool Val) {
507 unsigned OldSize = Size;
508 unsigned NewSize = Size + 1;
509
510 // Resize, which will insert zeros.
511 // If we already fit then the unused bits will be already zero.
512 if (NewSize > getBitCapacity())
513 resize(NewSize, false);
514 else
515 Size = NewSize;
516
517 // If true, set single bit.
518 if (Val)
519 set(OldSize);
503520 }
504521
505522 /// Test if any common bits are set.
464464 return (*this)[Idx];
465465 }
466466
467 // Push single bit to end of vector.
468 void push_back(bool Val) {
469 resize(size() + 1, Val);
470 }
471
467472 /// Test if any common bits are set.
468473 bool anyCommon(const SmallBitVector &RHS) const {
469474 if (isSmall() && RHS.isSmall())
835835 for (unsigned Bit : ToFill.set_bits())
836836 EXPECT_EQ(List[i++], Bit);
837837 }
838
839 TYPED_TEST(BitVectorTest, PushBack) {
840 TypeParam Vec(10, false);
841 EXPECT_EQ(-1, Vec.find_first());
842 EXPECT_EQ(10, Vec.size());
843 EXPECT_EQ(0, Vec.count());
844
845 Vec.push_back(true);
846 EXPECT_EQ(10, Vec.find_first());
847 EXPECT_EQ(11, Vec.size());
848 EXPECT_EQ(1, Vec.count());
849
850 Vec.push_back(false);
851 EXPECT_EQ(10, Vec.find_first());
852 EXPECT_EQ(12, Vec.size());
853 EXPECT_EQ(1, Vec.count());
854
855 Vec.push_back(true);
856 EXPECT_EQ(10, Vec.find_first());
857 EXPECT_EQ(13, Vec.size());
858 EXPECT_EQ(2, Vec.count());
859
860 // Add a lot of values to cause reallocation.
861 for (int i = 0; i != 100; ++i) {
862 Vec.push_back(true);
863 Vec.push_back(false);
864 }
865 EXPECT_EQ(10, Vec.find_first());
866 EXPECT_EQ(213, Vec.size());
867 EXPECT_EQ(102, Vec.count());
868 }
838869 }
839870 #endif