CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
enumBitset.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://www.scicomp.uni-kl.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://www.scicomp.uni-kl.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <bitset>
38#include <type_traits>
39
40#include "../config.h"
41#include "enumInterface.hpp"
42#include "macros.hpp"
43
45namespace codi {
46
47 /* Original idea from http://stackoverflow.com/questions/17350214/using-enum-class-with-stdbitset */
48
63 template<typename T_Enum>
64 struct EnumBitset {
65 public:
66 using Enum = T_Enum;
67
68 private:
69 using UnderlyingEnumType = typename std::underlying_type<Enum>::type;
70 using Bitset = std::bitset<static_cast<UnderlyingEnumType>(Enum::MaxElement)>;
71
72 static UnderlyingEnumType constexpr ALL_VALUE = (1 << static_cast<UnderlyingEnumType>(Enum::MaxElement)) - 1;
73
74 Bitset bitset;
75
76 CODI_INLINE UnderlyingEnumType get_value(Enum v) const {
77 return static_cast<UnderlyingEnumType>(v);
78 }
79
80 CODI_INLINE EnumBitset(UnderlyingEnumType value) : bitset(value) {}
81
82 public:
83
86
88 CODI_INLINE EnumBitset(Enum pos) : bitset() {
89 set(pos);
90 }
91
93 CODI_INLINE bool test(Enum pos) const {
94 return bitset.test(get_value(pos));
95 }
96
99 bitset.reset(get_value(pos));
100 return *this;
101 }
102
105 bitset.flip(get_value(pos));
106 return *this;
107 }
108
111 bitset.flip();
112 bitset &= Bitset(ALL_VALUE);
113 return *this;
114 }
115
118 bitset.set(get_value(pos));
119 return *this;
120 }
121
124 bitset.reset();
125 return *this;
126 }
127
130 bitset |= o.bitset;
131 return *this;
132 }
133
136 return set(pos);
137 }
138
141 bitset &= o.bitset;
142 return *this;
143 }
144
147 return *this &= EnumBitset(pos);
148 }
149
151 CODI_INLINE bool operator!=(EnumBitset const& o) const {
152 return bitset != o.bitset;
153 }
154
156 CODI_INLINE bool operator!=(Enum const& pos) const {
157 return bitset != EnumBitset(pos).bitset;
158 }
159
161 CODI_INLINE bool operator==(EnumBitset const& o) const {
162 return bitset == o.bitset;
163 }
164
166 CODI_INLINE bool operator==(Enum const& pos) const {
167 return bitset == EnumBitset(pos).bitset;
168 }
169
172 return this->bitset.any();
173 }
174
176 CODI_INLINE Bitset getData() const {
177 return bitset;
178 }
179
181 CODI_INLINE static constexpr EnumBitset ALL() {
182 return EnumBitset(ALL_VALUE);
183 }
184
186 CODI_INLINE static constexpr EnumBitset NONE() {
187 return EnumBitset();
188 }
189 };
190
192 template<typename Enum>
194 EnumBitset<Enum> r = a;
195 r |= b;
196
197 return r;
198 }
199
201 template<typename Enum>
203 EnumBitset<Enum> r = a;
204 r |= b;
205
206 return r;
207 }
208
210 template<typename Enum>
212 return b | a;
213 }
214
216 template<typename Enum>
218 EnumBitset<Enum> r = a;
219 r &= b;
220
221 return r;
222 }
223
225 template<typename Enum>
227 EnumBitset<Enum> r = a;
228 r &= b;
229
230 return r;
231 }
232
234 template<typename Enum>
236 return b & a;
237 }
238
240 template<typename Enum>
241 CODI_INLINE bool operator!=(Enum a, EnumBitset<Enum> const& b) {
242 return b != a;
243 }
244
246 template<typename Enum>
247 CODI_INLINE bool operator==(Enum a, EnumBitset<Enum> const& b) {
248 return b == a;
249 }
250
252 template<typename Out, typename Enum>
253 CODI_INLINE Out& operator<<(Out& out, EnumBitset<Enum> const& b) {
254 return out << b.getData();
255 }
256}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition: config.h:457
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
EnumBitset< Enum > operator|(EnumBitset< Enum > const &a, EnumBitset< Enum > const &b)
Or operation of two bitsets.
Definition: enumBitset.hpp:193
bool operator==(Enum a, EnumBitset< Enum > const &b)
Equal comparison for an enum and a bitset.
Definition: enumBitset.hpp:247
ExpressionTraits::EnableIfExpression< Expr, std::ostream > & operator<<(std::ostream &out, Expr const &v)
Write the primal value to the stream.
Definition: expressionInterface.hpp:129
bool operator!=(Enum a, EnumBitset< Enum > const &b)
Not equal comparison for an enum and a bitset.
Definition: enumBitset.hpp:241
EnumBitset< Enum > operator&(EnumBitset< Enum > const &a, EnumBitset< Enum > const &b)
And operation of two bitsets.
Definition: enumBitset.hpp:217
A bitset with enum items as flags.
Definition: enumBitset.hpp:64
EnumBitset & operator&=(EnumBitset const &o)
And operation of two bitsets.
Definition: enumBitset.hpp:140
EnumBitset & set(Enum pos)
Set the bit for the enum to true.
Definition: enumBitset.hpp:117
EnumBitset & operator|=(Enum const &pos)
Or operation of the bitset and an enum.
Definition: enumBitset.hpp:135
EnumBitset & reset(Enum pos)
Reset the bit for the enum to false.
Definition: enumBitset.hpp:98
static constexpr EnumBitset NONE()
Constructor for a bitset with all values flagged as false.
Definition: enumBitset.hpp:186
EnumBitset()=default
Constructor all false.
static constexpr EnumBitset ALL()
Constructor for a bitset with all values flagged as true.
Definition: enumBitset.hpp:181
EnumBitset & flip()
Flip all bits.
Definition: enumBitset.hpp:110
bool operator!=(EnumBitset const &o) const
Not equal operation for two bitsets.
Definition: enumBitset.hpp:151
bool test(Enum pos) const
Test if the bit for the enum is set.
Definition: enumBitset.hpp:93
T_Enum Enum
See EnumBitset.
Definition: enumBitset.hpp:66
EnumBitset & reset()
Reset all bits to false.
Definition: enumBitset.hpp:123
bool operator==(Enum const &pos) const
Equal operation for a bitset and an enum.
Definition: enumBitset.hpp:166
EnumBitset(Enum pos)
Constructor which sets one bit to true.
Definition: enumBitset.hpp:88
EnumBitset & operator|=(EnumBitset const &o)
Or operation of two bitsets.
Definition: enumBitset.hpp:129
bool operator==(EnumBitset const &o) const
Equal operation for two bitsets.
Definition: enumBitset.hpp:161
Bitset getData() const
Get the underlying bitset.
Definition: enumBitset.hpp:176
EnumBitset & flip(Enum pos)
Flip the bit for the enum.
Definition: enumBitset.hpp:104
EnumBitset & operator&=(Enum const &pos)
And operation of the bitsets and and enum.
Definition: enumBitset.hpp:146
bool any()
Conversion to boolean.
Definition: enumBitset.hpp:171
bool operator!=(Enum const &pos) const
Not equal operation for a bitset and an enum.
Definition: enumBitset.hpp:156