SparseBitSet.h revision 1d461589869ee5b7102f96271b0ef0a776ab513c
1/*
2 * Copyright (C) 2012 The Android Open Source Project
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#ifndef MINIKIN_SPARSE_BIT_SET_H
18#define MINIKIN_SPARSE_BIT_SET_H
19
20#include <sys/types.h>
21#include <cstdint>
22#include <memory>
23
24// ---------------------------------------------------------------------------
25
26namespace minikin {
27
28// This is an implementation of a set of integers. It is optimized for
29// values that are somewhat sparse, in the ballpark of a maximum value
30// of thousands to millions. It is particularly efficient when there are
31// large gaps. The motivating example is Unicode coverage of a font, but
32// the abstraction itself is fully general.
33class SparseBitSet {
34public:
35    // Create an empty bit set.
36    SparseBitSet() : mMaxVal(0) {}
37
38    // Initialize the set to a new value, represented by ranges. For
39    // simplicity, these ranges are arranged as pairs of values,
40    // inclusive of start, exclusive of end, laid out in a uint32 array.
41    SparseBitSet(const uint32_t* ranges, size_t nRanges) : SparseBitSet() {
42        initFromRanges(ranges, nRanges);
43    }
44
45    SparseBitSet(SparseBitSet&&) = default;
46    SparseBitSet& operator=(SparseBitSet&&) = default;
47
48    // Determine whether the value is included in the set
49    bool get(uint32_t ch) const {
50        if (ch >= mMaxVal) return false;
51        const uint32_t *bitmap = &mBitmaps[mIndices[ch >> kLogValuesPerPage]];
52        uint32_t index = ch & kPageMask;
53        return (bitmap[index >> kLogBitsPerEl] & (kElFirst >> (index & kElMask))) != 0;
54    }
55
56    // One more than the maximum value in the set, or zero if empty
57    uint32_t length() const {
58        return mMaxVal;
59    }
60
61    // The next set bit starting at fromIndex, inclusive, or kNotFound
62    // if none exists.
63    uint32_t nextSetBit(uint32_t fromIndex) const;
64
65    static const uint32_t kNotFound = ~0u;
66
67private:
68    void initFromRanges(const uint32_t* ranges, size_t nRanges);
69
70    static const uint32_t kMaximumCapacity = 0xFFFFFF;
71    static const int kLogValuesPerPage = 8;
72    static const int kPageMask = (1 << kLogValuesPerPage) - 1;
73    static const int kLogBytesPerEl = 2;
74    static const int kLogBitsPerEl = kLogBytesPerEl + 3;
75    static const int kElMask = (1 << kLogBitsPerEl) - 1;
76    // invariant: sizeof(element) == (1 << kLogBytesPerEl)
77    typedef uint32_t element;
78    static const element kElAllOnes = ~((element)0);
79    static const element kElFirst = ((element)1) << kElMask;
80    static const uint16_t noZeroPage = 0xFFFF;
81
82    static uint32_t calcNumPages(const uint32_t* ranges, size_t nRanges);
83    static int CountLeadingZeros(element x);
84
85    uint32_t mMaxVal;
86
87    std::unique_ptr<uint16_t[]> mIndices;
88    std::unique_ptr<element[]> mBitmaps;
89    uint16_t mZeroPageIndex;
90
91    // Forbid copy and assign.
92    SparseBitSet(const SparseBitSet&) = delete;
93    void operator=(const SparseBitSet&) = delete;
94};
95
96}  // namespace minikin
97
98#endif // MINIKIN_SPARSE_BIT_SET_H
99