SparseBitSet.h revision dff5e5d5a1d0f17a1b5d2fd197b5a6389bc41e28
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 <stdint.h>
21#include <sys/types.h>
22
23#include <memory>
24
25// ---------------------------------------------------------------------------
26
27namespace minikin {
28
29// This is an implementation of a set of integers. It is optimized for
30// values that are somewhat sparse, in the ballpark of a maximum value
31// of thousands to millions. It is particularly efficient when there are
32// large gaps. The motivating example is Unicode coverage of a font, but
33// the abstraction itself is fully general.
34
35class SparseBitSet {
36public:
37    SparseBitSet(): mMaxVal(0), mOwnIndicesAndBitmaps(false) {
38    }
39
40    // Clear the set
41    void clear();
42
43    // Initialize the set to a new value, represented by ranges. For
44    // simplicity, these ranges are arranged as pairs of values,
45    // inclusive of start, exclusive of end, laid out in a uint32 array.
46    void initFromRanges(const uint32_t* ranges, size_t nRanges);
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    static const int kLogValuesPerPage = 8;
69    static const int kPageMask = (1 << kLogValuesPerPage) - 1;
70    static const int kLogBytesPerEl = 2;
71    static const int kLogBitsPerEl = kLogBytesPerEl + 3;
72    static const int kElMask = (1 << kLogBitsPerEl) - 1;
73    // invariant: sizeof(element) == (1 << kLogBytesPerEl)
74    typedef uint32_t element;
75    static const element kElAllOnes = ~((element)0);
76    static const element kElFirst = ((element)1) << kElMask;
77    static const uint32_t noZeroPage = ~0u;
78
79    static uint32_t calcNumPages(const uint32_t* ranges, size_t nRanges);
80    static int CountLeadingZeros(element x);
81
82    uint32_t mMaxVal;
83
84    // True if this SparseBitSet is responsible for freeing mIndices and mBitamps.
85    bool mOwnIndicesAndBitmaps;
86
87    uint32_t mIndexSize;
88    const uint32_t* mIndices;
89    uint32_t mBitmapSize;
90    const element* mBitmaps;
91    uint32_t mZeroPageIndex;
92};
93
94// Note: this thing cannot be used in vectors yet. If that were important, we'd need to
95// make the copy constructor work, and probably set up move traits as well.
96
97}  // namespace minikin
98
99#endif // MINIKIN_SPARSE_BIT_SET_H
100