diff options
author | Ben Murdoch <benm@google.com> | 2010-08-11 14:44:44 +0100 |
---|---|---|
committer | Ben Murdoch <benm@google.com> | 2010-08-12 19:15:41 +0100 |
commit | dd8bb3de4f353a81954234999f1fea748aee2ea9 (patch) | |
tree | 729b52bf09294f0d6c67cd5ea80aee1b727b7bd8 /JavaScriptCore/wtf/Bitmap.h | |
parent | f3d41ba51d86bf719c7a65ab5297aea3c17e2d98 (diff) | |
download | external_webkit-dd8bb3de4f353a81954234999f1fea748aee2ea9.zip external_webkit-dd8bb3de4f353a81954234999f1fea748aee2ea9.tar.gz external_webkit-dd8bb3de4f353a81954234999f1fea748aee2ea9.tar.bz2 |
Merge WebKit at r65072 : Initial merge by git.
Change-Id: Ibcf418498376b2660aacb7f8d46ea7085ef91585
Diffstat (limited to 'JavaScriptCore/wtf/Bitmap.h')
-rw-r--r-- | JavaScriptCore/wtf/Bitmap.h | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/JavaScriptCore/wtf/Bitmap.h b/JavaScriptCore/wtf/Bitmap.h new file mode 100644 index 0000000..4dd88f6 --- /dev/null +++ b/JavaScriptCore/wtf/Bitmap.h @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2010 Apple Inc. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ +#ifndef Bitmap_h +#define Bitmap_h + +#include "FixedArray.h" +#include "StdLibExtras.h" + +#include <stdint.h> + +namespace WTF { + +template<size_t size> +class Bitmap { +private: + typedef uint32_t WordType; + +public: + Bitmap(); + + bool get(size_t) const; + void set(size_t); + void clear(size_t); + void clearAll(); + void advanceToNextFreeBit(size_t&) const; + size_t count(size_t = 0) const; + size_t isEmpty() const; + size_t isFull() const; + +private: + static const WordType wordSize = sizeof(WordType) * 8; + static const WordType words = (size + wordSize - 1) / wordSize; + + // the literal '1' is of type signed int. We want to use an unsigned + // version of the correct size when doing the calculations because if + // WordType is larger than int, '1 << 31' will first be sign extended + // and then casted to unsigned, meaning that set(31) when WordType is + // a 64 bit unsigned int would give 0xffff8000 + static const WordType one = 1; + + FixedArray<WordType, words> bits; +}; + +template<size_t size> +inline Bitmap<size>::Bitmap() +{ + clearAll(); +} + +template<size_t size> +inline bool Bitmap<size>::get(size_t n) const +{ + return !!(bits[n / wordSize] & (one << (n % wordSize))); +} + +template<size_t size> +inline void Bitmap<size>::set(size_t n) +{ + bits[n / wordSize] |= (one << (n % wordSize)); +} + +template<size_t size> +inline void Bitmap<size>::clear(size_t n) +{ + bits[n / wordSize] &= ~(one << (n % wordSize)); +} + +template<size_t size> +inline void Bitmap<size>::clearAll() +{ + memset(bits.data(), 0, sizeof(bits)); +} + +template<size_t size> +inline void Bitmap<size>::advanceToNextFreeBit(size_t& start) const +{ + if (!~bits[start / wordSize]) + start = ((start / wordSize) + 1) * wordSize; + else + ++start; +} + +template<size_t size> +inline size_t Bitmap<size>::count(size_t start) const +{ + size_t result = 0; + for ( ; (start % wordSize); ++start) { + if (get(start)) + ++result; + } + for (size_t i = start / wordSize; i < words; ++i) + result += WTF::bitCount(bits[i]); + return result; +} + +template<size_t size> +inline size_t Bitmap<size>::isEmpty() const +{ + for (size_t i = 0; i < words; ++i) + if (bits[i]) + return false; + return true; +} + +template<size_t size> +inline size_t Bitmap<size>::isFull() const +{ + for (size_t i = 0; i < words; ++i) + if (~bits[i]) + return false; + return true; +} + +} +#endif |