blob: b72a895ccc690ad050d96236268a915afd843e43 [file] [log] [blame]
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00001/*
2 * Copyright (C) 2011 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkBitSet_DEFINED
18#define SkBitSet_DEFINED
19
20#include "SkTypes.h"
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000021#include "SkTDArray.h"
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000022
23class SkBitSet {
24public:
25 /** NumberOfBits must be greater than zero.
26 */
27 explicit SkBitSet(int numberOfBits);
28 explicit SkBitSet(const SkBitSet& source);
29
30 const SkBitSet& operator=(const SkBitSet& rhs);
31 bool operator==(const SkBitSet& rhs);
32 bool operator!=(const SkBitSet& rhs);
33
34 /** Clear all data.
35 */
36 void clearAll();
37
38 /** Set the value of the index-th bit.
39 */
40 void setBit(int index, bool value);
41
42 /** Test if bit index is set.
43 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000044 bool isBitSet(int index) const;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000045
46 /** Or bits from source. false is returned if this doesn't have the same
47 * bit count as source.
48 */
vandebo@chromium.org98594282011-07-25 22:34:12 +000049 bool orBits(const SkBitSet& source);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000050
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000051 /** Export set bits to unsigned int array. (used in font subsetting)
52 */
53 void exportTo(SkTDArray<uint32_t>* array) const;
54
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000055private:
56 SkAutoFree fBitData;
57 // Dword (32-bit) count of the bitset.
58 size_t fDwordCount;
59 size_t fBitCount;
60
vandebo@chromium.org98594282011-07-25 22:34:12 +000061 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000062 SkASSERT((size_t)index < fBitCount);
63 size_t internalIndex = index / 32;
64 SkASSERT(internalIndex < fDwordCount);
65 return (uint32_t*)fBitData.get() + internalIndex;
66 }
67};
68
69
70#endif