• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_ITYP_BITSET_H_
16 #define COMMON_ITYP_BITSET_H_
17 
18 #include "common/BitSetIterator.h"
19 #include "common/TypedInteger.h"
20 #include "common/UnderlyingType.h"
21 
22 namespace ityp {
23 
24     // ityp::bitset is a helper class that wraps std::bitset with the restriction that
25     // indices must be a particular type |Index|.
26     template <typename Index, size_t N>
27     class bitset : private std::bitset<N> {
28         using I = UnderlyingType<Index>;
29         using Base = std::bitset<N>;
30 
31         static_assert(sizeof(I) <= sizeof(size_t), "");
32 
bitset(const Base & rhs)33         constexpr bitset(const Base& rhs) : Base(rhs) {
34         }
35 
36       public:
bitset()37         constexpr bitset() noexcept : Base() {
38         }
39 
bitset(unsigned long long value)40         constexpr bitset(unsigned long long value) noexcept : Base(value) {
41         }
42 
43         constexpr bool operator[](Index i) const {
44             return Base::operator[](static_cast<I>(i));
45         }
46 
47         typename Base::reference operator[](Index i) {
48             return Base::operator[](static_cast<I>(i));
49         }
50 
test(Index i)51         bool test(Index i) const {
52             return Base::test(static_cast<I>(i));
53         }
54 
55         using Base::all;
56         using Base::any;
57         using Base::count;
58         using Base::none;
59         using Base::size;
60 
61         bool operator==(const bitset& other) const noexcept {
62             return Base::operator==(static_cast<const Base&>(other));
63         }
64 
65         bool operator!=(const bitset& other) const noexcept {
66             return Base::operator!=(static_cast<const Base&>(other));
67         }
68 
69         bitset& operator&=(const bitset& other) noexcept {
70             return static_cast<bitset&>(Base::operator&=(static_cast<const Base&>(other)));
71         }
72 
73         bitset& operator|=(const bitset& other) noexcept {
74             return static_cast<bitset&>(Base::operator|=(static_cast<const Base&>(other)));
75         }
76 
77         bitset& operator^=(const bitset& other) noexcept {
78             return static_cast<bitset&>(Base::operator^=(static_cast<const Base&>(other)));
79         }
80 
81         bitset operator~() const noexcept {
82             return bitset(*this).flip();
83         }
84 
set()85         bitset& set() noexcept {
86             return static_cast<bitset&>(Base::set());
87         }
88 
89         bitset& set(Index i, bool value = true) {
90             return static_cast<bitset&>(Base::set(static_cast<I>(i), value));
91         }
92 
reset()93         bitset& reset() noexcept {
94             return static_cast<bitset&>(Base::reset());
95         }
96 
reset(Index i)97         bitset& reset(Index i) {
98             return static_cast<bitset&>(Base::reset(static_cast<I>(i)));
99         }
100 
flip()101         bitset& flip() noexcept {
102             return static_cast<bitset&>(Base::flip());
103         }
104 
flip(Index i)105         bitset& flip(Index i) {
106             return static_cast<bitset&>(Base::flip(static_cast<I>(i)));
107         }
108 
109         using Base::to_string;
110         using Base::to_ullong;
111         using Base::to_ulong;
112 
113         friend bitset operator&(const bitset& lhs, const bitset& rhs) noexcept {
114             return bitset(static_cast<const Base&>(lhs) & static_cast<const Base&>(rhs));
115         }
116 
117         friend bitset operator|(const bitset& lhs, const bitset& rhs) noexcept {
118             return bitset(static_cast<const Base&>(lhs) | static_cast<const Base&>(rhs));
119         }
120 
121         friend bitset operator^(const bitset& lhs, const bitset& rhs) noexcept {
122             return bitset(static_cast<const Base&>(lhs) ^ static_cast<const Base&>(rhs));
123         }
124 
IterateBitSet(const bitset & bitset)125         friend BitSetIterator<N, Index> IterateBitSet(const bitset& bitset) {
126             return BitSetIterator<N, Index>(static_cast<const Base&>(bitset));
127         }
128 
129         friend struct std::hash<bitset>;
130     };
131 
132 }  // namespace ityp
133 
134 #endif  // COMMON_ITYP_BITSET_H_
135