-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBitBool.h
183 lines (149 loc) · 6.91 KB
/
BitBool.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
/*
The MIT License (MIT)
Copyright (c) 2012-2016 Christopher Andrews
http://arduino.land/Code/BitBool/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef HEADER_BITBOOL
#define HEADER_BITBOOL
#include <stdint.h> //uintX_t types
#include <stddef.h> //size_t
//Algorithm reversal options.
enum REVERSE_OPTIONS{
REVERSE_NONE, //This will read data as big endian.
REVERSE_BITS, //Bit index zero references MSB, instead of LSB.
REVERSE_BYTES, //Byte index zero references sizeof(T) - 1.
REVERSE_BOTH, //This will read data as little endian.
REVERSE_DEFAULT = REVERSE_NONE
};
#define REVERSE_BITS_MASK 0x01
#define REVERSE_BYTES_MASK 0x02
/*
BitRef Class.
This is a proxy class which allows modifying and referencing single bits.
*/
template< uint8_t reverse = REVERSE_DEFAULT >
struct BitRef{
// Construct a BitRef using dynamically calculated offsets (original)
BitRef( uint8_t &dataRef, const uint8_t idx ) :
data( dataRef ),
index( (reverse & REVERSE_BITS_MASK) ? ( 0x80 >> idx ) : ( 0x1 << idx ) )
{ return; }
// Construct a BitRef using a lookup table.
BitRef( uint8_t &dataRef, const uint8_t idx, bool lookupTable ) :
data( dataRef ),
index( shift[idx] )
{ return; }
//Implicit conversion to bit value.
operator bool() const { return data & index; }
//Assignment of another BitRef, or a boolean
bool operator =( const BitRef © ) const { return *this = ( const bool ) copy; }
bool operator =( const bool © ) const {
if( copy ) data |= index;
else data &= ~index;
return copy;
}
//Unconditionally invert the bit.
void invert() const{ data ^= index; }
uint8_t &data; //Reference to byte being accessed.
uint8_t const index; //Index of bit inside 'data' to access.
static const uint8_t shift[8]; //The lookup table if used.
};
/*
BitBool Class.
count: number of bits required in array.
reverse: See REVERSE_OPTIONS, default is no reverse.
lookUp: If true, a lookup table is utilized.
*/
template< size_t count, uint8_t reverse = REVERSE_DEFAULT, bool lookUp = false>
struct BitBool{
//Data accessors.
enum{ bitCount = count, byteCount = ( bitCount / 0x8 ) + ( ( bitCount % 0x8 ) ? 0x1 : 0x0 ) };
//Ranged loop iteration structure.
struct bIterator{
bIterator( BitBool &o, uint16_t i ) : owner(o), idx(i) {}
bool operator !=( const bIterator &b ){ return b.idx != idx; }
bIterator &operator ++(){ return ++idx, *this; }
BitRef<reverse> operator *(){ return owner[idx]; }
BitBool &owner;
uint16_t idx;
};
//Proxy for arbitrary ranges
struct anyIterator{
anyIterator( BitBool &o, uint16_t s, uint16_t f ) : owner(o), start(s), finish(f) {}
bIterator begin(){ return bIterator( owner, start ); }
bIterator end(){ return bIterator( owner, finish ); }
BitBool &owner;
uint16_t start, finish;
};
//Basic iterator functionality.
bIterator begin(){ return bIterator( *this, 0 ); }
bIterator end(){ return bIterator( *this, count ); }
//Arbitrary range iterator functionality.
anyIterator iterate(){ return anyIterator(*this, 0, count); } //Duplicate of basic iteration.
anyIterator iterate( uint16_t start ){ return anyIterator(*this, start, count); }
anyIterator iterate( uint16_t start, uint16_t length ){ return anyIterator(*this, start, start + length); }
//Read / write access using subscript.
BitRef<reverse> operator []( const uint16_t index ){
const uint16_t offset = ( reverse & REVERSE_BYTES_MASK ) ? (byteCount - 1) - (index >> 0x3): (index >> 0x3);
if(lookUp) return BitRef<reverse>( data[ offset ], index & 0x7, true );
else return BitRef<reverse>( data[ offset ], index & 0x7 );
}
//Read / write access using get and set method.
bool get( uint16_t index ){ return (*this)[index]; }
void set( uint16_t index, bool value ){ (*this)[index] = value; }
void invert( uint16_t index){ return (*this)[index].invert(); }
//Invert the entire BitBool.
void invertAll(){ for( uint16_t idx = 0 ; idx < byteCount ; ++idx ){ data[ idx ] = ~data[ idx ]; } }
//Public data object (Free for users to manipulate).
uint8_t data[ byteCount ];
};
#define TBITS (sizeof(T)*8)
//Reference any object as a BitBool
template<uint8_t reverse, bool lookUp, typename T>
inline BitBool<TBITS, reverse, lookUp> &toBitBool( T &t ){
union{
T *_t;
BitBool<TBITS, reverse, lookUp> *_u;
} _t = {&t};
return *_t._u;
}
template<uint8_t reverse, typename T>
inline BitBool<TBITS, reverse, false> &toBitBool( T &t ){
return toBitBool<reverse, false, T>(t);
}
template<typename T>
inline BitBool<TBITS, false, false> &toBitBool( T &t ){
return toBitBool<REVERSE_DEFAULT, false, T>(t);
}
//Reference a single bit inside an object.
template<uint8_t reverse, bool lookUp, typename T>
inline BitRef<reverse> toBitRef( T &t, uint16_t bit ){
return toBitBool<reverse, lookUp>(t)[ bit ];
}
template<uint8_t reverse, typename T>
inline BitRef<reverse> toBitRef( T &t, uint16_t bit ){
return toBitRef<reverse, false>(t, bit);
}
template<typename T>
inline BitRef<REVERSE_DEFAULT> toBitRef( T &t, uint16_t bit ){
return toBitRef<REVERSE_DEFAULT, false>(t, bit);
}
#undef TBITS
#undef REVERSE_BITS_MASK
#undef REVERSE_BYTES_MASK
#endif