blob: 1e35c9120f47f3862efa56a9490747ab1907379b [file] [log] [blame]
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00009#ifndef SkBitSet_DEFINED
10#define SkBitSet_DEFINED
11
12#include "SkTypes.h"
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000013#include "SkTDArray.h"
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000014
15class SkBitSet {
16public:
17 /** NumberOfBits must be greater than zero.
18 */
19 explicit SkBitSet(int numberOfBits);
halcanary530032a2016-08-18 14:22:52 -070020 SkBitSet(const SkBitSet&) = delete;
21 SkBitSet(SkBitSet&&);
22 SkBitSet& operator=(const SkBitSet&) = delete;
23 SkBitSet& operator=(SkBitSet&& rhs);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000024
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000025 bool operator==(const SkBitSet& rhs);
26 bool operator!=(const SkBitSet& rhs);
27
28 /** Clear all data.
29 */
30 void clearAll();
31
32 /** Set the value of the index-th bit.
33 */
mtkleine099dd72014-10-09 11:49:30 -070034 void setBit(int index, bool value) {
35 uint32_t mask = 1 << (index & 31);
36 uint32_t* chunk = this->internalGet(index);
37 if (value) {
38 *chunk |= mask;
39 } else {
40 *chunk &= ~mask;
41 }
42 }
halcanary530032a2016-08-18 14:22:52 -070043 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 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000052
53 /** Test if bit index is set.
54 */
mtkleine099dd72014-10-09 11:49:30 -070055 bool isBitSet(int index) const {
56 uint32_t mask = 1 << (index & 31);
57 return SkToBool(*this->internalGet(index) & mask);
58 }
halcanary530032a2016-08-18 14:22:52 -070059 bool has(int index) const { return this->isBitSet(index); }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000060
61 /** Or bits from source. false is returned if this doesn't have the same
62 * bit count as source.
63 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000064 bool orBits(const SkBitSet& source);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000065
bungeman@google.comb29c8832011-10-10 13:19:10 +000066 /** Export indices of set bits to T array.
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000067 */
bungeman@google.comb29c8832011-10-10 13:19:10 +000068 template<typename T>
69 void exportTo(SkTDArray<T>* array) const {
halcanary530032a2016-08-18 14:22:52 -070070 static_assert(std::is_integral<T>::value, "T is integral");
bungeman@google.comb29c8832011-10-10 13:19:10 +000071 SkASSERT(array);
72 uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get());
73 for (unsigned int i = 0; i < fDwordCount; ++i) {
74 uint32_t value = data[i];
75 if (value) { // There are set bits
76 unsigned int index = i * 32;
77 for (unsigned int j = 0; j < 32; ++j) {
78 if (0x1 & (value >> j)) {
79 array->push(index + j);
80 }
81 }
82 }
83 }
84 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000085
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000086private:
87 SkAutoFree fBitData;
88 // Dword (32-bit) count of the bitset.
89 size_t fDwordCount;
90 size_t fBitCount;
91
vandebo@chromium.org98594282011-07-25 22:34:12 +000092 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000093 SkASSERT((size_t)index < fBitCount);
94 size_t internalIndex = index / 32;
95 SkASSERT(internalIndex < fDwordCount);
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000096 return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000097 }
98};
99
100
101#endif