michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkBitSet_DEFINED michael@0: #define SkBitSet_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: #include "SkTDArray.h" michael@0: michael@0: class SkBitSet { michael@0: public: michael@0: /** NumberOfBits must be greater than zero. michael@0: */ michael@0: explicit SkBitSet(int numberOfBits); michael@0: explicit SkBitSet(const SkBitSet& source); michael@0: michael@0: SkBitSet& operator=(const SkBitSet& rhs); michael@0: bool operator==(const SkBitSet& rhs); michael@0: bool operator!=(const SkBitSet& rhs); michael@0: michael@0: /** Clear all data. michael@0: */ michael@0: void clearAll(); michael@0: michael@0: /** Set the value of the index-th bit. michael@0: */ michael@0: void setBit(int index, bool value); michael@0: michael@0: /** Test if bit index is set. michael@0: */ michael@0: bool isBitSet(int index) const; michael@0: michael@0: /** Or bits from source. false is returned if this doesn't have the same michael@0: * bit count as source. michael@0: */ michael@0: bool orBits(const SkBitSet& source); michael@0: michael@0: /** Export indices of set bits to T array. michael@0: */ michael@0: template michael@0: void exportTo(SkTDArray* array) const { michael@0: SkASSERT(array); michael@0: uint32_t* data = reinterpret_cast(fBitData.get()); michael@0: for (unsigned int i = 0; i < fDwordCount; ++i) { michael@0: uint32_t value = data[i]; michael@0: if (value) { // There are set bits michael@0: unsigned int index = i * 32; michael@0: for (unsigned int j = 0; j < 32; ++j) { michael@0: if (0x1 & (value >> j)) { michael@0: array->push(index + j); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: private: michael@0: SkAutoFree fBitData; michael@0: // Dword (32-bit) count of the bitset. michael@0: size_t fDwordCount; michael@0: size_t fBitCount; michael@0: michael@0: uint32_t* internalGet(int index) const { michael@0: SkASSERT((size_t)index < fBitCount); michael@0: size_t internalIndex = index / 32; michael@0: SkASSERT(internalIndex < fDwordCount); michael@0: return reinterpret_cast(fBitData.get()) + internalIndex; michael@0: } michael@0: }; michael@0: michael@0: michael@0: #endif