blob: e113fd70044eb148302977ae70728565f6931426 [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 */
33 void setBit(int index, bool value);
34
35 /** Test if bit index is set.
36 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000037 bool isBitSet(int index) const;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000038
39 /** Or bits from source. false is returned if this doesn't have the same
40 * bit count as source.
41 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000042 bool orBits(const SkBitSet& source);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000043
bungeman@google.comb29c8832011-10-10 13:19:10 +000044 /** Export indices of set bits to T array.
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000045 */
bungeman@google.comb29c8832011-10-10 13:19:10 +000046 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.org17e66e22011-07-27 20:59:55 +000062
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000063private:
64 SkAutoFree fBitData;
65 // Dword (32-bit) count of the bitset.
66 size_t fDwordCount;
67 size_t fBitCount;
68
vandebo@chromium.org98594282011-07-25 22:34:12 +000069 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000070 SkASSERT((size_t)index < fBitCount);
71 size_t internalIndex = index / 32;
72 SkASSERT(internalIndex < fDwordCount);
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000073 return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000074 }
75};
76
77
78#endif