8#ifndef SkBitSet_DEFINED
9#define SkBitSet_DEFINED
25 , fChunks((Chunk*)
sk_calloc_throw(NumChunksFor(fSize) * sizeof(Chunk))) {}
32 this->fSize = that.fSize;
33 this->fChunks = std::move(that.fChunks);
41 void set(
size_t index) {
43 *this->chunkFor(index) |= ChunkMaskFor(index);
48 Chunk* chunks = fChunks.get();
49 const size_t numChunks = NumChunksFor(fSize);
50 std::memset(chunks, 0xFF,
sizeof(Chunk) * numChunks);
56 *this->chunkFor(index) &= ~ChunkMaskFor(index);
61 Chunk* chunks = fChunks.get();
62 const size_t numChunks = NumChunksFor(fSize);
63 std::memset(chunks, 0,
sizeof(Chunk) * numChunks);
66 bool test(
size_t index)
const {
68 return SkToBool(*this->chunkFor(index) & ChunkMaskFor(index));
78 const Chunk* chunks = fChunks.get();
79 const size_t numChunks = NumChunksFor(fSize);
80 for (
size_t i = 0;
i < numChunks; ++
i) {
81 if (Chunk chunk = chunks[
i]) {
82 const size_t index =
i * kChunkBits;
83 for (
size_t j = 0; j < kChunkBits; ++j) {
84 if (0x1 & (chunk >> j)) {
96 const Chunk* chunks = fChunks.get();
97 const size_t numChunks = NumChunksFor(fSize);
98 for (
size_t i = 0;
i < numChunks; ++
i) {
99 if (Chunk chunk = chunks[
i]) {
100 static_assert(kChunkBits <= std::numeric_limits<uint32_t>::digits,
"SkCTZ");
101 const size_t bitIndex =
i * kChunkBits +
SkCTZ(chunk);
110 const Chunk* chunks = fChunks.get();
111 const size_t numChunks = NumChunksFor(fSize);
112 for (
size_t i = 0;
i < numChunks; ++
i) {
113 if (Chunk chunk = ~chunks[
i]) {
114 static_assert(kChunkBits <= std::numeric_limits<uint32_t>::digits,
"SkCTZ");
115 const size_t bitIndex =
i * kChunkBits +
SkCTZ(chunk);
116 if (bitIndex >= fSize) {
128 using Chunk = uint32_t;
129 static_assert(std::numeric_limits<Chunk>::radix == 2);
130 inline static constexpr size_t kChunkBits = std::numeric_limits<Chunk>::digits;
131 static_assert(kChunkBits ==
sizeof(Chunk)*CHAR_BIT,
"SkBitSet must use every bit in a Chunk");
134 Chunk* chunkFor(
size_t index)
const {
135 return fChunks.get() + (index / kChunkBits);
138 static constexpr Chunk ChunkMaskFor(
size_t index) {
139 return (Chunk)1 << (index & (kChunkBits-1));
142 static constexpr size_t NumChunksFor(
size_t size) {
143 return (
size + (kChunkBits-1)) / kChunkBits;
static void * sk_calloc_throw(size_t size)
SK_API void sk_free(void *)
static int SkCTZ(uint32_t mask)
static constexpr bool SkToBool(const T &x)
SkBitSet & operator=(const SkBitSet &)=delete
OptionalIndex findFirst()
SkBitSet(const SkBitSet &)=delete
bool test(size_t index) const
std::optional< size_t > OptionalIndex
void forEachSetIndex(FN f) const
SkBitSet(SkBitSet &&that)
SkBitSet & operator=(SkBitSet &&that)
OptionalIndex findFirstUnset()