aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkBitSet.cpp
diff options
context:
space:
mode:
authorGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-03-21 23:11:46 +0000
committerGravatar vandebo@chromium.org <vandebo@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-03-21 23:11:46 +0000
commitec1a7fa3041944f7c951971f826d82ec05d31b60 (patch)
tree1a5a7f2d5edb4cf06a7c99974626a20055752623 /src/utils/SkBitSet.cpp
parentcbd76ae138c4f34cd272ee9c0ac4facbe90fed90 (diff)
Move SkBitSet to utils.
Review URL: https://codereview.appspot.com/5875043 git-svn-id: http://skia.googlecode.com/svn/trunk@3460 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/utils/SkBitSet.cpp')
-rwxr-xr-xsrc/utils/SkBitSet.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/utils/SkBitSet.cpp b/src/utils/SkBitSet.cpp
new file mode 100755
index 0000000000..664c8cd86f
--- /dev/null
+++ b/src/utils/SkBitSet.cpp
@@ -0,0 +1,83 @@
+
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+
+#include "SkBitSet.h"
+
+SkBitSet::SkBitSet(int numberOfBits)
+ : fBitData(NULL), fDwordCount(0), fBitCount(numberOfBits) {
+ SkASSERT(numberOfBits > 0);
+ // Round up size to 32-bit boundary.
+ fDwordCount = (numberOfBits + 31) / 32;
+ fBitData.set(malloc(fDwordCount * sizeof(uint32_t)));
+ clearAll();
+}
+
+SkBitSet::SkBitSet(const SkBitSet& source)
+ : fBitData(NULL), fDwordCount(0), fBitCount(0) {
+ *this = source;
+}
+
+const SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) {
+ if (this == &rhs) {
+ return *this;
+ }
+ fBitCount = rhs.fBitCount;
+ fBitData.free();
+ fDwordCount = rhs.fDwordCount;
+ fBitData.set(malloc(fDwordCount * sizeof(uint32_t)));
+ memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t));
+ return *this;
+}
+
+bool SkBitSet::operator==(const SkBitSet& rhs) {
+ if (fBitCount == rhs.fBitCount) {
+ if (fBitData.get() != NULL) {
+ return (memcmp(fBitData.get(), rhs.fBitData.get(),
+ fDwordCount * sizeof(uint32_t)) == 0);
+ }
+ return true;
+ }
+ return false;
+}
+
+bool SkBitSet::operator!=(const SkBitSet& rhs) {
+ return !(*this == rhs);
+}
+
+void SkBitSet::clearAll() {
+ if (fBitData.get() != NULL) {
+ sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t));
+ }
+}
+
+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* sourceBitmap = source.internalGet(0);
+ for (size_t i = 0; i < fDwordCount; ++i) {
+ targetBitmap[i] |= sourceBitmap[i];
+ }
+ return true;
+}