BLI: new bit span data structure #104671

Merged
Jacques Lucke merged 26 commits from JacquesLucke/blender:bit-span into main 2023-02-17 00:42:54 +01:00
3 changed files with 19 additions and 13 deletions
Showing only changes of commit 93e369acd6 - Show all commits

View File

@ -204,7 +204,7 @@ class MutableBitSpan {
}
/** Sets all referenced bits to 1. */
void set()
void set_all()
JacquesLucke marked this conversation as resolved
Review

It seems confusing to have set() and reset() methods for the whole span and for each bit. What about calling the functions on the span set_all() and reset_all(). Some name like fill() would be nice too since it mirrors MutableSpan

It seems confusing to have `set()` and `reset()` methods for the whole span and for each bit. What about calling the functions on the span `set_all()` and `reset_all()`. Some name like `fill()` would be nice too since it mirrors `MutableSpan`
{
const AlignedIndexRanges ranges = split_index_range_by_alignment(bit_range_, BitsPerInt);
{
@ -227,7 +227,7 @@ class MutableBitSpan {
}
/** Sets all referenced bits to 0. */
void reset()
void reset_all()
{
const AlignedIndexRanges ranges = split_index_range_by_alignment(bit_range_, BitsPerInt);
{
@ -250,15 +250,21 @@ class MutableBitSpan {
}
/** Sets all referenced bits to either 0 or 1. */
void set(const bool value)
void set_all(const bool value)
{
if (value) {
this->set();
this->set_all();
}
else {
this->reset();
this->reset_all();
}
}
/** Same as set_all to mirror #MutableSpan. */
void fill(const bool value)
{
this->set_all(value);
}
};
inline std::ostream &operator<<(std::ostream &stream, const BitSpan &span)

View File

@ -265,7 +265,7 @@ class BitVector {
size_in_bits_ = new_size_in_bits;
if (old_size_in_bits < new_size_in_bits) {
MutableBitSpan(data_, IndexRange(old_size_in_bits, new_size_in_bits - old_size_in_bits))
.set(value);
.set_all(value);
}
}
@ -274,7 +274,7 @@ class BitVector {
*/
void fill(const bool value)
{
MutableBitSpan(data_, size_in_bits_).set(value);
MutableBitSpan(data_, size_in_bits_).set_all(value);
}
/**

View File

@ -104,15 +104,15 @@ TEST(bit_span, RangeConstructor)
TEST(bit_span, Set)
{
uint64_t data = 0;
MutableBitSpan(&data, 64).set(true);
MutableBitSpan(&data, 64).set_all(true);
EXPECT_EQ(data, uint64_t(-1));
MutableBitSpan(&data, 64).set(false);
MutableBitSpan(&data, 64).set_all(false);
EXPECT_EQ(data, uint64_t(0));
MutableBitSpan(&data, IndexRange(4, 8)).set(true);
MutableBitSpan(&data, IndexRange(4, 8)).set_all(true);
EXPECT_EQ(data,
0b0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1111'1111'0000);
MutableBitSpan(&data, IndexRange(8, 30)).set(false);
MutableBitSpan(&data, IndexRange(8, 30)).set_all(false);
EXPECT_EQ(data,
0b0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'0000'1111'0000);
@ -123,13 +123,13 @@ TEST(bit_span, SetSliced)
std::array<uint64_t, 10> data;
memset(data.data(), 0, sizeof(data));
MutableBitSpan span{data.data(), 640};
span.slice(IndexRange(5, 500)).set(true);
span.slice(IndexRange(5, 500)).set_all(true);
for (const int64_t i : IndexRange(640)) {
EXPECT_EQ(span[i], i >= 5 && i < 505);
}
span.slice(IndexRange(10, 190)).set(false);
span.slice(IndexRange(10, 190)).set_all(false);
for (const int64_t i : IndexRange(640)) {
EXPECT_EQ(span[i], (i >= 5 && i < 10) || (i >= 200 && i < 505));