blob: dbfc80c8d9f79c4541ee67a95753c07d2af53054 [file] [log] [blame]
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00006 */
7
8#ifndef SkBitSet_DEFINED
9#define SkBitSet_DEFINED
10
halcanarye2348cc2016-08-19 16:23:23 -070011#include "SkTemplates.h"
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000012
13class SkBitSet {
14public:
halcanarye2348cc2016-08-19 16:23:23 -070015 explicit SkBitSet(int numberOfBits) {
halcanarye202bd82016-09-19 10:27:03 -070016 SkASSERT(numberOfBits >= 0);
halcanarye2348cc2016-08-19 16:23:23 -070017 fDwordCount = (numberOfBits + 31) / 32; // Round up size to 32-bit boundary.
halcanarye202bd82016-09-19 10:27:03 -070018 if (fDwordCount > 0) {
19 fBitData.reset((uint32_t*)sk_calloc_throw(fDwordCount * sizeof(uint32_t)));
20 }
halcanarye2348cc2016-08-19 16:23:23 -070021 }
22
halcanarye2348cc2016-08-19 16:23:23 -070023 /** Set the value of the index-th bit to true. */
24 void set(int index) {
mtkleine099dd72014-10-09 11:49:30 -070025 uint32_t mask = 1 << (index & 31);
26 uint32_t* chunk = this->internalGet(index);
halcanarye2348cc2016-08-19 16:23:23 -070027 SkASSERT(chunk);
28 *chunk |= mask;
mtkleine099dd72014-10-09 11:49:30 -070029 }
halcanary530032a2016-08-18 14:22:52 -070030
halcanarye2348cc2016-08-19 16:23:23 -070031 bool has(int index) const {
32 const uint32_t* chunk = this->internalGet(index);
mtkleine099dd72014-10-09 11:49:30 -070033 uint32_t mask = 1 << (index & 31);
halcanarye2348cc2016-08-19 16:23:23 -070034 return chunk && SkToBool(*chunk & mask);
mtkleine099dd72014-10-09 11:49:30 -070035 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000036
Hal Canary52514d52018-10-19 10:08:42 -040037 // Calls f(unsigned) for each set value.
38 template<typename FN>
39 void getSetValues(FN f) const {
40 const uint32_t* data = fBitData.get();
41 for (unsigned i = 0; i < fDwordCount; ++i) {
42 if (uint32_t value = data[i]) { // There are set bits
43 unsigned index = i * 32;
44 for (unsigned j = 0; j < 32; ++j) {
bungeman@google.comb29c8832011-10-10 13:19:10 +000045 if (0x1 & (value >> j)) {
Hal Canary52514d52018-10-19 10:08:42 -040046 f(index | j);
bungeman@google.comb29c8832011-10-10 13:19:10 +000047 }
48 }
49 }
50 }
51 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000052
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000053private:
halcanarye2348cc2016-08-19 16:23:23 -070054 std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData;
55 size_t fDwordCount; // Dword (32-bit) count of the bitset.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000056
vandebo@chromium.org98594282011-07-25 22:34:12 +000057 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000058 size_t internalIndex = index / 32;
halcanarye2348cc2016-08-19 16:23:23 -070059 if (internalIndex >= fDwordCount) {
60 return nullptr;
61 }
62 return fBitData.get() + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000063 }
64};
65
66
67#endif