Returns the number of set bits.
| 146 | |
| 147 | /// Returns the number of set bits. |
| 148 | fl::u32 count() const FL_NOEXCEPT { |
| 149 | fl::u32 cnt = 0; |
| 150 | // Count bits in all complete blocks |
| 151 | for (fl::u32 i = 0; i < block_count - 1; ++i) { |
| 152 | cnt += fl::popcount(_blocks[i]); |
| 153 | } |
| 154 | |
| 155 | // For the last block, we need to be careful about counting only valid |
| 156 | // bits |
| 157 | if (block_count > 0) { |
| 158 | block_type last_block = _blocks[block_count - 1]; |
| 159 | // If N is not a multiple of bits_per_block, mask out the unused |
| 160 | // bits |
| 161 | if (N % bits_per_block != 0) { |
| 162 | const fl::u32 valid_bits = N % bits_per_block; |
| 163 | // Create a mask with only the valid bits set to 1 |
| 164 | block_type mask = (valid_bits == bits_per_block) |
| 165 | ? static_cast<block_type>(~block_type(0)) |
| 166 | : ((block_type(1) << valid_bits) - 1); |
| 167 | last_block &= mask; |
| 168 | } |
| 169 | cnt += fl::popcount(last_block); |
| 170 | } |
| 171 | |
| 172 | return cnt; |
| 173 | } |
| 174 | |
| 175 | /// Queries. |
| 176 | bool any() const FL_NOEXCEPT { return count() > 0; } |