]>
git.saurik.com Git - apple/javascriptcore.git/blob - wtf/Bitmap.h
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 size_t nextPossiblyUnset(size_t) const;
43 int64_t findRunOfZeros(size_t) const;
44 size_t count(size_t = 0) const;
45 size_t isEmpty() const;
46 size_t isFull() const;
49 static const WordType wordSize
= sizeof(WordType
) * 8;
50 static const WordType words
= (size
+ wordSize
- 1) / wordSize
;
52 // the literal '1' is of type signed int. We want to use an unsigned
53 // version of the correct size when doing the calculations because if
54 // WordType is larger than int, '1 << 31' will first be sign extended
55 // and then casted to unsigned, meaning that set(31) when WordType is
56 // a 64 bit unsigned int would give 0xffff8000
57 static const WordType one
= 1;
59 FixedArray
<WordType
, words
> bits
;
63 inline Bitmap
<size
>::Bitmap()
69 inline bool Bitmap
<size
>::get(size_t n
) const
71 return !!(bits
[n
/ wordSize
] & (one
<< (n
% wordSize
)));
75 inline void Bitmap
<size
>::set(size_t n
)
77 bits
[n
/ wordSize
] |= (one
<< (n
% wordSize
));
81 inline bool Bitmap
<size
>::testAndSet(size_t n
)
83 WordType mask
= one
<< (n
% wordSize
);
84 size_t index
= n
/ wordSize
;
85 bool result
= bits
[index
] & mask
;
91 inline void Bitmap
<size
>::clear(size_t n
)
93 bits
[n
/ wordSize
] &= ~(one
<< (n
% wordSize
));
97 inline void Bitmap
<size
>::clearAll()
99 memset(bits
.data(), 0, sizeof(bits
));
102 template<size_t size
>
103 inline size_t Bitmap
<size
>::nextPossiblyUnset(size_t start
) const
105 if (!~bits
[start
/ wordSize
])
106 return ((start
/ wordSize
) + 1) * wordSize
;
110 template<size_t size
>
111 inline int64_t Bitmap
<size
>::findRunOfZeros(size_t runLength
) const
116 for (size_t i
= 0; i
<= (size
- runLength
) ; i
++) {
118 for (size_t j
= i
; j
<= (i
+ runLength
- 1) ; j
++) {
130 template<size_t size
>
131 inline size_t Bitmap
<size
>::count(size_t start
) const
134 for ( ; (start
% wordSize
); ++start
) {
138 for (size_t i
= start
/ wordSize
; i
< words
; ++i
)
139 result
+= WTF::bitCount(bits
[i
]);
143 template<size_t size
>
144 inline size_t Bitmap
<size
>::isEmpty() const
146 for (size_t i
= 0; i
< words
; ++i
)
152 template<size_t size
>
153 inline size_t Bitmap
<size
>::isFull() const
155 for (size_t i
= 0; i
< words
; ++i
)