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