aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkBitSet.cpp
blob: 7d03dfc0679e4016a69fd6801ce02af407bdec61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkBitSet.h"

SkBitSet::SkBitSet(int numberOfBits)
    : fBitData(nullptr), fDwordCount(0), fBitCount(numberOfBits) {
    SkASSERT(numberOfBits > 0);
    // Round up size to 32-bit boundary.
    fDwordCount = (numberOfBits + 31) / 32;
    fBitData.set(sk_calloc_throw(fDwordCount * sizeof(uint32_t)));
}

SkBitSet::SkBitSet(const SkBitSet& source)
    : fBitData(nullptr), fDwordCount(0), fBitCount(0) {
    *this = source;
}

SkBitSet& SkBitSet::operator=(const SkBitSet& rhs) {
    if (this == &rhs) {
        return *this;
    }
    fBitCount = rhs.fBitCount;
    fBitData.reset();
    fDwordCount = rhs.fDwordCount;
    fBitData.set(sk_malloc_throw(fDwordCount * sizeof(uint32_t)));
    memcpy(fBitData.get(), rhs.fBitData.get(), fDwordCount * sizeof(uint32_t));
    return *this;
}

bool SkBitSet::operator==(const SkBitSet& rhs) {
    if (fBitCount == rhs.fBitCount) {
        if (fBitData.get() != nullptr) {
            return (memcmp(fBitData.get(), rhs.fBitData.get(),
                           fDwordCount * sizeof(uint32_t)) == 0);
        }
        return true;
    }
    return false;
}

bool SkBitSet::operator!=(const SkBitSet& rhs) {
    return !(*this == rhs);
}

void SkBitSet::clearAll() {
    if (fBitData.get() != nullptr) {
        sk_bzero(fBitData.get(), fDwordCount * sizeof(uint32_t));
    }
}

bool SkBitSet::orBits(const SkBitSet& source) {
    if (fBitCount != source.fBitCount) {
        return false;
    }
    uint32_t* targetBitmap = this->internalGet(0);
    uint32_t* sourceBitmap = source.internalGet(0);
    for (size_t i = 0; i < fDwordCount; ++i) {
        targetBitmap[i] |= sourceBitmap[i];
    }
    return true;
}