Index: src/utils/SkBitSet.h |
diff --git a/src/utils/SkBitSet.h b/src/utils/SkBitSet.h |
index 1e35c9120f47f3862efa56a9490747ab1907379b..a8585a20b9ea9d30820ca9ad7b3c64d1d5f52bdd 100644 |
--- a/src/utils/SkBitSet.h |
+++ b/src/utils/SkBitSet.h |
@@ -5,42 +5,30 @@ |
* found in the LICENSE file. |
*/ |
- |
#ifndef SkBitSet_DEFINED |
#define SkBitSet_DEFINED |
-#include "SkTypes.h" |
#include "SkTDArray.h" |
+#include "SkTemplates.h" |
class SkBitSet { |
public: |
- /** NumberOfBits must be greater than zero. |
- */ |
- explicit SkBitSet(int numberOfBits); |
+ explicit SkBitSet(int numberOfBits) { |
+ SkASSERT(numberOfBits > 0); |
+ fDwordCount = (numberOfBits + 31) / 32; // Round up size to 32-bit boundary. |
+ fBitData.reset((uint32_t*)sk_calloc_throw(fDwordCount * sizeof(uint32_t))); |
+ } |
+ |
SkBitSet(const SkBitSet&) = delete; |
- SkBitSet(SkBitSet&&); |
SkBitSet& operator=(const SkBitSet&) = delete; |
- SkBitSet& operator=(SkBitSet&& rhs); |
- |
- bool operator==(const SkBitSet& rhs); |
- bool operator!=(const SkBitSet& rhs); |
- /** Clear all data. |
- */ |
- void clearAll(); |
- |
- /** Set the value of the index-th bit. |
- */ |
- void setBit(int index, bool value) { |
+ /** Set the value of the index-th bit to true. */ |
+ void set(int index) { |
uint32_t mask = 1 << (index & 31); |
uint32_t* chunk = this->internalGet(index); |
- if (value) { |
- *chunk |= mask; |
- } else { |
- *chunk &= ~mask; |
- } |
+ SkASSERT(chunk); |
+ *chunk |= mask; |
} |
- void set(int index) { this->setBit(index, true); } |
template<typename T> |
void setAll(T* array, int len) { |
@@ -50,21 +38,13 @@ public: |
} |
} |
- /** Test if bit index is set. |
- */ |
- bool isBitSet(int index) const { |
+ bool has(int index) const { |
+ const uint32_t* chunk = this->internalGet(index); |
uint32_t mask = 1 << (index & 31); |
- return SkToBool(*this->internalGet(index) & mask); |
+ return chunk && SkToBool(*chunk & mask); |
} |
- bool has(int index) const { return this->isBitSet(index); } |
- /** Or bits from source. false is returned if this doesn't have the same |
- * bit count as source. |
- */ |
- bool orBits(const SkBitSet& source); |
- |
- /** Export indices of set bits to T array. |
- */ |
+ /** Export indices of set bits to T array. */ |
template<typename T> |
void exportTo(SkTDArray<T>* array) const { |
static_assert(std::is_integral<T>::value, "T is integral"); |
@@ -84,16 +64,15 @@ public: |
} |
private: |
- SkAutoFree fBitData; |
- // Dword (32-bit) count of the bitset. |
- size_t fDwordCount; |
- size_t fBitCount; |
+ std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData; |
+ size_t fDwordCount; // Dword (32-bit) count of the bitset. |
uint32_t* internalGet(int index) const { |
- SkASSERT((size_t)index < fBitCount); |
size_t internalIndex = index / 32; |
- SkASSERT(internalIndex < fDwordCount); |
- return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex; |
+ if (internalIndex >= fDwordCount) { |
+ return nullptr; |
+ } |
+ return fBitData.get() + internalIndex; |
} |
}; |