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 | |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 11 | #include "SkTemplates.h" |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 12 | |
| 13 | class SkBitSet { |
| 14 | public: |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 15 | explicit SkBitSet(int numberOfBits) { |
halcanary | e202bd8 | 2016-09-19 10:27:03 -0700 | [diff] [blame] | 16 | SkASSERT(numberOfBits >= 0); |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 17 | fDwordCount = (numberOfBits + 31) / 32; // Round up size to 32-bit boundary. |
halcanary | e202bd8 | 2016-09-19 10:27:03 -0700 | [diff] [blame] | 18 | if (fDwordCount > 0) { |
| 19 | fBitData.reset((uint32_t*)sk_calloc_throw(fDwordCount * sizeof(uint32_t))); |
| 20 | } |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 21 | } |
| 22 | |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 23 | /** Set the value of the index-th bit to true. */ |
| 24 | void set(int index) { |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 25 | uint32_t mask = 1 << (index & 31); |
| 26 | uint32_t* chunk = this->internalGet(index); |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 27 | SkASSERT(chunk); |
| 28 | *chunk |= mask; |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 29 | } |
halcanary | 530032a | 2016-08-18 14:22:52 -0700 | [diff] [blame] | 30 | |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 31 | bool has(int index) const { |
| 32 | const uint32_t* chunk = this->internalGet(index); |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 33 | uint32_t mask = 1 << (index & 31); |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 34 | return chunk && SkToBool(*chunk & mask); |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 35 | } |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 36 | |
Hal Canary | 52514d5 | 2018-10-19 10:08:42 -0400 | [diff] [blame] | 37 | // Calls f(unsigned) for each set value. |
| 38 | template<typename FN> |
| 39 | void getSetValues(FN f) const { |
| 40 | const uint32_t* data = fBitData.get(); |
| 41 | for (unsigned i = 0; i < fDwordCount; ++i) { |
| 42 | if (uint32_t value = data[i]) { // There are set bits |
| 43 | unsigned index = i * 32; |
| 44 | for (unsigned j = 0; j < 32; ++j) { |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 45 | if (0x1 & (value >> j)) { |
Hal Canary | 52514d5 | 2018-10-19 10:08:42 -0400 | [diff] [blame] | 46 | f(index | j); |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 52 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 53 | private: |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 54 | std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData; |
| 55 | size_t fDwordCount; // Dword (32-bit) count of the bitset. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 56 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 57 | uint32_t* internalGet(int index) const { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 58 | size_t internalIndex = index / 32; |
halcanary | e2348cc | 2016-08-19 16:23:23 -0700 | [diff] [blame] | 59 | if (internalIndex >= fDwordCount) { |
| 60 | return nullptr; |
| 61 | } |
| 62 | return fBitData.get() + internalIndex; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | #endif |