SparseBitSet.h revision fbbc5a6b361c623e47a433f83e7200b4e2ba3501
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    // Initializes the set with pre-calculted data. Returns false if the serialized data is invalid.
49    // Even if this function returns false, the internal data is cleared.
50    bool initFromBuffer(const uint8_t* data, size_t size);
51
52    // Serialize the set and write into out.
53    //
54    // This method returns the number of bytes written to the buffer. By calling the method with
55    // 'out' set to nullptr, the method just returns the size needed, which the caller can then use
56    // for allocating a buffer for a second call.
57    size_t writeToBuffer(uint8_t* out) const;
58
59    // Determine whether the value is included in the set
60    bool get(uint32_t ch) const {
61        if (ch >= mMaxVal) return false;
62        const uint32_t *bitmap = &mBitmaps[mIndices[ch >> kLogValuesPerPage]];
63        uint32_t index = ch & kPageMask;
64        return (bitmap[index >> kLogBitsPerEl] & (kElFirst >> (index & kElMask))) != 0;
65    }
66
67    // One more than the maximum value in the set, or zero if empty
68    uint32_t length() const {
69        return mMaxVal;
70    }
71
72    // The next set bit starting at fromIndex, inclusive, or kNotFound
73    // if none exists.
74    uint32_t nextSetBit(uint32_t fromIndex) const;
75
76    static const uint32_t kNotFound = ~0u;
77
78private:
79    static const int kLogValuesPerPage = 8;
80    static const int kPageMask = (1 << kLogValuesPerPage) - 1;
81    static const int kLogBytesPerEl = 2;
82    static const int kLogBitsPerEl = kLogBytesPerEl + 3;
83    static const int kElMask = (1 << kLogBitsPerEl) - 1;
84    // invariant: sizeof(element) == (1 << kLogBytesPerEl)
85    typedef uint32_t element;
86    static const element kElAllOnes = ~((element)0);
87    static const element kElFirst = ((element)1) << kElMask;
88    static const uint32_t noZeroPage = ~0u;
89
90    static uint32_t calcNumPages(const uint32_t* ranges, size_t nRanges);
91    static int CountLeadingZeros(element x);
92
93    uint32_t mMaxVal;
94
95    // True if this SparseBitSet is responsible for freeing mIndices and mBitamps.
96    bool mOwnIndicesAndBitmaps;
97
98    uint32_t mIndexSize;
99    const uint32_t* mIndices;
100    uint32_t mBitmapSize;
101    const element* mBitmaps;
102    uint32_t mZeroPageIndex;
103};
104
105// Note: this thing cannot be used in vectors yet. If that were important, we'd need to
106// make the copy constructor work, and probably set up move traits as well.
107
108}  // namespace minikin
109
110#endif // MINIKIN_SPARSE_BIT_SET_H
111