Brick Library 0.1
Performance-portable stencil datalayout & codegen
Loading...
Searching...
No Matches
bitset.h
Go to the documentation of this file.
1
6#ifndef BRICK_BITSET_H
7#define BRICK_BITSET_H
8
9#include <initializer_list>
10#include <ostream>
11
18typedef struct BitSet {
19 static const long zero = 31;
20
26 static inline uint64_t to_set(long pos) {
27 if (pos < 0)
28 pos = zero - pos;
29 return 1ul << (uint64_t) pos;
30 }
31
33 uint64_t set;
34
36 BitSet() : set(0) {}
37
39 BitSet(uint64_t s) : set(s) {
40 }
41
51 BitSet(std::initializer_list<int> l) {
52 set = 0;
53 for (auto p: l)
54 set ^= to_set(p);
55 }
56
64 inline BitSet &flip(long pos) {
65 set ^= to_set(pos);
66 return *this;
67 }
68
70 inline long size() const {
71 return __builtin_popcountl(set);
72 }
73
75 inline bool get(long pos) const {
76 return (set & to_set(pos)) > 0;
77 }
78
80 inline BitSet operator&(BitSet a) const {
81 return set & a.set;
82 }
83
85 inline BitSet operator|(BitSet a) const {
86 return set | a.set;
87 }
88
90 inline BitSet operator^(BitSet a) const {
91 return set ^ a.set;
92 }
93
95 inline operator bool() const {
96 return set != 0ul;
97 }
98
100 inline bool operator<=(BitSet a) const {
101 return (set & a.set) == set;
102 }
103
105 inline bool operator>=(BitSet a) const {
106 return (set & a.set) == a.set;
107 }
108
119 inline BitSet operator!() const {
120 BitSet ret(0);
121
122 uint64_t mask = (1ul << (uint64_t) (zero + 1)) - 1ul;
123
124 ret.set = ((set & mask) << (uint64_t) zero) | (set >> (uint64_t) zero);
125 return ret;
126 }
128
139inline std::ostream &operator<<(std::ostream &os, const BitSet &bs) {
140 os << "{";
141 for (long i = 1; i < 32; ++i) {
142 if (bs.get(i)) os << i << "+";
143 if (bs.get(-i)) os << i << "-";
144 }
145 os << "}";
146 return os;
147}
148
155inline std::istream &operator>>(std::istream &is, BitSet &bs) {
156 std::string str;
157 is >> str;
158 if (str[0] != '{')
159 throw std::runtime_error("Err");
160 BitSet set;
161 long pos = 1;
162 while (str[pos] != '}') {
163 long d = 0;
164 while (isdigit(str[pos])) {
165 d = d * 10 + str[pos] - '0';
166 ++pos;
167 }
168 if (str[pos] == '-')
169 d = -d;
170 else if (str[pos] != '+')
171 throw std::runtime_error("Err");
172 ++pos;
173 set.flip(d);
174 }
175 bs = set;
176 return is;
177}
178
179#endif //BRICK_BITSET_H
Set using bitfield.
Definition: bitset.h:18
std::istream & operator>>(std::istream &is, BitSet &bs)
Read a bit set.
Definition: bitset.h:155
bool operator>=(BitSet a) const
True if .
Definition: bitset.h:105
BitSet(std::initializer_list< int > l)
Initialize a set based on a list of numbers.
Definition: bitset.h:51
uint64_t set
The bitfield of this set.
Definition: bitset.h:33
BitSet operator^(BitSet a) const
Definition: bitset.h:90
static uint64_t to_set(long pos)
Turn number into corresponding element of set.
Definition: bitset.h:26
BitSet operator|(BitSet a) const
Union with another set.
Definition: bitset.h:85
BitSet & flip(long pos)
Flipping an element.
Definition: bitset.h:64
static const long zero
negative zero start at 31 (1<<31).
Definition: bitset.h:19
BitSet operator&(BitSet a) const
Intersection with another set.
Definition: bitset.h:80
bool get(long pos) const
Return whether a number is in the set.
Definition: bitset.h:75
BitSet()
Default to empty set.
Definition: bitset.h:36
bool operator<=(BitSet a) const
True if .
Definition: bitset.h:100
long size() const
The number of elements currently stored in the set.
Definition: bitset.h:70
std::ostream & operator<<(std::ostream &os, const BitSet &bs)
Print a bit set.
Definition: bitset.h:139
BitSet operator!() const
Negate all elements in the set, not a set operation.
Definition: bitset.h:119
BitSet(uint64_t s)
Initialize a set based on an unsigned bitfield.
Definition: bitset.h:39