2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "FixedArray.h"
23 #include "StdLibExtras.h"
32 typedef uint32_t WordType;
37 bool get(size_t) const;
39 bool testAndSet(size_t);
40 bool testAndClear(size_t);
41 size_t nextPossiblyUnset(size_t) const;
44 int64_t findRunOfZeros(size_t) const;
45 size_t count(size_t = 0) const;
46 size_t isEmpty() const;
47 size_t isFull() const;
50 static const WordType wordSize = sizeof(WordType) * 8;
51 static const WordType words = (size + wordSize - 1) / wordSize;
53 // the literal '1' is of type signed int. We want to use an unsigned
54 // version of the correct size when doing the calculations because if
55 // WordType is larger than int, '1 << 31' will first be sign extended
56 // and then casted to unsigned, meaning that set(31) when WordType is
57 // a 64 bit unsigned int would give 0xffff8000
58 static const WordType one = 1;
60 FixedArray<WordType, words> bits;
64 inline Bitmap<size>::Bitmap()
70 inline bool Bitmap<size>::get(size_t n) const
72 return !!(bits[n / wordSize] & (one << (n % wordSize)));
76 inline void Bitmap<size>::set(size_t n)
78 bits[n / wordSize] |= (one << (n % wordSize));
82 inline bool Bitmap<size>::testAndSet(size_t n)
84 WordType mask = one << (n % wordSize);
85 size_t index = n / wordSize;
86 bool result = bits[index] & mask;
92 inline bool Bitmap<size>::testAndClear(size_t n)
94 WordType mask = one << (n % wordSize);
95 size_t index = n / wordSize;
96 bool result = bits[index] & mask;
101 template<size_t size>
102 inline void Bitmap<size>::clear(size_t n)
104 bits[n / wordSize] &= ~(one << (n % wordSize));
107 template<size_t size>
108 inline void Bitmap<size>::clearAll()
110 memset(bits.data(), 0, sizeof(bits));
113 template<size_t size>
114 inline size_t Bitmap<size>::nextPossiblyUnset(size_t start) const
116 if (!~bits[start / wordSize])
117 return ((start / wordSize) + 1) * wordSize;
121 template<size_t size>
122 inline int64_t Bitmap<size>::findRunOfZeros(size_t runLength) const
127 for (size_t i = 0; i <= (size - runLength) ; i++) {
129 for (size_t j = i; j <= (i + runLength - 1) ; j++) {
141 template<size_t size>
142 inline size_t Bitmap<size>::count(size_t start) const
145 for ( ; (start % wordSize); ++start) {
149 for (size_t i = start / wordSize; i < words; ++i)
150 result += WTF::bitCount(bits[i]);
154 template<size_t size>
155 inline size_t Bitmap<size>::isEmpty() const
157 for (size_t i = 0; i < words; ++i)
163 template<size_t size>
164 inline size_t Bitmap<size>::isFull() const
166 for (size_t i = 0; i < words; ++i)