blob: 98aa05fb7011356ce1a0db36209a4e1c0dc7a5c5 [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
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000011#include "SkTDArray.h"
halcanarye2348cc2016-08-19 16:23:23 -070012#include "SkTemplates.h"
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000013
14class SkBitSet {
15public:
halcanarye2348cc2016-08-19 16:23:23 -070016 explicit SkBitSet(int numberOfBits) {
halcanarye202bd82016-09-19 10:27:03 -070017 SkASSERT(numberOfBits >= 0);
halcanarye2348cc2016-08-19 16:23:23 -070018 fDwordCount = (numberOfBits + 31) / 32; // Round up size to 32-bit boundary.
halcanarye202bd82016-09-19 10:27:03 -070019 if (fDwordCount > 0) {
20 fBitData.reset((uint32_t*)sk_calloc_throw(fDwordCount * sizeof(uint32_t)));
21 }
halcanarye2348cc2016-08-19 16:23:23 -070022 }
23
halcanarye2348cc2016-08-19 16:23:23 -070024 /** Set the value of the index-th bit to true. */
25 void set(int index) {
mtkleine099dd72014-10-09 11:49:30 -070026 uint32_t mask = 1 << (index & 31);
27 uint32_t* chunk = this->internalGet(index);
halcanarye2348cc2016-08-19 16:23:23 -070028 SkASSERT(chunk);
29 *chunk |= mask;
mtkleine099dd72014-10-09 11:49:30 -070030 }
halcanary530032a2016-08-18 14:22:52 -070031
32 template<typename T>
33 void setAll(T* array, int len) {
34 static_assert(std::is_integral<T>::value, "T is integral");
35 for (int i = 0; i < len; ++i) {
36 this->set(static_cast<int>(array[i]));
37 }
38 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000039
halcanarye2348cc2016-08-19 16:23:23 -070040 bool has(int index) const {
41 const uint32_t* chunk = this->internalGet(index);
mtkleine099dd72014-10-09 11:49:30 -070042 uint32_t mask = 1 << (index & 31);
halcanarye2348cc2016-08-19 16:23:23 -070043 return chunk && SkToBool(*chunk & mask);
mtkleine099dd72014-10-09 11:49:30 -070044 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000045
halcanarye2348cc2016-08-19 16:23:23 -070046 /** Export indices of set bits to T array. */
bungeman@google.comb29c8832011-10-10 13:19:10 +000047 template<typename T>
48 void exportTo(SkTDArray<T>* array) const {
halcanary530032a2016-08-18 14:22:52 -070049 static_assert(std::is_integral<T>::value, "T is integral");
bungeman@google.comb29c8832011-10-10 13:19:10 +000050 SkASSERT(array);
51 uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get());
52 for (unsigned int i = 0; i < fDwordCount; ++i) {
53 uint32_t value = data[i];
54 if (value) { // There are set bits
55 unsigned int index = i * 32;
56 for (unsigned int j = 0; j < 32; ++j) {
57 if (0x1 & (value >> j)) {
58 array->push(index + j);
59 }
60 }
61 }
62 }
63 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000064
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000065private:
halcanarye2348cc2016-08-19 16:23:23 -070066 std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData;
67 size_t fDwordCount; // Dword (32-bit) count of the bitset.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000068
vandebo@chromium.org98594282011-07-25 22:34:12 +000069 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000070 size_t internalIndex = index / 32;
halcanarye2348cc2016-08-19 16:23:23 -070071 if (internalIndex >= fDwordCount) {
72 return nullptr;
73 }
74 return fBitData.get() + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000075 }
76};
77
78
79#endif