blob: 2e2dbebbb277879534692ea5117b30bd451e725e [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
halcanary530032a2016-08-18 14:22:52 -070024 SkBitSet(const SkBitSet&) = delete;
halcanary530032a2016-08-18 14:22:52 -070025 SkBitSet& operator=(const SkBitSet&) = delete;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000026
halcanarye2348cc2016-08-19 16:23:23 -070027 /** Set the value of the index-th bit to true. */
28 void set(int index) {
mtkleine099dd72014-10-09 11:49:30 -070029 uint32_t mask = 1 << (index & 31);
30 uint32_t* chunk = this->internalGet(index);
halcanarye2348cc2016-08-19 16:23:23 -070031 SkASSERT(chunk);
32 *chunk |= mask;
mtkleine099dd72014-10-09 11:49:30 -070033 }
halcanary530032a2016-08-18 14:22:52 -070034
35 template<typename T>
36 void setAll(T* array, int len) {
37 static_assert(std::is_integral<T>::value, "T is integral");
38 for (int i = 0; i < len; ++i) {
39 this->set(static_cast<int>(array[i]));
40 }
41 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000042
halcanarye2348cc2016-08-19 16:23:23 -070043 bool has(int index) const {
44 const uint32_t* chunk = this->internalGet(index);
mtkleine099dd72014-10-09 11:49:30 -070045 uint32_t mask = 1 << (index & 31);
halcanarye2348cc2016-08-19 16:23:23 -070046 return chunk && SkToBool(*chunk & mask);
mtkleine099dd72014-10-09 11:49:30 -070047 }
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000048
halcanarye2348cc2016-08-19 16:23:23 -070049 /** Export indices of set bits to T array. */
bungeman@google.comb29c8832011-10-10 13:19:10 +000050 template<typename T>
51 void exportTo(SkTDArray<T>* array) const {
halcanary530032a2016-08-18 14:22:52 -070052 static_assert(std::is_integral<T>::value, "T is integral");
bungeman@google.comb29c8832011-10-10 13:19:10 +000053 SkASSERT(array);
54 uint32_t* data = reinterpret_cast<uint32_t*>(fBitData.get());
55 for (unsigned int i = 0; i < fDwordCount; ++i) {
56 uint32_t value = data[i];
57 if (value) { // There are set bits
58 unsigned int index = i * 32;
59 for (unsigned int j = 0; j < 32; ++j) {
60 if (0x1 & (value >> j)) {
61 array->push(index + j);
62 }
63 }
64 }
65 }
66 }
vandebo@chromium.org17e66e22011-07-27 20:59:55 +000067
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000068private:
halcanarye2348cc2016-08-19 16:23:23 -070069 std::unique_ptr<uint32_t, SkFunctionWrapper<void, void, sk_free>> fBitData;
70 size_t fDwordCount; // Dword (32-bit) count of the bitset.
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000071
vandebo@chromium.org98594282011-07-25 22:34:12 +000072 uint32_t* internalGet(int index) const {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000073 size_t internalIndex = index / 32;
halcanarye2348cc2016-08-19 16:23:23 -070074 if (internalIndex >= fDwordCount) {
75 return nullptr;
76 }
77 return fBitData.get() + internalIndex;
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000078 }
79};
80
81
82#endif