vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 9 | #ifndef SkBitSet_DEFINED |
| 10 | #define SkBitSet_DEFINED |
| 11 | |
| 12 | #include "SkTypes.h" |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 13 | #include "SkTDArray.h" |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 14 | |
| 15 | class SkBitSet { |
| 16 | public: |
| 17 | /** NumberOfBits must be greater than zero. |
| 18 | */ |
| 19 | explicit SkBitSet(int numberOfBits); |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame^] | 20 | SkBitSet(const SkBitSet&) = delete; |
| 21 | SkBitSet(SkBitSet&&); |
| 22 | SkBitSet& operator=(const SkBitSet&) = delete; |
| 23 | SkBitSet& operator=(SkBitSet&& rhs); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 24 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 25 | bool operator==(const SkBitSet& rhs); |
| 26 | bool operator!=(const SkBitSet& rhs); |
| 27 | |
| 28 | /** Clear all data. |
| 29 | */ |
| 30 | void clearAll(); |
| 31 | |
| 32 | /** Set the value of the index-th bit. |
| 33 | */ |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 34 | void setBit(int index, bool value) { |
| 35 | uint32_t mask = 1 << (index & 31); |
| 36 | uint32_t* chunk = this->internalGet(index); |
| 37 | if (value) { |
| 38 | *chunk |= mask; |
| 39 | } else { |
| 40 | *chunk &= ~mask; |
| 41 | } |
| 42 | } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame^] | 43 | void set(int index) { this->setBit(index, true); } |
| 44 | |
| 45 | template<typename T> |
| 46 | void setAll(T* array, int len) { |
| 47 | static_assert(std::is_integral<T>::value, "T is integral"); |
| 48 | for (int i = 0; i < len; ++i) { |
| 49 | this->set(static_cast<int>(array[i])); |
| 50 | } |
| 51 | } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 52 | |
| 53 | /** Test if bit index is set. |
| 54 | */ |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 55 | bool isBitSet(int index) const { |
| 56 | uint32_t mask = 1 << (index & 31); |
| 57 | return SkToBool(*this->internalGet(index) & mask); |
| 58 | } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame^] | 59 | bool has(int index) const { return this->isBitSet(index); } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 60 | |
| 61 | /** Or bits from source. false is returned if this doesn't have the same |
| 62 | * bit count as source. |
| 63 | */ |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 64 | bool orBits(const SkBitSet& source); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 65 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 66 | /** Export indices of set bits to T array. |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 67 | */ |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 68 | template<typename T> |
| 69 | void exportTo(SkTDArray<T>* array) const { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame^] | 70 | static_assert(std::is_integral<T>::value, "T is integral"); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 71 | SkASSERT(array); |
| 72 | uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get()); |
| 73 | for (unsigned int i = 0; i < fDwordCount; ++i) { |
| 74 | uint32_t value = data[i]; |
| 75 | if (value) { // There are set bits |
| 76 | unsigned int index = i * 32; |
| 77 | for (unsigned int j = 0; j < 32; ++j) { |
| 78 | if (0x1 & (value >> j)) { |
| 79 | array->push(index + j); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | } |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 85 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 86 | private: |
| 87 | SkAutoFree fBitData; |
| 88 | // Dword (32-bit) count of the bitset. |
| 89 | size_t fDwordCount; |
| 90 | size_t fBitCount; |
| 91 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 92 | uint32_t* internalGet(int index) const { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 93 | SkASSERT((size_t)index < fBitCount); |
| 94 | size_t internalIndex = index / 32; |
| 95 | SkASSERT(internalIndex < fDwordCount); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 96 | return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 97 | } |
| 98 | }; |
| 99 | |
| 100 | |
| 101 | #endif |