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 | |
| 8 | #ifndef SkBitSet_DEFINED |
| 9 | #define SkBitSet_DEFINED |
| 10 | |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 11 | #include "SkTDArray.h" |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 12 | #include "SkTemplates.h" |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 13 | |
| 14 | class SkBitSet { |
| 15 | public: |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 16 | explicit SkBitSet(int numberOfBits) { |
halcanary | e202bd8 | 2016-09-19 10:27:03 -0700 | [diff] [blame] | 17 | SkASSERT(numberOfBits >= 0); |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 18 | fDwordCount = (numberOfBits + 31) / 32; // Round up size to 32-bit boundary. |
halcanary | e202bd8 | 2016-09-19 10:27:03 -0700 | [diff] [blame] | 19 | if (fDwordCount > 0) { |
| 20 | fBitData.reset((uint32_t*)sk_calloc_throw(fDwordCount * sizeof(uint32_t))); |
| 21 | } |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 22 | } |
| 23 | |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 24 | SkBitSet(const SkBitSet&) = delete; |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 25 | SkBitSet& operator=(const SkBitSet&) = delete; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 26 | |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 27 | /** Set the value of the index-th bit to true. */ |
| 28 | void set(int index) { |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 29 | uint32_t mask = 1 << (index & 31); |
| 30 | uint32_t* chunk = this->internalGet(index); |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 31 | SkASSERT(chunk); |
| 32 | *chunk |= mask; |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 33 | } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 34 | |
| 35 | template<typename T> |
| 36 | void setAll(T* array, int len) { |
| 37 | static_assert(std::is_integral<T>::value, "T is integral"); |
| 38 | for (int i = 0; i < len; ++i) { |
| 39 | this->set(static_cast<int>(array[i])); |
| 40 | } |
| 41 | } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 42 | |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 43 | bool has(int index) const { |
| 44 | const uint32_t* chunk = this->internalGet(index); |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 45 | uint32_t mask = 1 << (index & 31); |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 46 | return chunk && SkToBool(*chunk & mask); |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 47 | } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 48 | |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 49 | /** Export indices of set bits to T array. */ |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 50 | template<typename T> |
| 51 | void exportTo(SkTDArray<T>* array) const { |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 52 | static_assert(std::is_integral<T>::value, "T is integral"); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 53 | SkASSERT(array); |
| 54 | uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get()); |
| 55 | for (unsigned int i = 0; i < fDwordCount; ++i) { |
| 56 | uint32_t value = data[i]; |
| 57 | if (value) { // There are set bits |
| 58 | unsigned int index = i * 32; |
| 59 | for (unsigned int j = 0; j < 32; ++j) { |
| 60 | if (0x1 & (value >> j)) { |
| 61 | array->push(index + j); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 67 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 68 | private: |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 69 | std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData; |
| 70 | size_t fDwordCount; // Dword (32-bit) count of the bitset. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 71 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 72 | uint32_t* internalGet(int index) const { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 73 | size_t internalIndex = index / 32; |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 74 | if (internalIndex >= fDwordCount) { |
| 75 | return nullptr; |
| 76 | } |
| 77 | return fBitData.get() + internalIndex; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 78 | } |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | #endif |