epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 10 | #include "SkBitSet.h" |
| 11 | |
| 12 | SkBitSet::SkBitSet(int numberOfBits) |
| 13 | : fBitData(NULL), fDwordCount(0), fBitCount(numberOfBits) { |
| 14 | SkASSERT(numberOfBits > 0); |
| 15 | // Round up size to 32-bit boundary. |
| 16 | fDwordCount = (numberOfBits + 31) / 32; |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 17 | fBitData.set(sk_calloc_throw(fDwordCount * sizeof(uint32_t))); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | SkBitSet::SkBitSet(const SkBitSet& source) |
| 21 | : fBitData(NULL), fDwordCount(0), fBitCount(0) { |
| 22 | *this = source; |
| 23 | } |
| 24 | |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 25 | SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) { |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 26 | if (this == &rhs) { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 27 | return *this; |
| 28 | } |
| 29 | fBitCount = rhs.fBitCount; |
| 30 | fBitData.free(); |
| 31 | fDwordCount = rhs.fDwordCount; |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 32 | fBitData.set(sk_malloc_throw(fDwordCount * sizeof(uint32_t))); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 33 | memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t)); |
| 34 | return *this; |
| 35 | } |
| 36 | |
| 37 | bool SkBitSet::operator==(const SkBitSet& rhs) { |
| 38 | if (fBitCount == rhs.fBitCount) { |
| 39 | if (fBitData.get() != NULL) { |
| 40 | return (memcmp(fBitData.get(), rhs.fBitData.get(), |
| 41 | fDwordCount * sizeof(uint32_t)) == 0); |
| 42 | } |
| 43 | return true; |
| 44 | } |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | bool SkBitSet::operator!=(const SkBitSet& rhs) { |
| 49 | return !(*this == rhs); |
| 50 | } |
| 51 | |
| 52 | void SkBitSet::clearAll() { |
| 53 | if (fBitData.get() != NULL) { |
| 54 | sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t)); |
| 55 | } |
| 56 | } |
| 57 | |
vandebo@chromium.org | 9859428 | 2011-07-25 22:34:12 +0000 | [diff] [blame] | 58 | bool SkBitSet::orBits(const SkBitSet& source) { |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 59 | if (fBitCount != source.fBitCount) { |
| 60 | return false; |
| 61 | } |
mtklein | e099dd7 | 2014-10-09 11:49:30 -0700 | [diff] [blame] | 62 | uint32_t* targetBitmap = this->internalGet(0); |
vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame] | 63 | uint32_t* sourceBitmap = source.internalGet(0); |
| 64 | for (size_t i = 0; i < fDwordCount; ++i) { |
| 65 | targetBitmap[i] |= sourceBitmap[i]; |
| 66 | } |
| 67 | return true; |
| 68 | } |