Skip to content

Commit f69a50d

Browse files
committed
Set 0.1.09 - added const where possible
1 parent 82a6117 commit f69a50d

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

libraries/Set/Set.cpp

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//
22
// FILE: Set.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.08
4+
// VERSION: 0.1.09
55
// PURPOSE: SET library for Arduino
6-
// URL: http://forum.arduino.cc/index.php?topic=279044.0
6+
// URL:
77
//
88
// HISTORY:
9+
// 0.1.09 2015-07-12 const + constructor
910
// 0.1.08 memset for clr()
1011
// 0.1.07 faster first/next/last/prev; interface
1112
// 0.1.06 added flag to constructor to optimize +,-,*,
@@ -26,12 +27,13 @@
2627
//
2728
// CONSTRUCTORS
2829
//
29-
Set::Set(bool clear)
30+
Set::Set(const bool clear)
3031
{
3132
if (clear)
3233
{
3334
clr();
3435
}
36+
_current = -1;
3537
}
3638

3739
Set::Set(const Set &t)
@@ -40,34 +42,35 @@ Set::Set(const Set &t)
4042
{
4143
_mem[i] = t._mem[i];
4244
}
45+
_current = -1;
4346
}
4447

4548
/////////////////////////////////////////////////////
4649
//
4750
// METHODS
4851
//
49-
void Set::add(uint8_t v)
52+
void Set::add(const uint8_t v)
5053
{
5154
uint8_t mask = 1 << (v & 7);
5255
uint8_t idx = v / 8;
5356
_mem[idx] |= mask;
5457
}
5558

56-
void Set::sub(uint8_t v)
59+
void Set::sub(const uint8_t v)
5760
{
5861
uint8_t mask = 1 << (v & 7);
5962
uint8_t idx = v / 8;
6063
_mem[idx] &= ~mask;
6164
}
6265

63-
void Set::invert(uint8_t v)
66+
void Set::invert(const uint8_t v)
6467
{
6568
uint8_t mask = 1 << (v & 7);
6669
uint8_t idx = v / 8;
6770
_mem[idx] ^= mask;
6871
}
6972

70-
bool Set::has(uint8_t v)
73+
bool Set::has(const uint8_t v)
7174
{
7275
uint8_t mask = 1 << (v & 7);
7376
uint8_t idx = v / 8;
@@ -125,19 +128,19 @@ int Set::next()
125128
return findNext(p, q);
126129
}
127130

128-
int Set::findNext(uint8_t p, uint8_t q)
131+
int Set::findNext(const uint8_t p, const uint8_t q)
129132
{
130-
for (uint8_t i=p; i<32; i++)
133+
for (uint8_t i = p; i < 32; i++)
131134
{
132135
uint8_t b = _mem[i];
133136
if (b != 0)
134137
{
135-
uint8_t mask = 1<<q;
136-
for (uint8_t j=q; j<8; j++)
138+
uint8_t mask = 1 << q;
139+
for (uint8_t j = q; j < 8; j++)
137140
{
138141
if (b & mask)
139142
{
140-
_current = i*8 + j;
143+
_current = i * 8 + j;
141144
return _current;
142145
}
143146
mask <<= 1;
@@ -163,26 +166,26 @@ int Set::last()
163166
return findPrev(31, 7);
164167
}
165168

166-
int Set::findPrev(uint8_t p, uint8_t q)
169+
int Set::findPrev(const uint8_t p, const uint8_t q)
167170
{
168171
uint8_t m = 1 << q;
169-
for (uint8_t i=p; i!=255; --i) // uint < 0
172+
for (uint8_t i = p; i != 255; --i) // uint < 0
170173
{
171174
uint8_t b = _mem[i];
172175
if (b != 0)
173176
{
174177
uint8_t mask = m;
175-
for (uint8_t j=q; j!=255; --j)
178+
for (uint8_t j = q; j != 255; --j)
176179
{
177180
if (b & mask)
178181
{
179-
_current = i*8 + j;
182+
_current = i * 8 + j;
180183
return _current;
181184
}
182185
mask >>= 1;
183186
}
184187
}
185-
m = 1<<7;
188+
m = 1 << 7;
186189
q = 7;
187190
}
188191
_current = -1;
@@ -195,81 +198,81 @@ int Set::findPrev(uint8_t p, uint8_t q)
195198
// OPERATORS
196199
//
197200

198-
Set Set::operator + (Set &t) // union
201+
Set Set::operator + (const Set &t) // union
199202
{
200203
Set s(false);
201-
for (uint8_t i=0; i<32; i++)
204+
for (uint8_t i = 0; i < 32; i++)
202205
{
203206
s._mem[i] = this->_mem[i] | t._mem[i];
204207
}
205208
return s;
206209
}
207210

208-
Set Set::operator - (Set &t) // diff
211+
Set Set::operator - (const Set &t) // diff
209212
{
210213
Set s(false);
211-
for (uint8_t i=0; i<32; i++)
214+
for (uint8_t i = 0; i < 32; i++)
212215
{
213216
s._mem[i] = this->_mem[i] & ~t._mem[i];
214217
}
215218
return s;
216219
}
217220

218-
Set Set::operator * (Set &t) // intersection
221+
Set Set::operator * (const Set &t) // intersection
219222
{
220223
Set s(false);
221-
for (uint8_t i=0; i<32; i++)
224+
for (uint8_t i = 0; i < 32; i++)
222225
{
223226
s._mem[i] = this->_mem[i] & t._mem[i];
224227
}
225228
return s;
226229
}
227230

228-
void Set::operator += (Set &t) // union
231+
void Set::operator += (const Set &t) // union
229232
{
230-
for (uint8_t i=0; i<32; i++)
233+
for (uint8_t i = 0; i < 32; i++)
231234
{
232235
_mem[i] |= t._mem[i];
233236
}
234237
}
235238

236-
void Set::operator -= (Set &t) // diff
239+
void Set::operator -= (const Set &t) // diff
237240
{
238-
for (uint8_t i=0; i<32; i++)
241+
for (uint8_t i = 0; i < 32; i++)
239242
{
240243
_mem[i] &= ~t._mem[i];
241244
}
242245
}
243246

244-
void Set::operator *= (Set &t) // intersection
247+
void Set::operator *= (const Set &t) // intersection
245248
{
246-
for (uint8_t i=0; i<32; i++)
249+
for (uint8_t i = 0; i < 32; i++)
247250
{
248251
_mem[i] &= t._mem[i];
249252
}
250253
}
251254

252-
bool Set::operator == (Set &t) // equal
255+
bool Set::operator == (const Set &t) const // equal
253256
{
254-
for (uint8_t i=0; i<32; i++)
257+
for (uint8_t i = 0; i < 32; i++)
255258
{
256259
if (_mem[i] != t._mem[i]) return false;
257260
}
258261
return true;
259262
}
260263

261-
bool Set::operator != (Set &t) // not equal
264+
bool Set::operator != (const Set &t) const // not equal
262265
{
263-
for (uint8_t i=0; i<32; i++)
266+
for (uint8_t i = 0; i < 32; i++)
264267
{
265268
if (_mem[i] != t._mem[i]) return true;
266269
}
267270
return false;
268271
}
269272

270-
bool Set::operator <= (Set &t) // subSet
273+
bool Set::operator <= (const Set &t) const // subSet
271274
{
272-
for (uint8_t i=0; i<32; i++)
275+
for (uint8_t i = 0; i < 32; i++)
273276
{
274277
if ((_mem[i] & ~t._mem[i]) > 0) return false;
275278
}

libraries/Set/Set.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
22
// FILE: Set.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.08
4+
// VERSION: 0.1.09
55
// PURPOSE: SET library for Arduino
6-
// URL: http://forum.arduino.cc/index.php?topic=279044.0
6+
// URL:
77
//
88
// HISTORY:
99
// see Set.cpp file
@@ -18,35 +18,35 @@
1818
#include <Arduino.h>
1919
#endif
2020

21-
#define SET_LIB_VERSION "0.1.08"
21+
#define SET_LIB_VERSION "0.1.09"
2222

2323
class Set
2424
{
2525
public:
26-
Set(bool clear = true); // create empty Set
26+
explicit Set(const bool clear = true); // create empty Set
2727
Set(const Set &t); // create copy Set
2828

2929
void clr(); // clear the Set
3030
void invert(); // flip all elements in the Set
31-
uint8_t count(); // return the #elements
31+
uint8_t count() const; // return the #elements
3232
// bool isEmpty();
3333

34-
void add(uint8_t); // add element to the Set
35-
void sub(uint8_t); // remove element from Set
36-
void invert(uint8_t); // flip element in Set
37-
bool has(uint8_t); // element is in Set
34+
void add(const uint8_t); // add element to the Set
35+
void sub(const uint8_t); // remove element from Set
36+
void invert(const uint8_t); // flip element in Set
37+
bool has(const uint8_t) const; // element is in Set
3838

39-
Set operator + (Set &); // union
40-
Set operator - (Set &); // diff
41-
Set operator * (Set &); // intersection
39+
Set operator + (const Set &); // union
40+
Set operator - (const Set &); // diff
41+
Set operator * (const Set &); // intersection
4242

43-
void operator += (Set &); // union
44-
void operator -= (Set &); // diff
45-
void operator *= (Set &); // intersection
43+
void operator += (const Set &); // union
44+
void operator -= (const Set &); // diff
45+
void operator *= (const Set &); // intersection
4646

47-
bool operator == (Set &); // equal
48-
bool operator != (Set &); // not equal
49-
bool operator <= (Set &); // is subSet,
47+
bool operator == (const Set &) const; // equal
48+
bool operator != (const Set &) const; // not equal
49+
bool operator <= (const Set &) const; // is subSet,
5050
// a superSet b is not implemented as one could
5151
// say b subSet a (b <= a)
5252
// a <= b
@@ -62,8 +62,8 @@ class Set
6262
private:
6363
uint8_t _mem[32]; // can hold 0..255
6464
int _current;
65-
int findNext(uint8_t, uint8_t); // helper for first, next
66-
int findPrev(uint8_t, uint8_t); // helper for last, prev
65+
int findNext(const uint8_t, const uint8_t); // helper for first, next
66+
int findPrev(const uint8_t, const uint8_t); // helper for last, prev
6767
};
6868
#endif
6969
//

libraries/Set/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Set
2-
version=0.1.08
2+
version=0.1.09
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Library to implement SET datastructure.

0 commit comments

Comments
 (0)