2020#include < span>
2121#include < string>
2222
23- // Common functions related to bits/bit-packing.
24- //
25- // The inlined methods are too simple to test or don't need tests
26- // as they are for debugging. The non-inlined methods are tested
27- // indirectly through the nullableColumnTests.cpp.
23+ // / Common functions related to bits/bit-packing.
24+ // /
25+ // / The inlined methods are too simple to test or don't need tests
26+ // / as they are for debugging. The non-inlined methods are tested
27+ // / indirectly through the nullableColumnTests.cpp.
2828
2929namespace facebook ::nimble::bits {
3030
31- // Returns the number of bits required to store the value.
32- // For a value of 0 we return 1.
31+ // / Returns the number of bits required to store the value.
32+ // / For a value of 0 we return 1.
3333inline int bitsRequired (uint64_t value) noexcept {
3434 return 64 - __builtin_clzll (value | 1 );
3535}
3636
37- // How many buckets are required to hold some elements?
37+ // / How many buckets are required to hold some elements?
3838inline uint64_t bucketsRequired (
3939 uint64_t elementCount,
4040 uint64_t bucketSize) noexcept {
4141 return elementCount / bucketSize + (elementCount % bucketSize != 0 );
4242}
4343
44- // Num bytes required to hold X bits.
44+ // / Num bytes required to hold X bits.
4545inline uint64_t bytesRequired (uint64_t bitCount) noexcept {
4646 return bucketsRequired (bitCount, 8 );
4747}
4848
49- // Sets the |n|th bit to true.
49+ // / Sets the |n|th bit to true.
5050inline void setBit (int64_t n, char * c) {
5151 const int64_t byte = n >> 3 ;
5252 const int64_t remainder = n & 7 ;
5353 c[byte] |= (1 << remainder);
5454}
5555
56- // Sets the |n|th bit to |bit|. Note that if bit is false, this
57- // is a no-op. The intention is to let you avoid a branch.
56+ // / Sets the |n|th bit to |bit|. Note that if bit is false, this
57+ // / is a no-op. The intention is to let you avoid a branch.
5858inline void maybeSetBit (int64_t n, char * c, bool bit) {
5959 const int64_t byte = n >> 3 ;
6060 const int64_t remainder = n & 7 ;
6161 c[byte] |= (bit << remainder);
6262}
6363
64- // Sets the |n|th bit to false.
64+ // / Sets the |n|th bit to false.
6565inline void clearBit (int64_t n, char * c) {
6666 const int64_t byte = n >> 3 ;
6767 const int64_t remainder = n & 7 ;
6868 c[byte] &= ~(1 << remainder);
6969}
7070
71- // Retrieves the |n|th bit.
71+ // / Retrieves the |n|th bit.
7272inline bool getBit (int64_t n, const char * c) {
7373 const int64_t byte = n >> 3 ;
7474 const int64_t remainder = n & 7 ;
7575 return c[byte] & (1 << remainder);
7676}
7777
78- // Sets bits [|begin|, |end|) in |bitmap| to true. Expects |bitmap| to be word
79- // aligned.
78+ // / Sets bits [|begin|, |end|) in |bitmap| to true. Expects |bitmap| to be word
79+ // / aligned.
8080void setBits (uint64_t begin, uint64_t end, char * bitmap);
8181
82- // Sets bits [|begin|, |end|) in |bitmap| to false. Expects |bitmap| to be word
83- // aligned.
82+ // / Sets bits [|begin|, |end|) in |bitmap| to false. Expects |bitmap| to be word
83+ // / aligned.
8484void clearBits (uint64_t begin, uint64_t end, char * bitmap);
8585
86- // Packs |bools| in bitmap. bitmap must point to a region of at least
87- // FixedBitArray::BufferSize(nulls.size(), 1) bytes. Note that we do not
88- // first clear the bytes being written to in |bitmap|, so if |bitmap| does
89- // not point to a zeroed out region after this call bit i will be set if
90- // bools[i] is true OR bit i was originally true.
86+ // / Packs |bools| in bitmap. bitmap must point to a region of at least
87+ // / FixedBitArray::BufferSize(nulls.size(), 1) bytes. Note that we do not
88+ // / first clear the bytes being written to in |bitmap|, so if |bitmap| does
89+ // / not point to a zeroed out region after this call bit i will be set if
90+ // / bools[i] is true OR bit i was originally true.
9191void packBitmap (std::span<const bool > bools, char * bitmap);
9292
93- // Returns how many of the bits in the |bitmap| starting at index |row| and
94- // going |rowCount| forward are non-null.
93+ // / Returns how many of the bits in the |bitmap| starting at index |row| and
94+ // / going |rowCount| forward are non-null.
9595uint32_t countSetBits (uint32_t row, uint32_t rowCount, const char * bitmap);
9696
97- // Finds the indices (relative to the row) of the non-null indices (i.e. set
98- // bits) among the next |rowCount| rows. |indices| should point to a buffer
99- // Y enough to hold rowCount values. We return the number of non-nulls.
97+ // / Finds the indices (relative to the row) of the non-null indices (i.e. set
98+ // / bits) among the next |rowCount| rows. |indices| should point to a buffer
99+ // / Y enough to hold rowCount values. We return the number of non-nulls.
100100uint32_t findNonNullIndices (
101101 uint32_t row,
102102 uint32_t rowCount,
103103 const char * bitmap,
104104 uint32_t * indices);
105105
106- // Same as above, but returns the indices of the nulls (i.e. unset bits).
106+ // / Same as above, but returns the indices of the nulls (i.e. unset bits).
107107uint32_t findNullIndices (
108108 uint32_t row,
109109 uint32_t rowCount,
110110 const char * bitmap,
111111 uint32_t * indices);
112112
113- // Finds the index of |n|th set bit in [|begin|, |end|) in |bitmap|. If not
114- // found, return |end|.
113+ // / Finds the index of |n|th set bit in [|begin|, |end|) in |bitmap|. If not
114+ // / found, return |end|.
115115uint32_t
116116findSetBit (const char * bitmap, uint32_t begin, uint32_t end, uint32_t n);
117117
118- // Prints out the bits in numeric type in nibbles. Not efficient,
119- // only should be used for debugging/prototyping.
118+ // / Prints out the bits in numeric type in nibbles. Not efficient,
119+ // / only should be used for debugging/prototyping.
120120template <typename T>
121121std::string printBits (T c) {
122122 std::string result;
@@ -131,7 +131,7 @@ std::string printBits(T c) {
131131 }
132132 c >>= 1 ;
133133 }
134- // We actually want little endian order.
134+ // / We actually want little endian order.
135135 std::reverse (result.begin (), result.end ());
136136 return result;
137137}
@@ -173,8 +173,8 @@ class Bitmap {
173173 }
174174
175175 protected:
176- char * bitmap_;
177- uint32_t size_;
176+ char * const bitmap_;
177+ const uint32_t size_;
178178};
179179
180180class BitmapBuilder : public Bitmap {
@@ -197,9 +197,9 @@ class BitmapBuilder : public Bitmap {
197197 clearBits (begin, end, bitmap_);
198198 }
199199
200- // Copy the specified range from the source bitmap into this one. It
201- // guarantees |begin| is the beginning bit offset, but may copy more beyond
202- // |end|.
200+ // / Copy the specified range from the source bitmap into this one. It
201+ // / guarantees |begin| is the beginning bit offset, but may copy more beyond
202+ // / |end|.
203203 void copy (const Bitmap& other, uint32_t begin, uint32_t end);
204204};
205205
0 commit comments