Skip to content

Commit

Permalink
+ 0.1.06 set -> Set (follow Arduino style)
Browse files Browse the repository at this point in the history
+ added flag to constructor to optimize +,-,*,
  • Loading branch information
RobTillaart committed Nov 18, 2014
1 parent ff2d38c commit 3946fd8
Showing 3 changed files with 100 additions and 64 deletions.
49 changes: 40 additions & 9 deletions libraries/set/examples/allTest/allTest.ino
Original file line number Diff line number Diff line change
@@ -2,16 +2,16 @@
// FILE: allTest.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: demo/test set class
// PURPOSE: demo/test Set class
// DATE: 2014-11-16
// URL:
//
// Released to the public domain
//

#include "set.h"
#include "Set.h"

set setA, setB;
Set setA, setB;
volatile bool b;

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

void timingTest()
{
set myset;
Set myset;

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

Serial.println();
Serial.println("iteration 10 elements");
Serial.print("first:\t");
setA.clr();
randomSeed(1);
for (int i=0; i<10; i++)
{
setA.add(random(256));
}
start = micros();
int n = setA.first();
stop = micros();
Serial.println(stop-start);

Serial.print("next:\t");
start = micros();
n = setA.next();
stop = micros();
Serial.println(stop-start);

Serial.print("first + next until -1 :\t");
start = micros();
n = setA.first();
while (n != -1)
{
n = setA.next();
}
stop = micros();
Serial.println(stop-start);
Serial.println();

Serial.println();
}

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

set setC(setB);
Set setC(setB);
Serial.println(setC == setB?"true":"false");
Serial.println(setC.count());

set setD = setB;
Set setD = setB;
Serial.println(setD != setB?"unequal":"equal");
Serial.println(setD == setB?"true":"false");
Serial.println(setD.count());
@@ -247,7 +278,7 @@ void intersection2Test()
setA.add(random(256));
setB.add(random(256));
}
set setC;
Set setC;
setC = setA * setB;
Serial.println(setA.count());
Serial.println(setB.count());
@@ -272,10 +303,10 @@ void intersection2Test()
void subsetTest()
{
Serial.println("SUBSET TEST");
set setE;
Set setE;
for (int i=0; i<5; i++) setE.add(i);

set setF(setE);
Set setF(setE);

Serial.println(setE.count());
Serial.println(setF.count());
65 changes: 35 additions & 30 deletions libraries/set/set.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
//
// FILE: set.cpp
// FILE: Set.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.05
// VERSION: 0.1.06
// PURPOSE: SET library for Arduino
// URL:
//
// HISTORY:
// 0.1.06 added flag to constructor to optimize +,-,*,
// set -> Set
// 0.1.05 bug fixing + performance a.o. count()
// 0.1.04 support for + - *, some optimizations
// 0.1.03 changed &= to *= to follow Pascal conventions
@@ -16,18 +18,21 @@
// Released to the public domain
//

#include "set.h"
#include "Set.h"

/////////////////////////////////////////////////////
//
// CONSTRUCTORS
//
set::set()
Set::Set(bool clear)
{
clr();
if (clear)
{
clr();
}
}

set::set(set &t)
Set::Set(Set &t)
{
for (uint8_t i=0; i<32; i++)
{
@@ -39,36 +44,36 @@ set::set(set &t)
//
// METHODS
//
void set::add(uint8_t v)
void Set::add(uint8_t v)
{
uint8_t mask = 1 << (v & 7);
uint8_t idx = v / 8;
_mem[idx] |= mask;
}

void set::sub(uint8_t v)
void Set::sub(uint8_t v)
{
uint8_t mask = 1 << (v & 7);
uint8_t idx = v / 8;
_mem[idx] &= ~mask;
}

void set::invert(uint8_t v)
void Set::invert(uint8_t v)
{
uint8_t mask = 1 << (v & 7);
uint8_t idx = v / 8;
_mem[idx] ^= mask;
}

bool set::has(uint8_t v)
bool Set::has(uint8_t v)
{
uint8_t mask = 1 << (v & 7);
uint8_t idx = v / 8;
return (_mem[idx] & mask) > 0;
}

// TODO OPTIMIZE COUNT
uint8_t set::count()
uint8_t Set::count()
{
uint8_t cnt = 0;
for (uint8_t i=0; i<32; i++)
@@ -83,23 +88,23 @@ uint8_t set::count()
return cnt;
}

void set::clr()
void Set::clr()
{
for (uint8_t i=0; i<32; i++)
{
_mem[i] = 0;
}
}

void set::invert()
void Set::invert()
{
for (uint8_t i=0; i<32; i++)
{
_mem[i] ^= 0xFF;
}
}

int set::first()
int Set::first()
{
for (int i = 0; i < 256; i++)
{
@@ -113,7 +118,7 @@ int set::first()
return _current;
}

int set::next()
int Set::next()
{
if (_current != -1)
{
@@ -131,7 +136,7 @@ int set::next()
return _current;
}

int set::prev()
int Set::prev()
{
if (_current != -1)
{
@@ -149,7 +154,7 @@ int set::prev()
return _current;
}

int set::last()
int Set::last()
{
_current = -1;
for (int i = 255; i >=0; --i)
@@ -169,61 +174,61 @@ int set::last()
// OPERATORS
//

set set::operator + (set &t) // union
Set Set::operator + (Set &t) // union
{
set s;
for (uint8_t i=0; i<32; i++)
Set s(false);
for (uint8_t i=0; i<32; i++)
{
s._mem[i] = this->_mem[i] | t._mem[i];
}
return s;
}

set set::operator - (set &t) // diff
Set Set::operator - (Set &t) // diff
{
set s;
Set s(false);
for (uint8_t i=0; i<32; i++)
{
s._mem[i] = this->_mem[i] & ~t._mem[i];
}
return s;
}

set set::operator * (set &t) // intersection
Set Set::operator * (Set &t) // intersection
{
set s;
Set s(false);
for (uint8_t i=0; i<32; i++)
{
s._mem[i] = this->_mem[i] & t._mem[i];
}
return s;
}

void set::operator += (set &t) // union
void Set::operator += (Set &t) // union
{
for (uint8_t i=0; i<32; i++)
{
_mem[i] |= t._mem[i];
}
}

void set::operator -= (set &t) // diff
void Set::operator -= (Set &t) // diff
{
for (uint8_t i=0; i<32; i++)
{
_mem[i] &= ~t._mem[i];
}
}

void set::operator *= (set &t) // intersection
void Set::operator *= (Set &t) // intersection
{
for (uint8_t i=0; i<32; i++)
{
_mem[i] &= t._mem[i];
}
}

bool set::operator == (set &t) // equal
bool Set::operator == (Set &t) // equal
{
for (uint8_t i=0; i<32; i++)
{
@@ -232,7 +237,7 @@ bool set::operator == (set &t) // equal
return true;
}

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

bool set::operator <= (set &t) // subset
bool Set::operator <= (Set &t) // subSet
{
for (uint8_t i=0; i<32; i++)
{
Loading

0 comments on commit 3946fd8

Please sign in to comment.