Skip to content

Commit 3946fd8

Browse files
committed
+ 0.1.06 set -> Set (follow Arduino style)
+ added flag to constructor to optimize +,-,*,
1 parent ff2d38c commit 3946fd8

File tree

3 files changed

+100
-64
lines changed

3 files changed

+100
-64
lines changed

libraries/set/examples/allTest/allTest.ino

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
// FILE: allTest.ino
33
// AUTHOR: Rob Tillaart
44
// VERSION: 0.1.00
5-
// PURPOSE: demo/test set class
5+
// PURPOSE: demo/test Set class
66
// DATE: 2014-11-16
77
// URL:
88
//
99
// Released to the public domain
1010
//
1111

12-
#include "set.h"
12+
#include "Set.h"
1313

14-
set setA, setB;
14+
Set setA, setB;
1515
volatile bool b;
1616

1717
void setup()
@@ -38,7 +38,7 @@ void loop()
3838

3939
void timingTest()
4040
{
41-
set myset;
41+
Set myset;
4242

4343
Serial.println("TIMING TEST");
4444
Serial.print("clr():\t");
@@ -157,6 +157,37 @@ void timingTest()
157157
stop = micros();
158158
Serial.println(stop-start);
159159

160+
Serial.println();
161+
Serial.println("iteration 10 elements");
162+
Serial.print("first:\t");
163+
setA.clr();
164+
randomSeed(1);
165+
for (int i=0; i<10; i++)
166+
{
167+
setA.add(random(256));
168+
}
169+
start = micros();
170+
int n = setA.first();
171+
stop = micros();
172+
Serial.println(stop-start);
173+
174+
Serial.print("next:\t");
175+
start = micros();
176+
n = setA.next();
177+
stop = micros();
178+
Serial.println(stop-start);
179+
180+
Serial.print("first + next until -1 :\t");
181+
start = micros();
182+
n = setA.first();
183+
while (n != -1)
184+
{
185+
n = setA.next();
186+
}
187+
stop = micros();
188+
Serial.println(stop-start);
189+
Serial.println();
190+
160191
Serial.println();
161192
}
162193

@@ -171,11 +202,11 @@ void equalTest()
171202
Serial.println(setA == setB?"true":"false");
172203
Serial.println(setA == setA?"true":"false");
173204

174-
set setC(setB);
205+
Set setC(setB);
175206
Serial.println(setC == setB?"true":"false");
176207
Serial.println(setC.count());
177208

178-
set setD = setB;
209+
Set setD = setB;
179210
Serial.println(setD != setB?"unequal":"equal");
180211
Serial.println(setD == setB?"true":"false");
181212
Serial.println(setD.count());
@@ -247,7 +278,7 @@ void intersection2Test()
247278
setA.add(random(256));
248279
setB.add(random(256));
249280
}
250-
set setC;
281+
Set setC;
251282
setC = setA * setB;
252283
Serial.println(setA.count());
253284
Serial.println(setB.count());
@@ -272,10 +303,10 @@ void intersection2Test()
272303
void subsetTest()
273304
{
274305
Serial.println("SUBSET TEST");
275-
set setE;
306+
Set setE;
276307
for (int i=0; i<5; i++) setE.add(i);
277308

278-
set setF(setE);
309+
Set setF(setE);
279310

280311
Serial.println(setE.count());
281312
Serial.println(setF.count());

libraries/set/set.cpp

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//
2-
// FILE: set.cpp
2+
// FILE: Set.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.05
4+
// VERSION: 0.1.06
55
// PURPOSE: SET library for Arduino
66
// URL:
77
//
88
// HISTORY:
9+
// 0.1.06 added flag to constructor to optimize +,-,*,
10+
// set -> Set
911
// 0.1.05 bug fixing + performance a.o. count()
1012
// 0.1.04 support for + - *, some optimizations
1113
// 0.1.03 changed &= to *= to follow Pascal conventions
@@ -16,18 +18,21 @@
1618
// Released to the public domain
1719
//
1820

19-
#include "set.h"
21+
#include "Set.h"
2022

2123
/////////////////////////////////////////////////////
2224
//
2325
// CONSTRUCTORS
2426
//
25-
set::set()
27+
Set::Set(bool clear)
2628
{
27-
clr();
29+
if (clear)
30+
{
31+
clr();
32+
}
2833
}
2934

30-
set::set(set &t)
35+
Set::Set(Set &t)
3136
{
3237
for (uint8_t i=0; i<32; i++)
3338
{
@@ -39,36 +44,36 @@ set::set(set &t)
3944
//
4045
// METHODS
4146
//
42-
void set::add(uint8_t v)
47+
void Set::add(uint8_t v)
4348
{
4449
uint8_t mask = 1 << (v & 7);
4550
uint8_t idx = v / 8;
4651
_mem[idx] |= mask;
4752
}
4853

49-
void set::sub(uint8_t v)
54+
void Set::sub(uint8_t v)
5055
{
5156
uint8_t mask = 1 << (v & 7);
5257
uint8_t idx = v / 8;
5358
_mem[idx] &= ~mask;
5459
}
5560

56-
void set::invert(uint8_t v)
61+
void Set::invert(uint8_t v)
5762
{
5863
uint8_t mask = 1 << (v & 7);
5964
uint8_t idx = v / 8;
6065
_mem[idx] ^= mask;
6166
}
6267

63-
bool set::has(uint8_t v)
68+
bool Set::has(uint8_t v)
6469
{
6570
uint8_t mask = 1 << (v & 7);
6671
uint8_t idx = v / 8;
6772
return (_mem[idx] & mask) > 0;
6873
}
6974

7075
// TODO OPTIMIZE COUNT
71-
uint8_t set::count()
76+
uint8_t Set::count()
7277
{
7378
uint8_t cnt = 0;
7479
for (uint8_t i=0; i<32; i++)
@@ -83,23 +88,23 @@ uint8_t set::count()
8388
return cnt;
8489
}
8590

86-
void set::clr()
91+
void Set::clr()
8792
{
8893
for (uint8_t i=0; i<32; i++)
8994
{
9095
_mem[i] = 0;
9196
}
9297
}
9398

94-
void set::invert()
99+
void Set::invert()
95100
{
96101
for (uint8_t i=0; i<32; i++)
97102
{
98103
_mem[i] ^= 0xFF;
99104
}
100105
}
101106

102-
int set::first()
107+
int Set::first()
103108
{
104109
for (int i = 0; i < 256; i++)
105110
{
@@ -113,7 +118,7 @@ int set::first()
113118
return _current;
114119
}
115120

116-
int set::next()
121+
int Set::next()
117122
{
118123
if (_current != -1)
119124
{
@@ -131,7 +136,7 @@ int set::next()
131136
return _current;
132137
}
133138

134-
int set::prev()
139+
int Set::prev()
135140
{
136141
if (_current != -1)
137142
{
@@ -149,7 +154,7 @@ int set::prev()
149154
return _current;
150155
}
151156

152-
int set::last()
157+
int Set::last()
153158
{
154159
_current = -1;
155160
for (int i = 255; i >=0; --i)
@@ -169,61 +174,61 @@ int set::last()
169174
// OPERATORS
170175
//
171176

172-
set set::operator + (set &t) // union
177+
Set Set::operator + (Set &t) // union
173178
{
174-
set s;
175-
for (uint8_t i=0; i<32; i++)
179+
Set s(false);
180+
for (uint8_t i=0; i<32; i++)
176181
{
177182
s._mem[i] = this->_mem[i] | t._mem[i];
178183
}
179184
return s;
180185
}
181186

182-
set set::operator - (set &t) // diff
187+
Set Set::operator - (Set &t) // diff
183188
{
184-
set s;
189+
Set s(false);
185190
for (uint8_t i=0; i<32; i++)
186191
{
187192
s._mem[i] = this->_mem[i] & ~t._mem[i];
188193
}
189194
return s;
190195
}
191196

192-
set set::operator * (set &t) // intersection
197+
Set Set::operator * (Set &t) // intersection
193198
{
194-
set s;
199+
Set s(false);
195200
for (uint8_t i=0; i<32; i++)
196201
{
197202
s._mem[i] = this->_mem[i] & t._mem[i];
198203
}
199204
return s;
200205
}
201206

202-
void set::operator += (set &t) // union
207+
void Set::operator += (Set &t) // union
203208
{
204209
for (uint8_t i=0; i<32; i++)
205210
{
206211
_mem[i] |= t._mem[i];
207212
}
208213
}
209214

210-
void set::operator -= (set &t) // diff
215+
void Set::operator -= (Set &t) // diff
211216
{
212217
for (uint8_t i=0; i<32; i++)
213218
{
214219
_mem[i] &= ~t._mem[i];
215220
}
216221
}
217222

218-
void set::operator *= (set &t) // intersection
223+
void Set::operator *= (Set &t) // intersection
219224
{
220225
for (uint8_t i=0; i<32; i++)
221226
{
222227
_mem[i] &= t._mem[i];
223228
}
224229
}
225230

226-
bool set::operator == (set &t) // equal
231+
bool Set::operator == (Set &t) // equal
227232
{
228233
for (uint8_t i=0; i<32; i++)
229234
{
@@ -232,7 +237,7 @@ bool set::operator == (set &t) // equal
232237
return true;
233238
}
234239

235-
bool set::operator != (set &t) // not equal
240+
bool Set::operator != (Set &t) // not equal
236241
{
237242
for (uint8_t i=0; i<32; i++)
238243
{
@@ -241,7 +246,7 @@ bool set::operator != (set &t) // not equal
241246
return false;
242247
}
243248

244-
bool set::operator <= (set &t) // subset
249+
bool Set::operator <= (Set &t) // subSet
245250
{
246251
for (uint8_t i=0; i<32; i++)
247252
{

0 commit comments

Comments
 (0)