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 #include <UniquePtr.h> 23 24 // --------------------------------------------------------------------------- 25 26 namespace android { 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. 33 34 class SparseBitSet { 35 public: SparseBitSet()36 SparseBitSet(): mMaxVal(0) { 37 } 38 39 // Clear the set 40 void clear(); 41 42 // Initialize the set to a new value, represented by ranges. For 43 // simplicity, these ranges are arranged as pairs of values, 44 // inclusive of start, exclusive of end, laid out in a uint32 array. 45 void initFromRanges(const uint32_t* ranges, size_t nRanges); 46 47 // Determine whether the value is included in the set get(uint32_t ch)48 bool get(uint32_t ch) const { 49 if (ch >= mMaxVal) return false; 50 uint32_t *bitmap = &mBitmaps[mIndices[ch >> kLogValuesPerPage]]; 51 uint32_t index = ch & kPageMask; 52 return (bitmap[index >> kLogBitsPerEl] & (kElFirst >> (index & kElMask))) != 0; 53 } 54 55 // One more than the maximum value in the set, or zero if empty length()56 uint32_t length() const { 57 return mMaxVal; 58 } 59 60 // The next set bit starting at fromIndex, inclusive, or kNotFound 61 // if none exists. 62 uint32_t nextSetBit(uint32_t fromIndex) const; 63 64 static const uint32_t kNotFound = ~0u; 65 66 private: 67 static const int kLogValuesPerPage = 8; 68 static const int kPageMask = (1 << kLogValuesPerPage) - 1; 69 static const int kLogBytesPerEl = 2; 70 static const int kLogBitsPerEl = kLogBytesPerEl + 3; 71 static const int kElMask = (1 << kLogBitsPerEl) - 1; 72 // invariant: sizeof(element) == (1 << kLogBytesPerEl) 73 typedef uint32_t element; 74 static const element kElAllOnes = ~((element)0); 75 static const element kElFirst = ((element)1) << kElMask; 76 static const uint32_t noZeroPage = ~0u; 77 78 static uint32_t calcNumPages(const uint32_t* ranges, size_t nRanges); 79 static int CountLeadingZeros(element x); 80 81 uint32_t mMaxVal; 82 UniquePtr<uint32_t[]> mIndices; 83 UniquePtr<element[]> mBitmaps; 84 uint32_t mZeroPageIndex; 85 }; 86 87 // Note: this thing cannot be used in vectors yet. If that were important, we'd need to 88 // make the copy constructor work, and probably set up move traits as well. 89 90 }; // namespace android 91 92 #endif // MINIKIN_SPARSE_BIT_SET_H 93