blob: 802b9e12acd4e6d0a2379fce43fa882f770e229f [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);
20 explicit SkBitSet(const SkBitSet& source);
21
robertphillips@google.com87379e12013-03-29 12:11:10 +000022 SkBitSet& operator=(const SkBitSet& rhs);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000023 bool operator==(const SkBitSet& rhs);
24 bool operator!=(const SkBitSet& rhs);
25
26 /** Clear all data.
27 */
28 void clearAll();
29
30 /** Set the value of the index-th bit.
31 */
mtkleine099dd72014-10-09 11:49:30 -070032 void setBit(int index, bool value) {
33 uint32_t mask = 1 << (index & 31);
34 uint32_t* chunk = this->internalGet(index);
35 if (value) {
36 *chunk |= mask;
37 } else {
38 *chunk &= ~mask;
39 }
40 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000041
42 /** Test if bit index is set.
43 */
mtkleine099dd72014-10-09 11:49:30 -070044 bool isBitSet(int index) const {
45 uint32_t mask = 1 << (index & 31);
46 return SkToBool(*this->internalGet(index) & mask);
47 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000048
49 /** Or bits from source. false is returned if this doesn't have the same
50 * bit count as source.
51 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000052 bool orBits(const SkBitSet& source);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000053
bungeman@google.comb29c8832011-10-10 13:19:10 +000054 /** Export indices of set bits to T array.
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000055 */
bungeman@google.comb29c8832011-10-10 13:19:10 +000056 template<typename T>
57 void exportTo(SkTDArray<T>* array) const {
58 SkASSERT(array);
59 uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get());
60 for (unsigned int i = 0; i < fDwordCount; ++i) {
61 uint32_t value = data[i];
62 if (value) { // There are set bits
63 unsigned int index = i * 32;
64 for (unsigned int j = 0; j < 32; ++j) {
65 if (0x1 & (value >> j)) {
66 array->push(index + j);
67 }
68 }
69 }
70 }
71 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000072
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000073private:
74 SkAutoFree fBitData;
75 // Dword (32-bit) count of the bitset.
76 size_t fDwordCount;
77 size_t fBitCount;
78
vandebo@chromium.org98594282011-07-25 22:34:12 +000079 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000080 SkASSERT((size_t)index < fBitCount);
81 size_t internalIndex = index / 32;
82 SkASSERT(internalIndex < fDwordCount);
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000083 return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000084 }
85};
86
87
88#endif