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
21 #include <bitset>
22 #include <limits>
23
24 // This is ANGLE's BitSetIterator class with a customizable return type
25 // TODO(cwallez@chromium.org): it could be optimized, in particular when N <= 64
26
27 template <typename T>
roundUp(const T value,const T alignment)28 T roundUp(const T value, const T alignment) {
29 auto temp = value + alignment - static_cast<T>(1);
30 return temp - temp % alignment;
31 }
32
33 template <size_t N, typename T>
34 class BitSetIterator final {
35 public:
36 BitSetIterator(const std::bitset<N>& bitset);
37 BitSetIterator(const BitSetIterator& other);
38 BitSetIterator& operator=(const BitSetIterator& other);
39
40 class Iterator final {
41 public:
42 Iterator(const std::bitset<N>& bits);
43 Iterator& operator++();
44
45 bool operator==(const Iterator& other) const;
46 bool operator!=(const Iterator& other) const;
47 T operator*() const {
48 return static_cast<T>(mCurrentBit);
49 }
50
51 private:
52 unsigned long getNextBit();
53
54 static const size_t BitsPerWord = sizeof(uint32_t) * 8;
55 std::bitset<N> mBits;
56 unsigned long mCurrentBit;
57 unsigned long mOffset;
58 };
59
begin()60 Iterator begin() const {
61 return Iterator(mBits);
62 }
end()63 Iterator end() const {
64 return Iterator(std::bitset<N>(0));
65 }
66
67 private:
68 const std::bitset<N> mBits;
69 };
70
71 template <size_t N, typename T>
BitSetIterator(const std::bitset<N> & bitset)72 BitSetIterator<N, T>::BitSetIterator(const std::bitset<N>& bitset) : mBits(bitset) {
73 }
74
75 template <size_t N, typename T>
BitSetIterator(const BitSetIterator & other)76 BitSetIterator<N, T>::BitSetIterator(const BitSetIterator& other) : mBits(other.mBits) {
77 }
78
79 template <size_t N, typename T>
80 BitSetIterator<N, T>& BitSetIterator<N, T>::operator=(const BitSetIterator& other) {
81 mBits = other.mBits;
82 return *this;
83 }
84
85 template <size_t N, typename T>
Iterator(const std::bitset<N> & bits)86 BitSetIterator<N, T>::Iterator::Iterator(const std::bitset<N>& bits)
87 : mBits(bits), mCurrentBit(0), mOffset(0) {
88 if (bits.any()) {
89 mCurrentBit = getNextBit();
90 } else {
91 mOffset = static_cast<unsigned long>(roundUp(N, BitsPerWord));
92 }
93 }
94
95 template <size_t N, typename T>
96 typename BitSetIterator<N, T>::Iterator& BitSetIterator<N, T>::Iterator::operator++() {
97 DAWN_ASSERT(mBits.any());
98 mBits.set(mCurrentBit - mOffset, 0);
99 mCurrentBit = getNextBit();
100 return *this;
101 }
102
103 template <size_t N, typename T>
104 bool BitSetIterator<N, T>::Iterator::operator==(const Iterator& other) const {
105 return mOffset == other.mOffset && mBits == other.mBits;
106 }
107
108 template <size_t N, typename T>
109 bool BitSetIterator<N, T>::Iterator::operator!=(const Iterator& other) const {
110 return !(*this == other);
111 }
112
113 template <size_t N, typename T>
getNextBit()114 unsigned long BitSetIterator<N, T>::Iterator::getNextBit() {
115 static std::bitset<N> wordMask(std::numeric_limits<uint32_t>::max());
116
117 while (mOffset < N) {
118 uint32_t wordBits = static_cast<uint32_t>((mBits & wordMask).to_ulong());
119 if (wordBits != 0ul) {
120 return ScanForward(wordBits) + mOffset;
121 }
122
123 mBits >>= BitsPerWord;
124 mOffset += BitsPerWord;
125 }
126 return 0;
127 }
128
129 // Helper to avoid needing to specify the template parameter size
130 template <size_t N>
IterateBitSet(const std::bitset<N> & bitset)131 BitSetIterator<N, uint32_t> IterateBitSet(const std::bitset<N>& bitset) {
132 return BitSetIterator<N, uint32_t>(bitset);
133 }
134
135 #endif // COMMON_BITSETITERATOR_H_
136