1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/utils/SkBitSet.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 + 1.12 + 1.13 +#ifndef SkBitSet_DEFINED 1.14 +#define SkBitSet_DEFINED 1.15 + 1.16 +#include "SkTypes.h" 1.17 +#include "SkTDArray.h" 1.18 + 1.19 +class SkBitSet { 1.20 +public: 1.21 + /** NumberOfBits must be greater than zero. 1.22 + */ 1.23 + explicit SkBitSet(int numberOfBits); 1.24 + explicit SkBitSet(const SkBitSet& source); 1.25 + 1.26 + SkBitSet& operator=(const SkBitSet& rhs); 1.27 + bool operator==(const SkBitSet& rhs); 1.28 + bool operator!=(const SkBitSet& rhs); 1.29 + 1.30 + /** Clear all data. 1.31 + */ 1.32 + void clearAll(); 1.33 + 1.34 + /** Set the value of the index-th bit. 1.35 + */ 1.36 + void setBit(int index, bool value); 1.37 + 1.38 + /** Test if bit index is set. 1.39 + */ 1.40 + bool isBitSet(int index) const; 1.41 + 1.42 + /** Or bits from source. false is returned if this doesn't have the same 1.43 + * bit count as source. 1.44 + */ 1.45 + bool orBits(const SkBitSet& source); 1.46 + 1.47 + /** Export indices of set bits to T array. 1.48 + */ 1.49 + template<typename T> 1.50 + void exportTo(SkTDArray<T>* array) const { 1.51 + SkASSERT(array); 1.52 + uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get()); 1.53 + for (unsigned int i = 0; i < fDwordCount; ++i) { 1.54 + uint32_t value = data[i]; 1.55 + if (value) { // There are set bits 1.56 + unsigned int index = i * 32; 1.57 + for (unsigned int j = 0; j < 32; ++j) { 1.58 + if (0x1 & (value >> j)) { 1.59 + array->push(index + j); 1.60 + } 1.61 + } 1.62 + } 1.63 + } 1.64 + } 1.65 + 1.66 +private: 1.67 + SkAutoFree fBitData; 1.68 + // Dword (32-bit) count of the bitset. 1.69 + size_t fDwordCount; 1.70 + size_t fBitCount; 1.71 + 1.72 + uint32_t* internalGet(int index) const { 1.73 + SkASSERT((size_t)index < fBitCount); 1.74 + size_t internalIndex = index / 32; 1.75 + SkASSERT(internalIndex < fDwordCount); 1.76 + return reinterpret_cast<uint32_t*>(fBitData.get()) + internalIndex; 1.77 + } 1.78 +}; 1.79 + 1.80 + 1.81 +#endif