+ 0.1.06 set -> Set (follow Arduino style)

+ added flag to constructor to optimize +,-,*,
This commit is contained in:
rob tillaart 2014-11-18 19:23:50 +01:00
parent ff2d38c69b
commit 3946fd88a9
3 changed files with 100 additions and 64 deletions

View File

@ -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());

View File

@ -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,28 +44,28 @@ 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;
@ -68,7 +73,7 @@ bool set::has(uint8_t v)
}
// TODO OPTIMIZE COUNT
uint8_t set::count()
uint8_t Set::count()
{
uint8_t cnt = 0;
for (uint8_t i=0; i<32; i++)
@ -83,7 +88,7 @@ uint8_t set::count()
return cnt;
}
void set::clr()
void Set::clr()
{
for (uint8_t i=0; i<32; i++)
{
@ -91,7 +96,7 @@ void set::clr()
}
}
void set::invert()
void Set::invert()
{
for (uint8_t i=0; i<32; i++)
{
@ -99,7 +104,7 @@ void set::invert()
}
}
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,19 +174,19 @@ 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];
@ -189,9 +194,9 @@ set set::operator - (set &t) // diff
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];
@ -199,7 +204,7 @@ set set::operator * (set &t) // intersection
return s;
}
void set::operator += (set &t) // union
void Set::operator += (Set &t) // union
{
for (uint8_t i=0; i<32; i++)
{
@ -207,7 +212,7 @@ void set::operator += (set &t) // union
}
}
void set::operator -= (set &t) // diff
void Set::operator -= (Set &t) // diff
{
for (uint8_t i=0; i<32; i++)
{
@ -215,7 +220,7 @@ void set::operator -= (set &t) // diff
}
}
void set::operator *= (set &t) // intersection
void Set::operator *= (Set &t) // intersection
{
for (uint8_t i=0; i<32; i++)
{
@ -223,7 +228,7 @@ void set::operator *= (set &t) // intersection
}
}
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++)
{

View File

@ -1,16 +1,16 @@
//
// FILE: set.h
// FILE: Set.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.05
// VERSION: 0.1.06
// PURPOSE: SET library for Arduino
// URL:
//
// HISTORY:
// see set.cpp file
// see Set.cpp file
//
#ifndef set_h
#define set_h
#ifndef Set_h
#define Set_h
#if ARDUINO < 100
#include <WProgram.h>
@ -18,36 +18,36 @@
#include <Arduino.h>
#endif
#define SET_LIB_VERSION "0.1.05"
#define SET_LIB_VERSION "0.1.06"
class set
class Set
{
public:
set(); // create empty set
set(set &t); // create copy set
Set(bool clear = true); // create empty Set
Set(Set &t); // create copy Set
void clr(); // clear the set
void invert(); // flip all elements in the set
void clr(); // clear the Set
void invert(); // flip all elements in the Set
uint8_t count(); // return the #elements
void add(uint8_t); // add element to the set
void sub(uint8_t); // remove element from set
void invert(uint8_t); // flip element in set
bool has(uint8_t); // element is in set
void add(uint8_t); // add element to the Set
void sub(uint8_t); // remove element from Set
void invert(uint8_t); // flip element in Set
bool has(uint8_t); // element is in Set
set operator + (set &t); // union
set operator - (set &t); // diff
set operator * (set &t); // intersection
Set operator + (Set &t); // union
Set operator - (Set &t); // diff
Set operator * (Set &t); // intersection
void operator += (set &t); // union
void operator -= (set &t); // diff
void operator *= (set &t); // intersection
void operator += (Set &t); // union
void operator -= (Set &t); // diff
void operator *= (Set &t); // intersection
bool operator == (set&); // equal
bool operator != (set&); // not equal
bool operator <= (set&); // is subset
bool operator == (Set&); // equal
bool operator != (Set&); // not equal
bool operator <= (Set&); // is subSet
// iterating through the set
// iterating through the Set
// returns value or -1 if not exist
int first(); // find first element
int next(); // find next element