/** Set the value of the index-th bit to true. */ void set(size_t index) {
SkASSERT(index < fSize);
*this->chunkFor(index) |= ChunkMaskFor(index);
}
/** Sets every bit in the bitset to true. */ void set() {
Chunk* chunks = fChunks.get(); const size_t numChunks = NumChunksFor(fSize);
std::memset(chunks, 0xFF, sizeof(Chunk) * numChunks);
}
/** Set the value of the index-th bit to false. */ void reset(size_t index) {
SkASSERT(index < fSize);
*this->chunkFor(index) &= ~ChunkMaskFor(index);
}
/** Sets every bit in the bitset to false. */ void reset() {
Chunk* chunks = fChunks.get(); const size_t numChunks = NumChunksFor(fSize);
std::memset(chunks, 0, sizeof(Chunk) * numChunks);
}
// Calls f(size_t index) for each set index. template<typename FN> void forEachSetIndex(FN f) const { const Chunk* chunks = fChunks.get(); const size_t numChunks = NumChunksFor(fSize); for (size_t i = 0; i < numChunks; ++i) { if (Chunk chunk = chunks[i]) { // There are set bits const size_t index = i * kChunkBits; for (size_t j = 0; j < kChunkBits; ++j) { if (0x1 & (chunk >> j)) {
f(index + j);
}
}
}
}
}
using OptionalIndex = std::optional<size_t>;
// If any bits are set, returns the index of the first.
OptionalIndex findFirst() { const Chunk* chunks = fChunks.get(); const size_t numChunks = NumChunksFor(fSize); for (size_t i = 0; i < numChunks; ++i) { if (Chunk chunk = chunks[i]) { // There are set bits
static_assert(kChunkBits <= std::numeric_limits<uint32_t>::digits, "SkCTZ"); const size_t bitIndex = i * kChunkBits + SkCTZ(chunk); return OptionalIndex(bitIndex);
}
} return OptionalIndex();
}
// If any bits are not set, returns the index of the first.
OptionalIndex findFirstUnset() { const Chunk* chunks = fChunks.get(); const size_t numChunks = NumChunksFor(fSize); for (size_t i = 0; i < numChunks; ++i) { if (Chunk chunk = ~chunks[i]) { // if there are unset bits ...
static_assert(kChunkBits <= std::numeric_limits<uint32_t>::digits, "SkCTZ"); const size_t bitIndex = i * kChunkBits + SkCTZ(chunk); if (bitIndex >= fSize) { break;
} return OptionalIndex(bitIndex);
}
} return OptionalIndex();
}
private:
size_t fSize;
using Chunk = uint32_t;
static_assert(std::numeric_limits<Chunk>::radix == 2); inlinestatic constexpr size_t kChunkBits = std::numeric_limits<Chunk>::digits;
static_assert(kChunkBits == sizeof(Chunk)*CHAR_BIT, "SkBitSet must use every bit in a Chunk");
std::unique_ptr<Chunk, SkOverloadedFunctionObject<void(void*), sk_free>> fChunks;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.