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