1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org */
8d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
9ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
10d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org#include "SkBitSet.h"
11d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
12d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.orgSkBitSet::SkBitSet(int numberOfBits)
13d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    : fBitData(NULL), fDwordCount(0), fBitCount(numberOfBits) {
14d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    SkASSERT(numberOfBits > 0);
15d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    // Round up size to 32-bit boundary.
16d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    fDwordCount = (numberOfBits + 31) / 32;
17d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    fBitData.set(malloc(fDwordCount * sizeof(uint32_t)));
18d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    clearAll();
19d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
20d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
21d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.orgSkBitSet::SkBitSet(const SkBitSet& source)
22d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    : fBitData(NULL), fDwordCount(0), fBitCount(0) {
23d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    *this = source;
24d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
25d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
2687379e17c5e95c6fe0d88b3b9ae134355cfafc66robertphillips@google.comSkBitSet& SkBitSet::operator=(const SkBitSet& rhs) {
27a5c7234e81748f76cbeede40e619351146e5286actguil@chromium.org    if (this == &rhs) {
28d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        return *this;
29d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    }
30d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    fBitCount = rhs.fBitCount;
31d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    fBitData.free();
32d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    fDwordCount = rhs.fDwordCount;
33d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    fBitData.set(malloc(fDwordCount * sizeof(uint32_t)));
34d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t));
35d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    return *this;
36d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
37d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
38d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.orgbool SkBitSet::operator==(const SkBitSet& rhs) {
39d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    if (fBitCount == rhs.fBitCount) {
40d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        if (fBitData.get() != NULL) {
41d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org            return (memcmp(fBitData.get(), rhs.fBitData.get(),
42d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org                           fDwordCount * sizeof(uint32_t)) == 0);
43d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        }
44d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        return true;
45d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    }
46d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    return false;
47d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
48d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
49d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.orgbool SkBitSet::operator!=(const SkBitSet& rhs) {
50d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    return !(*this == rhs);
51d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
52d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
53d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.orgvoid SkBitSet::clearAll() {
54d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    if (fBitData.get() != NULL) {
55d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t));
56d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    }
57d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
58d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
59d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.orgvoid SkBitSet::setBit(int index, bool value) {
60d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    uint32_t mask = 1 << (index % 32);
61d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    if (value) {
62d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        *(internalGet(index)) |= mask;
63d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    } else {
64d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        *(internalGet(index)) &= ~mask;
65d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    }
66d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
67d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
689859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.orgbool SkBitSet::isBitSet(int index) const {
69d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    uint32_t mask = 1 << (index % 32);
709d12f5c11b56ef51ba2c95db721ae7e5bab27023bsalomon@google.com    return 0 != (*internalGet(index) & mask);
71d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
72d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org
739859428e71c6041928e6dd741ae3284017e78e81vandebo@chromium.orgbool SkBitSet::orBits(const SkBitSet& source) {
74d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    if (fBitCount != source.fBitCount) {
75d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        return false;
76d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    }
77d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    uint32_t* targetBitmap = internalGet(0);
78d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    uint32_t* sourceBitmap = source.internalGet(0);
79d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    for (size_t i = 0; i < fDwordCount; ++i) {
80d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org        targetBitmap[i] |= sourceBitmap[i];
81d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    }
82d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org    return true;
83d3a8c94dfdabb333b12da3ff796d1f558cb06fbavandebo@chromium.org}
84