diff options
author | 2014-10-09 11:49:30 -0700 | |
---|---|---|
committer | 2014-10-09 11:49:30 -0700 | |
commit | e099dd73dff0f447029323e918118a189bb27679 (patch) | |
tree | dd116d01d9e86525a8ebf46ed22cf5f0fe071ef7 | |
parent | 60e4ad7b29f50ebd7698d2d37580d5c8da5ce600 (diff) |
Small improvements to SkBitSet:
- implement O(1) operations in SkBitSet.h so they can inline away
- use calloc to allocate empty bitsets instead of malloc then clear
- little style things
BUG=skia:
Review URL: https://codereview.chromium.org/640243003
-rwxr-xr-x | src/utils/SkBitSet.cpp | 21 | ||||
-rw-r--r-- | src/utils/SkBitSet.h | 15 |
2 files changed, 16 insertions, 20 deletions
diff --git a/src/utils/SkBitSet.cpp b/src/utils/SkBitSet.cpp index 36ff6cbb01..63d18ff2f3 100755 --- a/src/utils/SkBitSet.cpp +++ b/src/utils/SkBitSet.cpp @@ -14,8 +14,7 @@ SkBitSet::SkBitSet(int numberOfBits) SkASSERT(numberOfBits > 0); // Round up size to 32-bit boundary. fDwordCount = (numberOfBits + 31) / 32; - fBitData.set(malloc(fDwordCount * sizeof(uint32_t))); - clearAll(); + fBitData.set(sk_calloc_throw(fDwordCount * sizeof(uint32_t))); } SkBitSet::SkBitSet(const SkBitSet& source) @@ -30,7 +29,7 @@ SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) { fBitCount = rhs.fBitCount; fBitData.free(); fDwordCount = rhs.fDwordCount; - fBitData.set(malloc(fDwordCount * sizeof(uint32_t))); + fBitData.set(sk_malloc_throw(fDwordCount * sizeof(uint32_t))); memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t)); return *this; } @@ -56,25 +55,11 @@ void SkBitSet::clearAll() { } } -void SkBitSet::setBit(int index, bool value) { - uint32_t mask = 1 << (index % 32); - if (value) { - *(internalGet(index)) |= mask; - } else { - *(internalGet(index)) &= ~mask; - } -} - -bool SkBitSet::isBitSet(int index) const { - uint32_t mask = 1 << (index % 32); - return 0 != (*internalGet(index) & mask); -} - bool SkBitSet::orBits(const SkBitSet& source) { if (fBitCount != source.fBitCount) { return false; } - uint32_t* targetBitmap = internalGet(0); + uint32_t* targetBitmap = this->internalGet(0); uint32_t* sourceBitmap = source.internalGet(0); for (size_t i = 0; i < fDwordCount; ++i) { targetBitmap[i] |= sourceBitmap[i]; diff --git a/src/utils/SkBitSet.h b/src/utils/SkBitSet.h index e113fd7004..266fd87496 100644 --- a/src/utils/SkBitSet.h +++ b/src/utils/SkBitSet.h @@ -30,11 +30,22 @@ public: /** Set the value of the index-th bit. */ - void setBit(int index, bool value); + void setBit(int index, bool value) { + uint32_t mask = 1 << (index & 31); + uint32_t* chunk = this->internalGet(index); + if (value) { + *chunk |= mask; + } else { + *chunk &= ~mask; + } + } /** Test if bit index is set. */ - bool isBitSet(int index) const; + bool isBitSet(int index) const { + uint32_t mask = 1 << (index & 31); + return SkToBool(*this->internalGet(index) & mask); + } /** Or bits from source. false is returned if this doesn't have the same * bit count as source. |