blob: 266fd87496255ec1d2f9a528933ae0bbc32b96f3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000010#ifndef SkBitSet_DEFINED
11#define SkBitSet_DEFINED
12
13#include "SkTypes.h"
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000014#include "SkTDArray.h"
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000015
16class SkBitSet {
17public:
18 /** NumberOfBits must be greater than zero.
19 */
20 explicit SkBitSet(int numberOfBits);
21 explicit SkBitSet(const SkBitSet& source);
22
robertphillips@google.com87379e12013-03-29 12:11:10 +000023 SkBitSet& operator=(const SkBitSet& rhs);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000024 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 */
mtkleine099dd72014-10-09 11:49:30 -070033 void setBit(int index, bool value) {
34 uint32_t mask = 1 << (index & 31);
35 uint32_t* chunk = this->internalGet(index);
36 if (value) {
37 *chunk |= mask;
38 } else {
39 *chunk &= ~mask;
40 }
41 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000042
43 /** Test if bit index is set.
44 */
mtkleine099dd72014-10-09 11:49:30 -070045 bool isBitSet(int index) const {
46 uint32_t mask = 1 << (index & 31);
47 return SkToBool(*this->internalGet(index) & mask);
48 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000049
50 /** Or bits from source. false is returned if this doesn't have the same
51 * bit count as source.
52 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000053 bool orBits(const SkBitSet& source);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000054
bungeman@google.comb29c8832011-10-10 13:19:10 +000055 /** Export indices of set bits to T array.
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000056 */
bungeman@google.comb29c8832011-10-10 13:19:10 +000057 template<typename T>
58 void exportTo(SkTDArray<T>* array) const {
59 SkASSERT(array);
60 uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get());
61 for (unsigned int i = 0; i < fDwordCount; ++i) {
62 uint32_t value = data[i];
63 if (value) { // There are set bits
64 unsigned int index = i * 32;
65 for (unsigned int j = 0; j < 32; ++j) {
66 if (0x1 & (value >> j)) {
67 array->push(index + j);
68 }
69 }
70 }
71 }
72 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000073
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000074private:
75 SkAutoFree fBitData;
76 // Dword (32-bit) count of the bitset.
77 size_t fDwordCount;
78 size_t fBitCount;
79
vandebo@chromium.org98594282011-07-25 22:34:12 +000080 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000081 SkASSERT((size_t)index < fBitCount);
82 size_t internalIndex = index / 32;
83 SkASSERT(internalIndex < fDwordCount);
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000084 return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000085 }
86};
87
88
89#endif