-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitSet.h
208 lines (182 loc) · 6.48 KB
/
BitSet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#pragma once
#include <iostream>
#include <cassert>
#define CE constexpr
// битовое поле -> bitfield = { mask }
// битовый набор -> bitset = { mask, val }
// ссылка на битовое поле -> bitfieldref? = { mask, val& }
// Загрузка строковой константы из потока.
// При успехе строковая константа из потока извлекается.
/*inline std::istream& operator>>( std::istream& is, const char *str )
{
assert( str);
for( unsigned int i = 0; *str; ++i)
{
int c = is.get();
if( c != *str++)
{
if( c <= 0)
is.clear();
else
is.putback( c);
--str;
for( ; i > 0; --i)
is.putback( *--str);
// При неудаче устанавливам failbit у потока, в остальном его состояние не меняется.
is.setstate( std::ios_base::failbit );
return is;
}
}
return is;
}
*/
static bool get_from( std::istream& input, const char *token )
{
assert( token);
const char *p = token;
while( *p )
{
if( input.peek() != *p++ )
{
--p;
for( unsigned int i = p - token; i > 0; --i )
input.unget();
return false;
}
input.ignore();
}
return true;
}
CE static bool matches( const char *s1, const char *s2, unsigned int i = 0)
{
return s1[i] ? s1[i] == s2[i] && matches( s1, s2, i + 1)
: !s2[i];
}
template< typename Val>
struct Field
{
Val val;
Val mask;
const char *name;
CE Field( const char *name_, const Val& val_ ): val( val_), mask( val_ ), name( name_) {}
CE Field( const char *name_, const Val& val_, const Val& mask_ ): val( val_), mask( mask_), name( name_) {}
};
typedef Field< uint8_t> Field8;
typedef Field< uint16_t> Field16;
typedef Field< uint32_t> Field32;
typedef Field< uint64_t> Field64;
template< typename Val >
struct BitSet0
{
Val val;
struct Bit
{
Val val;
Val mask;
CE Bit( const Val &val_, const Val &mask_ ): val( val_ ), mask( mask_ ) {}
CE Bit( const Bit &r ): val( r.val ), mask( r.mask ) {}
CE Bit( const Field< Val> &r ): val( r.val ), mask( r.mask ) {}
CE Bit operator~( ) const { return Bit(~val , mask ); }
CE Bit operator|( const Bit &r) const { return Bit( val | r.val, mask | r.mask); }
CE Bit operator&( const Bit &r) const { return Bit( val & r.val, mask & r.mask); }
};
CE BitSet0( ): val( ) {}
CE BitSet0( const Val &r): val( r ) {}
CE BitSet0( const Bit &r): val( r.val & r.mask ) {}
void print( std::ostream &os, const Field< Val> *map, unsigned int maplen ) const
{
for( ; maplen > 0; )
{
--maplen;
if( map->val == (val & map->mask) )
{
//os << ' ' << map->name;
os << map->name;
return;
}
map++;
}
}
void read( std::istream &is, const Field< Val> *map, unsigned int maplen )
{
if( !is)
return;
const Field< Val> *const map0 = map;
const unsigned int maplen0 = maplen;
for( ; maplen > 0; )
{
--maplen;
if( get_from( is, map->name) )
{
val &= ~map->mask;
val |= map->val ;
return;
}
//is.clear();
map++;
}
std::cerr << "Wrong read! Wait words: ";
const char* t = "\"";
map = map0;
for( maplen = maplen0; maplen > 0; )
{
--maplen;
std::cerr << t << map->name << '"';
t = " or \"";
map++;
}
is.setstate( std::ios_base::failbit );
}
};
template< typename Val, const Field< Val> map[], const unsigned int maplen>
class BitSet: BitSet0< Val>
{
typedef BitSet0< Val> Base;
CE static Field< Val> recurfind( const char *s, unsigned int i )
{
return matches( s, map[ i].name) ? map[ i]
: recurfind( s, i - 1);
//: i > 0 ? recurfind( s, i - 1)
// : Field();
}
CE static Field< Val> value( const char *bitname )
{
return recurfind( bitname, maplen - 1);
}
CE BitSet( const Val &r): Base( r ) {}
public:
struct Bit: Base::Bit
{
CE Bit( const char *name ): Base::Bit( value( name) ) {}
CE Bit( const typename Base::Bit &r ): Base::Bit( r ) {}
};
struct Mask
{
Val mask;
CE Mask( const Bit &r ): mask( r.mask ) {}
CE Mask( const Field< Val> &r ): mask( r.mask ) {}
CE Mask( const char *name ): mask( value( name).mask ) {}
};
CE BitSet getfield( const Mask &mask ) const
{
return BitSet( Base::val & mask.mask );
}
CE bool getvalue( const Bit &bit ) const
{
return (Base::val & bit.mask) == bit.val;
}
CE BitSet( ): Base( ) {}
CE BitSet( const typename Base::Bit &r ): Base( r ) {}
// BitSet &operator = ( const Bit &r ) { (Base::val &= ~r.mask) |= r.val; return *this; }
BitSet &operator |=( const BitSet &r) { Base::val |= r.val; return *this; }
BitSet &operator &=( const BitSet &r) { Base::val &= r.val; return *this; }
CE BitSet operator ~ ( ) const { return BitSet(~Base::val );}
CE BitSet operator | ( const BitSet &r) const { return BitSet( Base::val | r.val);}
CE BitSet operator & ( const BitSet &r) const { return BitSet( Base::val & r.val);}
//CE operator bool() const { return val; } // если раскоменьтит эту строку -> у BitSet появится operator[] !!!
void print( std::ostream &os) const { Base::print( os, map, maplen);}
void read( std::istream &is) { Base::read( is, map, maplen);}
friend std::ostream& operator<<( std::ostream &os, const BitSet &obj ) { obj.print( os); return os;}
friend std::istream& operator>>( std::istream &is, BitSet &obj ) { obj.read( is); return is; }
};
#define BITSET(x) BitSet< decltype((x)->val), (x), (sizeof(x)/sizeof(*(x))) >