epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 10 | #include "SkBitSet.h" |
| 11 | |
| 12 | SkBitSet::SkBitSet(int numberOfBits) |
| 13 | : fBitData(NULL), fDwordCount(0), fBitCount(numberOfBits) { |
| 14 | SkASSERT(numberOfBits > 0); |
| 15 | // Round up size to 32-bit boundary. |
| 16 | fDwordCount = (numberOfBits + 31) / 32; |
| 17 | fBitData.set(malloc(fDwordCount * sizeof(uint32_t))); |
| 18 | clearAll(); |
| 19 | } |
| 20 | |
| 21 | SkBitSet::SkBitSet(const SkBitSet& source) |
| 22 | : fBitData(NULL), fDwordCount(0), fBitCount(0) { |
| 23 | *this = source; |
| 24 | } |
| 25 | |
| 26 | const SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 27 | if (this == &rhs) { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 28 | return *this; |
| 29 | } |
| 30 | fBitCount = rhs.fBitCount; |
| 31 | fBitData.free(); |
| 32 | fDwordCount = rhs.fDwordCount; |
| 33 | fBitData.set(malloc(fDwordCount * sizeof(uint32_t))); |
| 34 | memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t)); |
| 35 | return *this; |
| 36 | } |
| 37 | |
| 38 | bool SkBitSet::operator==(const SkBitSet& rhs) { |
| 39 | if (fBitCount == rhs.fBitCount) { |
| 40 | if (fBitData.get() != NULL) { |
| 41 | return (memcmp(fBitData.get(), rhs.fBitData.get(), |
| 42 | fDwordCount * sizeof(uint32_t)) == 0); |
| 43 | } |
| 44 | return true; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | bool SkBitSet::operator!=(const SkBitSet& rhs) { |
| 50 | return !(*this == rhs); |
| 51 | } |
| 52 | |
| 53 | void SkBitSet::clearAll() { |
| 54 | if (fBitData.get() != NULL) { |
| 55 | sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t)); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void SkBitSet::setBit(int index, bool value) { |
| 60 | uint32_t mask = 1 << (index % 32); |
| 61 | if (value) { |
| 62 | *(internalGet(index)) |= mask; |
| 63 | } else { |
| 64 | *(internalGet(index)) &= ~mask; |
| 65 | } |
| 66 | } |
| 67 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 68 | bool SkBitSet::isBitSet(int index) const { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 69 | uint32_t mask = 1 << (index % 32); |
bsalomon@google.com | 9d12f5c | 2011-09-29 18:08:18 +0000 | [diff] [blame^] | 70 | return 0 != (*internalGet(index) & mask); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 71 | } |
| 72 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 73 | bool SkBitSet::orBits(const SkBitSet& source) { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 74 | if (fBitCount != source.fBitCount) { |
| 75 | return false; |
| 76 | } |
| 77 | uint32_t* targetBitmap = internalGet(0); |
| 78 | uint32_t* sourceBitmap = source.internalGet(0); |
| 79 | for (size_t i = 0; i < fDwordCount; ++i) { |
| 80 | targetBitmap[i] |= sourceBitmap[i]; |
| 81 | } |
| 82 | return true; |
| 83 | } |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 84 | |
| 85 | void SkBitSet::exportTo(SkTDArray<uint32_t>* array) const { |
| 86 | SkASSERT(array); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 87 | uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get()); |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 88 | for (unsigned int i = 0; i < fDwordCount; ++i) { |
| 89 | uint32_t value = data[i]; |
| 90 | if (value) { // There are set bits |
| 91 | unsigned int index = i * 32; |
| 92 | for (unsigned int j = 0; j < 32; ++j) { |
| 93 | if (0x1 & (value >> j)) { |
| 94 | array->push(index + j); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |