blob: 7d03dfc0679e4016a69fd6801ce02af407bdec61 [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
robertphillips@google.com87379e12013-03-29 12:11:10 +000024SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) {
ctguil@chromium.orga5c72342011-08-15 23:55:03 +000025 if (this == &rhs) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000026 return *this;
27 }
28 fBitCount = rhs.fBitCount;
mtklein852f15d2016-03-17 10:51:27 -070029 fBitData.reset();
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000030 fDwordCount = rhs.fDwordCount;
mtkleine099dd72014-10-09 11:49:30 -070031 fBitData.set(sk_malloc_throw(fDwordCount * sizeof(uint32_t)));
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000032 memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t));
33 return *this;
34}
35
36bool SkBitSet::operator==(const SkBitSet& rhs) {
37 if (fBitCount == rhs.fBitCount) {
halcanary96fcdcc2015-08-27 07:41:13 -070038 if (fBitData.get() != nullptr) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000039 return (memcmp(fBitData.get(), rhs.fBitData.get(),
40 fDwordCount * sizeof(uint32_t)) == 0);
41 }
42 return true;
43 }
44 return false;
45}
46
47bool SkBitSet::operator!=(const SkBitSet& rhs) {
48 return !(*this == rhs);
49}
50
51void SkBitSet::clearAll() {
halcanary96fcdcc2015-08-27 07:41:13 -070052 if (fBitData.get() != nullptr) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000053 sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t));
54 }
55}
56
vandebo@chromium.org98594282011-07-25 22:34:12 +000057bool SkBitSet::orBits(const SkBitSet& source) {
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000058 if (fBitCount != source.fBitCount) {
59 return false;
60 }
mtkleine099dd72014-10-09 11:49:30 -070061 uint32_t* targetBitmap = this->internalGet(0);
vandebo@chromium.orgd3a8c942011-07-02 01:26:37 +000062 uint32_t* sourceBitmap = source.internalGet(0);
63 for (size_t i = 0; i < fDwordCount; ++i) {
64 targetBitmap[i] |= sourceBitmap[i];
65 }
66 return true;
67}