mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-02-01 13:11:39 +01:00
[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 llvm-svn: 342535
This commit is contained in:
parent
b1a1176161
commit
43e5ef164c
@ -503,6 +503,23 @@ public:
|
|||||||
return (*this)[Idx];
|
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.
|
/// Test if any common bits are set.
|
||||||
bool anyCommon(const BitVector &RHS) const {
|
bool anyCommon(const BitVector &RHS) const {
|
||||||
unsigned ThisWords = NumBitWords(size());
|
unsigned ThisWords = NumBitWords(size());
|
||||||
|
@ -465,6 +465,11 @@ public:
|
|||||||
return (*this)[Idx];
|
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.
|
/// Test if any common bits are set.
|
||||||
bool anyCommon(const SmallBitVector &RHS) const {
|
bool anyCommon(const SmallBitVector &RHS) const {
|
||||||
if (isSmall() && RHS.isSmall())
|
if (isSmall() && RHS.isSmall())
|
||||||
|
@ -836,5 +836,36 @@ TYPED_TEST(BitVectorTest, Iterators) {
|
|||||||
for (unsigned Bit : ToFill.set_bits())
|
for (unsigned Bit : ToFill.set_bits())
|
||||||
EXPECT_EQ(List[i++], Bit);
|
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
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user