blob: a8585a20b9ea9d30820ca9ad7b3c64d1d5f52bdd [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) {
17 SkASSERT(numberOfBits > 0);
18 fDwordCount = (numberOfBits + 31) / 32; // Round up size to 32-bit boundary.
19 fBitData.reset((uint32_t*)sk_calloc_throw(fDwordCount * sizeof(uint32_t)));
20 }
21
halcanary530032a2016-08-18 14:22:52 -070022 SkBitSet(const SkBitSet&) = delete;
halcanary530032a2016-08-18 14:22:52 -070023 SkBitSet& operator=(const SkBitSet&) = delete;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000024
halcanarye2348cc2016-08-19 16:23:23 -070025 /** Set the value of the index-th bit to true. */
26 void set(int index) {
mtkleine099dd72014-10-09 11:49:30 -070027 uint32_t mask = 1 << (index & 31);
28 uint32_t* chunk = this->internalGet(index);
halcanarye2348cc2016-08-19 16:23:23 -070029 SkASSERT(chunk);
30 *chunk |= mask;
mtkleine099dd72014-10-09 11:49:30 -070031 }
halcanary530032a2016-08-18 14:22:52 -070032
33 template<typename T>
34 void setAll(T* array, int len) {
35 static_assert(std::is_integral<T>::value, "T is integral");
36 for (int i = 0; i < len; ++i) {
37 this->set(static_cast<int>(array[i]));
38 }
39 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000040
halcanarye2348cc2016-08-19 16:23:23 -070041 bool has(int index) const {
42 const uint32_t* chunk = this->internalGet(index);
mtkleine099dd72014-10-09 11:49:30 -070043 uint32_t mask = 1 << (index & 31);
halcanarye2348cc2016-08-19 16:23:23 -070044 return chunk && SkToBool(*chunk & mask);
mtkleine099dd72014-10-09 11:49:30 -070045 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000046
halcanarye2348cc2016-08-19 16:23:23 -070047 /** Export indices of set bits to T array. */
bungeman@google.comb29c8832011-10-10 13:19:10 +000048 template<typename T>
49 void exportTo(SkTDArray<T>* array) const {
halcanary530032a2016-08-18 14:22:52 -070050 static_assert(std::is_integral<T>::value, "T is integral");
bungeman@google.comb29c8832011-10-10 13:19:10 +000051 SkASSERT(array);
52 uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get());
53 for (unsigned int i = 0; i < fDwordCount; ++i) {
54 uint32_t value = data[i];
55 if (value) { // There are set bits
56 unsigned int index = i * 32;
57 for (unsigned int j = 0; j < 32; ++j) {
58 if (0x1 & (value >> j)) {
59 array->push(index + j);
60 }
61 }
62 }
63 }
64 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000065
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000066private:
halcanarye2348cc2016-08-19 16:23:23 -070067 std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData;
68 size_t fDwordCount; // Dword (32-bit) count of the bitset.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000069
vandebo@chromium.org98594282011-07-25 22:34:12 +000070 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000071 size_t internalIndex = index / 32;
halcanarye2348cc2016-08-19 16:23:23 -070072 if (internalIndex >= fDwordCount) {
73 return nullptr;
74 }
75 return fBitData.get() + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000076 }
77};
78
79
80#endif