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 | */ |
| 33 | void setBit(int index, bool value); |
| 34 | |
| 35 | /** Test if bit index is set. |
| 36 | */ |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 37 | bool isBitSet(int index) const; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 38 | |
| 39 | /** Or bits from source. false is returned if this doesn't have the same |
| 40 | * bit count as source. |
| 41 | */ |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 42 | bool orBits(const SkBitSet& source); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 43 | |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 44 | /** Export indices of set bits to T array. |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 45 | */ |
bungeman@google.com | b29c883 | 2011-10-10 13:19:10 +0000 | [diff] [blame] | 46 | template<typename T> |
| 47 | void exportTo(SkTDArray<T>* array) const { |
| 48 | SkASSERT(array); |
| 49 | uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get()); |
| 50 | for (unsigned int i = 0; i < fDwordCount; ++i) { |
| 51 | uint32_t value = data[i]; |
| 52 | if (value) { // There are set bits |
| 53 | unsigned int index = i * 32; |
| 54 | for (unsigned int j = 0; j < 32; ++j) { |
| 55 | if (0x1 & (value >> j)) { |
| 56 | array->push(index + j); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | } |
vandebo@chromium.org | 17e66e2 | 2011-07-27 20:59:55 +0000 | [diff] [blame] | 62 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 63 | private: |
| 64 | SkAutoFree fBitData; |
| 65 | // Dword (32-bit) count of the bitset. |
| 66 | size_t fDwordCount; |
| 67 | size_t fBitCount; |
| 68 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 69 | uint32_t* internalGet(int index) const { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 70 | SkASSERT((size_t)index < fBitCount); |
| 71 | size_t internalIndex = index / 32; |
| 72 | SkASSERT(internalIndex < fDwordCount); |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 73 | return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex; |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | |
| 78 | #endif |