Small improvements to SkBitSet:
authormtklein <mtklein@chromium.org>
Thu, 9 Oct 2014 18:49:30 +0000 (11:49 -0700)
committerCommit bot <commit-bot@chromium.org>
Thu, 9 Oct 2014 18:49:30 +0000 (11:49 -0700)
  - 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

src/utils/SkBitSet.cpp
src/utils/SkBitSet.h

index 36ff6cb..63d18ff 100755 (executable)
@@ -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];
index e113fd7..266fd87 100644 (file)
@@ -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.