1 // Copyright 2017 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef COMMON_BITSETITERATOR_H_
16 #define COMMON_BITSETITERATOR_H_
17
18 #include "common/Assert.h"
19 #include "common/Math.h"
20 #include "common/UnderlyingType.h"
21
22 #include <bitset>
23 #include <limits>
24
25 // This is ANGLE's BitSetIterator class with a customizable return type
26 // TODO(crbug.com/dawn/306): it could be optimized, in particular when N <= 64
27
28 template <typename T>
roundUp(const T value,const T alignment)29 T roundUp(const T value, const T alignment) {
30 auto temp = value + alignment - static_cast<T>(1);
31 return temp - temp % alignment;
32 }
33
34 template <size_t N, typename T>
35 class BitSetIterator final {
36 public:
37 BitSetIterator(const std::bitset<N>& bitset);
38 BitSetIterator(const BitSetIterator& other);
39 BitSetIterator& operator=(const BitSetIterator& other);
40
41 class Iterator final {
42 public:
43 Iterator(const std::bitset<N>& bits);
44 Iterator& operator++();
45
46 bool operator==(const Iterator& other) const;
47 bool operator!=(const Iterator& other) const;
48
49 T operator*() const {
50 using U = UnderlyingType<T>;
51 ASSERT(static_cast<U>(mCurrentBit) <= std::numeric_limits<U>::max());
52 return static_cast<T>(static_cast<U>(mCurrentBit));
53 }
54
55 private:
56 unsigned long getNextBit();
57
58 static constexpr size_t kBitsPerWord = sizeof(uint32_t) * 8;
59 std::bitset<N> mBits;
60 unsigned long mCurrentBit;
61 unsigned long mOffset;
62 };
63
begin()64 Iterator begin() const {
65 return Iterator(mBits);
66 }
end()67 Iterator end() const {
68 return Iterator(std::bitset<N>(0));
69 }
70
71 private:
72 const std::bitset<N> mBits;
73 };
74
75 template <size_t N, typename T>
BitSetIterator(const std::bitset<N> & bitset)76 BitSetIterator<N, T>::BitSetIterator(const std::bitset<N>& bitset) : mBits(bitset) {
77 }
78
79 template <size_t N, typename T>
BitSetIterator(const BitSetIterator & other)80 BitSetIterator<N, T>::BitSetIterator(const BitSetIterator& other) : mBits(other.mBits) {
81 }
82
83 template <size_t N, typename T>
84 BitSetIterator<N, T>& BitSetIterator<N, T>::operator=(const BitSetIterator& other) {
85 mBits = other.mBits;
86 return *this;
87 }
88
89 template <size_t N, typename T>
Iterator(const std::bitset<N> & bits)90 BitSetIterator<N, T>::Iterator::Iterator(const std::bitset<N>& bits)
91 : mBits(bits), mCurrentBit(0), mOffset(0) {
92 if (bits.any()) {
93 mCurrentBit = getNextBit();
94 } else {
95 mOffset = static_cast<unsigned long>(roundUp(N, kBitsPerWord));
96 }
97 }
98
99 template <size_t N, typename T>
100 typename BitSetIterator<N, T>::Iterator& BitSetIterator<N, T>::Iterator::operator++() {
101 DAWN_ASSERT(mBits.any());
102 mBits.set(mCurrentBit - mOffset, 0);
103 mCurrentBit = getNextBit();
104 return *this;
105 }
106
107 template <size_t N, typename T>
108 bool BitSetIterator<N, T>::Iterator::operator==(const Iterator& other) const {
109 return mOffset == other.mOffset && mBits == other.mBits;
110 }
111
112 template <size_t N, typename T>
113 bool BitSetIterator<N, T>::Iterator::operator!=(const Iterator& other) const {
114 return !(*this == other);
115 }
116
117 template <size_t N, typename T>
getNextBit()118 unsigned long BitSetIterator<N, T>::Iterator::getNextBit() {
119 static std::bitset<N> wordMask(std::numeric_limits<uint32_t>::max());
120
121 while (mOffset < N) {
122 uint32_t wordBits = static_cast<uint32_t>((mBits & wordMask).to_ulong());
123 if (wordBits != 0ul) {
124 return ScanForward(wordBits) + mOffset;
125 }
126
127 mBits >>= kBitsPerWord;
128 mOffset += kBitsPerWord;
129 }
130 return 0;
131 }
132
133 // Helper to avoid needing to specify the template parameter size
134 template <size_t N>
IterateBitSet(const std::bitset<N> & bitset)135 BitSetIterator<N, uint32_t> IterateBitSet(const std::bitset<N>& bitset) {
136 return BitSetIterator<N, uint32_t>(bitset);
137 }
138
139 #endif // COMMON_BITSETITERATOR_H_
140