diff options
author | halcanary <halcanary@google.com> | 2016-08-19 16:23:23 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-08-19 16:23:23 -0700 |
commit | e2348ccb477b97847cd147161a57fbbbfc8bba10 (patch) | |
tree | 8a31a4b288fdb75c5d69af66e32ff9d7944286c9 /src/utils/SkBitSet.h | |
parent | 4c54788e9298dce2b963ce288baf0d1695323e1f (diff) |
src/utils/SkBitSet: simplify
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2265623002
Review-Url: https://codereview.chromium.org/2265623002
Diffstat (limited to 'src/utils/SkBitSet.h')
-rw-r--r-- | src/utils/SkBitSet.h | 63 |
1 files changed, 21 insertions, 42 deletions
diff --git a/src/utils/SkBitSet.h b/src/utils/SkBitSet.h index 1e35c9120f..a8585a20b9 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; } }; |