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 | #ifndef SkBitSet_DEFINED |
| 11 | #define SkBitSet_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 14 | #include "SkTDArray.h" |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 15 | |
| 16 | class SkBitSet { |
| 17 | public: |
| 18 | /** NumberOfBits must be greater than zero. |
| 19 | */ |
| 20 | explicit SkBitSet(int numberOfBits); |
| 21 | explicit SkBitSet(const SkBitSet& source); |
| 22 | |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 23 | SkBitSet& operator=(const SkBitSet& rhs); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 24 | bool operator==(const SkBitSet& rhs); |
| 25 | bool operator!=(const SkBitSet& rhs); |
| 26 | |
| 27 | /** Clear all data. |
| 28 | */ |
| 29 | void clearAll(); |
| 30 | |
| 31 | /** Set the value of the index-th bit. |
| 32 | */ |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 33 | void setBit(int index, bool value) { |
| 34 | uint32_t mask = 1 << (index & 31); |
| 35 | uint32_t* chunk = this->internalGet(index); |
| 36 | if (value) { |
| 37 | *chunk |= mask; |
| 38 | } else { |
| 39 | *chunk &= ~mask; |
| 40 | } |
| 41 | } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 42 | |
| 43 | /** Test if bit index is set. |
| 44 | */ |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 45 | bool isBitSet(int index) const { |
| 46 | uint32_t mask = 1 << (index & 31); |
| 47 | return SkToBool(*this->internalGet(index) & mask); |
| 48 | } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 49 | |
| 50 | /** Or bits from source. false is returned if this doesn't have the same |
| 51 | * bit count as source. |
| 52 | */ |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 53 | bool orBits(const SkBitSet& source); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 54 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 55 | /** Export indices of set bits to T array. |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 56 | */ |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 57 | template<typename T> |
| 58 | void exportTo(SkTDArray<T>* array) const { |
| 59 | SkASSERT(array); |
| 60 | uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get()); |
| 61 | for (unsigned int i = 0; i < fDwordCount; ++i) { |
| 62 | uint32_t value = data[i]; |
| 63 | if (value) { // There are set bits |
| 64 | unsigned int index = i * 32; |
| 65 | for (unsigned int j = 0; j < 32; ++j) { |
| 66 | if (0x1 & (value >> j)) { |
| 67 | array->push(index + j); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 73 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 74 | private: |
| 75 | SkAutoFree fBitData; |
| 76 | // Dword (32-bit) count of the bitset. |
| 77 | size_t fDwordCount; |
| 78 | size_t fBitCount; |
| 79 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 80 | uint32_t* internalGet(int index) const { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 81 | SkASSERT((size_t)index < fBitCount); |
| 82 | size_t internalIndex = index / 32; |
| 83 | SkASSERT(internalIndex < fDwordCount); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 84 | return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 85 | } |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | #endif |