blob: 0a1ecacf8ab2583074d1fe72afaafcd60a51572e [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +00009#include "SkBitSet.h"
10
11SkBitSet::SkBitSet(int numberOfBits)
halcanary96fcdcc2015-08-27 07:41:13 -070012 : fBitData(nullptr), fDwordCount(0), fBitCount(numberOfBits) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000013 SkASSERT(numberOfBits > 0);
14 // Round up size to 32-bit boundary.
15 fDwordCount = (numberOfBits + 31) / 32;
mtkleine099dd72014-10-09 11:49:30 -070016 fBitData.set(sk_calloc_throw(fDwordCount * sizeof(uint32_t)));
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000017}
18
19SkBitSet::SkBitSet(const SkBitSet& source)
halcanary96fcdcc2015-08-27 07:41:13 -070020 : fBitData(nullptr), fDwordCount(0), fBitCount(0) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000021 *this = source;
22}
23
halcanary3c35fb32016-06-30 11:55:07 -070024SkBitSet::SkBitSet(SkBitSet&& source)
25 : fBitData(source.fBitData.release())
26 , fDwordCount(source.fDwordCount)
27 , fBitCount(source.fBitCount) {
28 source.fDwordCount = 0;
29 source.fBitCount = 0;
30}
31
robertphillips@google.com87379e12013-03-29 12:11:10 +000032SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000033 if (this == &rhs) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000034 return *this;
35 }
36 fBitCount = rhs.fBitCount;
mtklein852f15d2016-03-17 10:51:27 -070037 fBitData.reset();
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000038 fDwordCount = rhs.fDwordCount;
mtkleine099dd72014-10-09 11:49:30 -070039 fBitData.set(sk_malloc_throw(fDwordCount * sizeof(uint32_t)));
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000040 memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t));
41 return *this;
42}
43
44bool SkBitSet::operator==(const SkBitSet& rhs) {
45 if (fBitCount == rhs.fBitCount) {
halcanary96fcdcc2015-08-27 07:41:13 -070046 if (fBitData.get() != nullptr) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000047 return (memcmp(fBitData.get(), rhs.fBitData.get(),
48 fDwordCount * sizeof(uint32_t)) == 0);
49 }
50 return true;
51 }
52 return false;
53}
54
55bool SkBitSet::operator!=(const SkBitSet& rhs) {
56 return !(*this == rhs);
57}
58
59void SkBitSet::clearAll() {
halcanary96fcdcc2015-08-27 07:41:13 -070060 if (fBitData.get() != nullptr) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000061 sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t));
62 }
63}
64
vandebo@chromium.org98594282011-07-25 22:34:12 +000065bool SkBitSet::orBits(const SkBitSet& source) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000066 if (fBitCount != source.fBitCount) {
67 return false;
68 }
mtkleine099dd72014-10-09 11:49:30 -070069 uint32_t* targetBitmap = this->internalGet(0);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000070 uint32_t* sourceBitmap = source.internalGet(0);
71 for (size_t i = 0; i < fDwordCount; ++i) {
72 targetBitmap[i] |= sourceBitmap[i];
73 }
74 return true;
75}