vandebo@chromium.org | d3a8c94 | 2011-07-02 01:26:37 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include "SkBitSet.h" |
| 19 | |
| 20 | SkBitSet::SkBitSet(int numberOfBits) |
| 21 | : fBitData(NULL), fDwordCount(0), fBitCount(numberOfBits) { |
| 22 | SkASSERT(numberOfBits > 0); |
| 23 | // Round up size to 32-bit boundary. |
| 24 | fDwordCount = (numberOfBits + 31) / 32; |
| 25 | fBitData.set(malloc(fDwordCount * sizeof(uint32_t))); |
| 26 | clearAll(); |
| 27 | } |
| 28 | |
| 29 | SkBitSet::SkBitSet(const SkBitSet& source) |
| 30 | : fBitData(NULL), fDwordCount(0), fBitCount(0) { |
| 31 | *this = source; |
| 32 | } |
| 33 | |
| 34 | const SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) { |
| 35 | if (this == (SkBitSet*)&rhs) { |
| 36 | return *this; |
| 37 | } |
| 38 | fBitCount = rhs.fBitCount; |
| 39 | fBitData.free(); |
| 40 | fDwordCount = rhs.fDwordCount; |
| 41 | fBitData.set(malloc(fDwordCount * sizeof(uint32_t))); |
| 42 | memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t)); |
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | bool SkBitSet::operator==(const SkBitSet& rhs) { |
| 47 | if (fBitCount == rhs.fBitCount) { |
| 48 | if (fBitData.get() != NULL) { |
| 49 | return (memcmp(fBitData.get(), rhs.fBitData.get(), |
| 50 | fDwordCount * sizeof(uint32_t)) == 0); |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | bool SkBitSet::operator!=(const SkBitSet& rhs) { |
| 58 | return !(*this == rhs); |
| 59 | } |
| 60 | |
| 61 | void SkBitSet::clearAll() { |
| 62 | if (fBitData.get() != NULL) { |
| 63 | sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t)); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void SkBitSet::setBit(int index, bool value) { |
| 68 | uint32_t mask = 1 << (index % 32); |
| 69 | if (value) { |
| 70 | *(internalGet(index)) |= mask; |
| 71 | } else { |
| 72 | *(internalGet(index)) &= ~mask; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | bool SkBitSet::isBitSet(int index) { |
| 77 | uint32_t mask = 1 << (index % 32); |
| 78 | return (*internalGet(index) & mask); |
| 79 | } |
| 80 | |
| 81 | bool SkBitSet::orBits(SkBitSet& source) { |
| 82 | if (fBitCount != source.fBitCount) { |
| 83 | return false; |
| 84 | } |
| 85 | uint32_t* targetBitmap = internalGet(0); |
| 86 | uint32_t* sourceBitmap = source.internalGet(0); |
| 87 | for (size_t i = 0; i < fDwordCount; ++i) { |
| 88 | targetBitmap[i] |= sourceBitmap[i]; |
| 89 | } |
| 90 | return true; |
| 91 | } |