forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitArray.cpp
More file actions
218 lines (196 loc) · 4.78 KB
/
Copy pathBitArray.cpp
File metadata and controls
218 lines (196 loc) · 4.78 KB
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
209
210
211
212
213
214
215
216
217
218
//
// FILE: BitArray.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// PURPOSE: BitArray library for Arduino
// URL: https://github.com/RobTillaart/BitArray
// http://forum.arduino.cc/index.php?topic=361167
//
// 16 bit clear is faster --> verify correctness
// 0.2.2 2020-12-14 add arduino-CI + unit test
// 0.2.1 2020-06-05 fix library.json
// 0.2.0 2020-03-28 #pragma once, readme, fix fibnacci demo
//
// 0.1.9 - fix constructor bug
// 0.1.8 - added toggle
// 0.1.07 - private calls inline -> performance & footprint
// 0.1.06 - refactored
// 0.1.05 - added upper limits
// 0.1.04 - improve performance
// 0.1.03 - refactoring
// 0.1.02 - first stabile version (at last)
// 0.1.01 - added clear() + fixed set bug
// 0.1.00 - initial version
//
#include "BitArray.h"
BitArray::BitArray()
{
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
_ar[i] = NULL;
}
}
BitArray::~BitArray()
{
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
if (_ar[i]) free(_ar[i]);
}
}
uint8_t BitArray::begin(const uint8_t bits, const uint16_t size)
{
if (bits == 0 || bits > 32)
{
_error = BA_ELEMENT_SIZE_ERR;
return _error;
}
if ((1UL * bits * size)/8 > (1UL * BA_MAX_SEGMENTS * BA_SEGMENT_SIZE))
{
_error = BA_SIZE_ERR;
return _error;
}
for (uint8_t i = 0; i < BA_MAX_SEGMENTS; i++)
{
if (_ar[i]) free(_ar[i]);
}
_segments = 0;
_bits = bits;
_bytes = (_bits * size + 7) / 8;
uint16_t b = _bytes;
while (b > 0)
{
_ar[_segments] = (uint8_t*) malloc(min(b, BA_SEGMENT_SIZE));
if (_ar[_segments] == NULL)
{
_error = BA_NO_MEMORY_ERR;
return _error;
}
b = b - min(b, BA_SEGMENT_SIZE);
_segments++;
}
_error = BA_OK;
return _error;
}
uint32_t BitArray::get(const uint16_t idx)
{
// if (_error != BA_OK) return BA_ERR;
// if (idx >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = idx * _bits;
for (uint8_t i = _bits; i-- > 0;)
{
v <<= 1;
v += _bitget(pos + i);
}
return v;
}
uint32_t BitArray::set(const uint16_t idx, uint32_t value)
{
// if (_error != BA_OK) return BA_ERR;
// if (idx >= _size) return BA_IDX_RANGE;
uint16_t pos = idx * _bits;
uint32_t mask = 1UL;
for (uint8_t i = 0; i < _bits; i++)
{
uint8_t v = (value & mask) > 0 ? 1 : 0;
_bitset(pos + i, v);
mask <<= 1;
}
return value;
}
uint32_t BitArray::toggle(const uint16_t idx)
{
// if (_error != BA_OK) return BA_ERR;
// if (idx >= _size) return BA_IDX_RANGE;
uint32_t v = 0;
uint16_t pos = idx * _bits;
for (uint8_t i = _bits; i-- > 0;)
{
v <<= 1;
v += _bittoggle(pos + i);
}
return v;
}
void BitArray::clear()
{
uint16_t b = _bytes;
for (uint8_t s = 0; s < _segments; s++)
{
uint8_t *p = _ar[s];
if (p)
{
uint8_t t = min(b, BA_SEGMENT_SIZE);
b -= t;
while(t--)
{
*p++ = 0;
}
}
if (b == 0) break;
}
}
// 16 bit address usage is faster
// void BitArray::clear()
// {
// uint16_t b = _bytes;
// for (uint8_t s = 0; s < _segments; s++)
// {
// uint8_t *q = _ar[s];
// uint16_t *p = (uint16_t*)q;
// if (p)
// {
// for (uint8_t t = 0; t < BA_SEGMENT_SIZE/2; t++)
// {
// *p++ = 0; // might be bug @ edge..
// }
// }
// if (b == 0) break;
// }
// }
// PRIVATE
inline uint8_t BitArray::_bitget(uint16_t pos)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
return (p[by] >> bi) & 0x01; // bitRead(p[by], bi);
}
inline void BitArray::_bitset(uint16_t pos, uint8_t value)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
if (value == 0) p[by] &= ~(1 << bi); // bitClear(p[by], bi);
else p[by] |= (1 << bi); // bitSet(p[by], bi);
}
inline uint8_t BitArray::_bittoggle(const uint16_t pos)
{
uint8_t se = 0;
uint16_t re = pos;
while (re >= (BA_SEGMENT_SIZE * 8)) // 8 == #bits in uint8_t
{
se++;
re -= (BA_SEGMENT_SIZE * 8);
}
uint8_t by = re / 8;
uint8_t bi = re & 7;
uint8_t * p = _ar[se];
uint8_t mask = 1 << bi;
p[by] ^= mask;
return (mask > 0);
}
// END OF FILE