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;
41 void advanceToNextFreeBit(size_t&) const;
42 size_t count(size_t = 0) const;
43 size_t isEmpty() const;
44 size_t isFull() const;
47 static const WordType wordSize = sizeof(WordType) * 8;
48 static const WordType words = (size + wordSize - 1) / wordSize;
50 // the literal '1' is of type signed int. We want to use an unsigned
51 // version of the correct size when doing the calculations because if
52 // WordType is larger than int, '1 << 31' will first be sign extended
53 // and then casted to unsigned, meaning that set(31) when WordType is
54 // a 64 bit unsigned int would give 0xffff8000
55 static const WordType one = 1;
57 FixedArray<WordType, words> bits;
61 inline Bitmap<size>::Bitmap()
67 inline bool Bitmap<size>::get(size_t n) const
69 return !!(bits[n / wordSize] & (one << (n % wordSize)));
73 inline void Bitmap<size>::set(size_t n)
75 bits[n / wordSize] |= (one << (n % wordSize));
79 inline void Bitmap<size>::clear(size_t n)
81 bits[n / wordSize] &= ~(one << (n % wordSize));
85 inline void Bitmap<size>::clearAll()
87 memset(bits.data(), 0, sizeof(bits));
91 inline void Bitmap<size>::advanceToNextFreeBit(size_t& start) const
93 if (!~bits[start / wordSize])
94 start = ((start / wordSize) + 1) * wordSize;
100 inline size_t Bitmap<size>::count(size_t start) const
103 for ( ; (start % wordSize); ++start) {
107 for (size_t i = start / wordSize; i < words; ++i)
108 result += WTF::bitCount(bits[i]);
112 template<size_t size>
113 inline size_t Bitmap<size>::isEmpty() const
115 for (size_t i = 0; i < words; ++i)
121 template<size_t size>
122 inline size_t Bitmap<size>::isFull() const
124 for (size_t i = 0; i < words; ++i)