llvm.org GIT mirror llvm / 8038a28
[ADT] A few minor improvements to BitVector Fixes some spelling mistakes, uses a helper function, and adds an additional test case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302208 91177308-0d34-0410-b5e6-96231b3b80d8 Zachary Turner 3 years ago
3 changed file(s) with 33 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
216216 unsigned BitPos = Prev % BITWORD_SIZE;
217217 BitWord Copy = Bits[WordPos];
218218 // Mask off previous bits.
219 Copy &= ~0UL << BitPos;
219 Copy &= maskTrailingZeros(BitPos);
220220
221221 if (Copy != 0)
222222 return WordPos * BITWORD_SIZE + countTrailingZeros(Copy);
228228 return -1;
229229 }
230230
231 /// find_next_unset - Returns the index of the next usnet bit following the
231 /// find_next_unset - Returns the index of the next unset bit following the
232232 /// "Prev" bit. Returns -1 if all remaining bits are set.
233233 int find_next_unset(unsigned Prev) const {
234234 ++Prev;
213213 return ~maskTrailingOnes(CHAR_BIT * sizeof(T) - N);
214214 }
215215
216 /// \brief Create a bitmask with the N right-most bits set to 0, and all other
217 /// bits set to 1. Only unsigned types are allowed.
218 template T maskTrailingZeros(unsigned N) {
219 return maskLeadingOnes(CHAR_BIT * sizeof(T) - N);
220 }
221
222 /// \brief Create a bitmask with the N left-most bits set to 0, and all other
223 /// bits set to 1. Only unsigned types are allowed.
224 template T maskLeadingZeros(unsigned N) {
225 return maskTrailingOnes(CHAR_BIT * sizeof(T) - N);
226 }
227
216228 /// \brief Get the index of the last set bit starting from the least
217229 /// significant bit.
218230 ///
226226 EXPECT_EQ(-1, A.find_last());
227227 EXPECT_EQ(0, A.find_first_unset());
228228 EXPECT_EQ(99, A.find_last_unset());
229
230 // Also test with a vector that is small enough to fit in 1 word.
231 A.resize(20);
232 A.set(3);
233 A.set(4);
234 A.set(16);
235 EXPECT_EQ(16, A.find_last());
236 EXPECT_EQ(3, A.find_first());
237 EXPECT_EQ(3, A.find_next(1));
238 EXPECT_EQ(4, A.find_next(3));
239 EXPECT_EQ(16, A.find_next(4));
240 EXPECT_EQ(-1, A.find_next(16));
241
242 EXPECT_EQ(0, A.find_first_unset());
243 EXPECT_EQ(19, A.find_last_unset());
244 EXPECT_EQ(5, A.find_next_unset(3));
245 EXPECT_EQ(5, A.find_next_unset(4));
246 EXPECT_EQ(13, A.find_next_unset(12));
247 EXPECT_EQ(17, A.find_next_unset(15));
229248 }
230249
231250 TYPED_TEST(BitVectorTest, CompoundAssignment) {