forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
4f3ecf0
commit 2500ca6
Showing
3 changed files
with
417 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// | ||
// FILE: set_demo.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.00 | ||
// PURPOSE: demo | ||
// DATE: 2014-11-09 | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include "set.h" | ||
|
||
set myset; | ||
set setA, setB; | ||
|
||
uint32_t start; | ||
uint32_t stop; | ||
|
||
volatile bool b; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
Serial.print("Start set_demo : "); | ||
Serial.println(SET_LIB_VERSION); | ||
Serial.println(); | ||
|
||
Serial.print("myset.clr():\t"); | ||
start = micros(); | ||
myset.clr(); | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
Serial.print("myset.add():\t"); | ||
start = micros(); | ||
for (int i=0; i<256; i++) myset.add(i); | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
Serial.print("myset.sub():\t"); | ||
start = micros(); | ||
for (int i=0; i<256; i++) myset.sub(i); | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
Serial.print("myset.has():\t"); | ||
start = micros(); | ||
for (int i=0; i<256; i++) b = myset.has(i); | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
Serial.print("myset.invert(v):\t"); | ||
start = micros(); | ||
for (int i=0; i<256; i++) myset.invert(i); | ||
; | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
Serial.print("myset.invert():\t"); | ||
start = micros(); | ||
myset.invert(); | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
Serial.print("myset.count():\t"); | ||
start = micros(); | ||
myset.count(); | ||
stop = micros(); | ||
Serial.println(stop-start); | ||
|
||
|
||
Serial.println("\nrandom test"); | ||
randomSeed(1); | ||
setA.clr(); | ||
for (int i=0; i<150; i++) | ||
{ | ||
setA.add(random(256)); | ||
} | ||
for (int i=0; i<4; i++) | ||
{ | ||
for (int j=0; j<64; j++) | ||
{ | ||
Serial.print(setA.has(i*64+j)); | ||
} | ||
Serial.println(); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(); | ||
|
||
Serial.println("\nintersection test"); | ||
randomSeed(1); | ||
setA.clr(); | ||
setB.clr(); | ||
for (int i=0; i<150; i++) | ||
{ | ||
setA.add(random(256)); | ||
setB.add(random(256)); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(setB.count()); | ||
setA.is(setB); | ||
for (int i=0; i<4; i++) | ||
{ | ||
for (int j=0; j<64; j++) | ||
{ | ||
Serial.print(setA.has(i*64+j)); | ||
} | ||
Serial.println(); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(); | ||
|
||
Serial.println("\nunion test"); | ||
randomSeed(1); | ||
setA.clr(); | ||
setB.clr(); | ||
for (int i=0; i<150; i++) | ||
{ | ||
setA.add(random(256)); | ||
setB.add(random(256)); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(setB.count()); | ||
setA.un(setB); | ||
for (int i=0; i<4; i++) | ||
{ | ||
for (int j=0; j<64; j++) | ||
{ | ||
Serial.print(setA.has(i*64+j)); | ||
} | ||
Serial.println(); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(); | ||
|
||
|
||
Serial.println("\ndiff test"); | ||
randomSeed(1); | ||
setA.clr(); | ||
setB.clr(); | ||
for (int i=0; i<150; i++) | ||
{ | ||
setA.add(random(256)); | ||
setB.add(random(256)); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(setB.count()); | ||
setA.df(setB); | ||
for (int i=0; i<4; i++) | ||
{ | ||
for (int j=0; j<64; j++) | ||
{ | ||
Serial.print(setA.has(i*64+j)); | ||
} | ||
Serial.println(); | ||
} | ||
Serial.println(setA.count()); | ||
Serial.println(); | ||
|
||
Serial.println("\nequal test"); | ||
randomSeed(1); | ||
setA.clr(); | ||
setB.clr(); | ||
Serial.println(setA == setB?"true":"false"); | ||
setB.add(0); | ||
Serial.println(setA == setB?"true":"false"); | ||
Serial.println(setA == setA?"true":"false"); | ||
|
||
set setC(setB); | ||
Serial.println(setC == setB?"true":"false"); | ||
Serial.println(setC.count()); | ||
|
||
set setD = setB; | ||
Serial.println(setD == setB?"true":"false"); | ||
Serial.println(setD.count()); | ||
|
||
setD = setA; | ||
Serial.println(setD == setB?"true":"false"); | ||
Serial.println(setD.count()); | ||
|
||
Serial.println("done..."); | ||
Serial.println(); | ||
} | ||
|
||
void loop() | ||
{ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
// | ||
// FILE: set.cpp | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.00 | ||
// PURPOSE: SET library for Arduino | ||
// URL: | ||
// | ||
// HISTORY: | ||
// 0.1.00 by Rob Tillaart 09/11/2014 | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include "set.h" | ||
|
||
///////////////////////////////////////////////////// | ||
// | ||
// PUBLIC | ||
// | ||
|
||
//////////////////////////// | ||
set::set() | ||
{ | ||
clr(); | ||
} | ||
|
||
set::set(set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] = t._mem[i]; | ||
} | ||
} | ||
|
||
|
||
//////////////////////////// | ||
|
||
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) | ||
{ | ||
uint8_t mask = 1 << (v & 7); | ||
uint8_t idx = v / 8; | ||
_mem[idx] &= ~mask; | ||
} | ||
|
||
bool set::has(uint8_t v) | ||
{ | ||
uint8_t mask = 1 << (v & 7); | ||
uint8_t idx = v / 8; | ||
return (_mem[idx] & mask) > 0; | ||
} | ||
|
||
void set::invert(uint8_t v) | ||
{ | ||
uint8_t mask = 1 << (v & 7); | ||
uint8_t idx = v / 8; | ||
_mem[idx] ^= mask; | ||
} | ||
|
||
|
||
//////////////////////////////////// | ||
|
||
// TODO OPTIMIZE COUNT | ||
uint8_t set::count() | ||
{ | ||
uint8_t cnt = 0; | ||
for (int i=0; i<256; i++) | ||
{ | ||
if ( has(i) ) cnt++; | ||
} | ||
return cnt; | ||
} | ||
|
||
uint8_t set::size() | ||
{ | ||
return 256; // TODO _size; | ||
} | ||
|
||
void set::clr() | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] = 0; | ||
} | ||
} | ||
|
||
void set::invert() | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] ^= 0xFF; | ||
} | ||
} | ||
|
||
|
||
/////////////////////////////////////////////////// | ||
void set::operator = (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] = t._mem[i]; | ||
} | ||
} | ||
|
||
void set::operator -= (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] &= ~t._mem[i]; | ||
} | ||
} | ||
|
||
void set::operator &= (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] &= t._mem[i]; | ||
} | ||
} | ||
|
||
void set::operator |= (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] |= t._mem[i]; | ||
} | ||
} | ||
|
||
void set::operator ^= (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
_mem[i] ^= t._mem[i]; | ||
} | ||
} | ||
|
||
bool set::operator == (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
if (_mem[i] != t._mem[i]) return false; | ||
} | ||
return true; | ||
} | ||
|
||
bool set::operator < (set &t) | ||
{ | ||
for (int i=0; i<32; i++) | ||
{ | ||
if (_mem[i] ^= t._mem[i]) return false; | ||
} | ||
return true; | ||
} | ||
// | ||
// END OF FILE | ||
// |
Oops, something went wrong.