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
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