2014-11-18 13:28:12 -05:00
|
|
|
//
|
|
|
|
// FILE: Set.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2017-07-16 14:09:23 -04:00
|
|
|
// VERSION: 0.1.11
|
2014-11-18 13:28:12 -05:00
|
|
|
// PURPOSE: SET library for Arduino
|
2016-12-18 05:19:56 -05:00
|
|
|
// URL:
|
2014-11-18 13:28:12 -05:00
|
|
|
//
|
|
|
|
// HISTORY:
|
2017-07-16 14:09:23 -04:00
|
|
|
// 0.1.11 2017-07-16 fix count() --> 16 bit when set is full !
|
2017-07-16 11:33:43 -04:00
|
|
|
// 0.1.10 2017-07-16 performance refactor. isEmpty()
|
2016-12-18 05:19:56 -05:00
|
|
|
// 0.1.09 2015-07-12 const + constructor
|
2016-01-03 13:11:17 -05:00
|
|
|
// 0.1.08 memset for clr()
|
2015-03-07 12:46:16 -05:00
|
|
|
// 0.1.07 faster first/next/last/prev; interface
|
2014-11-18 13:28:12 -05:00
|
|
|
// 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
|
|
|
|
// 0.1.02 documentation
|
|
|
|
// 0.1.01 extending/refactor etc (09/11/2014)
|
|
|
|
// 0.1.00 initial version by Rob Tillaart (09/11/2014)
|
|
|
|
//
|
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Set.h"
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CONSTRUCTORS
|
|
|
|
//
|
2016-12-18 05:19:56 -05:00
|
|
|
Set::Set(const bool clear)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
if (clear)
|
|
|
|
{
|
|
|
|
clr();
|
|
|
|
}
|
2016-12-18 05:19:56 -05:00
|
|
|
_current = -1;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
2015-03-07 12:46:16 -05:00
|
|
|
Set::Set(const Set &t)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<32; i++)
|
|
|
|
{
|
|
|
|
_mem[i] = t._mem[i];
|
|
|
|
}
|
2016-12-18 05:19:56 -05:00
|
|
|
_current = -1;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// METHODS
|
|
|
|
//
|
2016-12-18 05:19:56 -05:00
|
|
|
void Set::add(const uint8_t v)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
uint8_t idx = v / 8;
|
2017-07-16 11:33:43 -04:00
|
|
|
_mem[idx] |= masks[v&7];
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
void Set::sub(const uint8_t v)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
uint8_t idx = v / 8;
|
2017-07-16 11:33:43 -04:00
|
|
|
_mem[idx] &= ~masks[v&7];
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
void Set::invert(const uint8_t v)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
uint8_t idx = v / 8;
|
2017-07-16 11:33:43 -04:00
|
|
|
_mem[idx] ^= masks[v&7];;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
bool Set::has(const uint8_t v)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
uint8_t idx = v / 8;
|
2017-07-16 11:33:43 -04:00
|
|
|
return (_mem[idx] & masks[v&7]) > 0;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
2017-07-16 14:09:23 -04:00
|
|
|
uint16_t Set::count() const
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2017-07-16 14:09:23 -04:00
|
|
|
uint16_t cnt = 0;
|
2014-11-18 13:28:12 -05:00
|
|
|
for (uint8_t i=0; i<32; i++)
|
|
|
|
{
|
|
|
|
// kerningham bit count trick
|
|
|
|
uint8_t b = _mem[i];
|
|
|
|
for (; b; cnt++)
|
|
|
|
{
|
|
|
|
b &= b-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Set::clr()
|
|
|
|
{
|
2016-01-03 13:11:17 -05:00
|
|
|
memset(_mem, 0, 32);
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void Set::invert()
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<32; i++)
|
|
|
|
{
|
|
|
|
_mem[i] ^= 0xFF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-16 11:33:43 -04:00
|
|
|
bool Set::isEmpty()
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<32; i++)
|
|
|
|
{
|
|
|
|
if (_mem[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-01-03 13:11:17 -05:00
|
|
|
|
2017-07-16 14:09:23 -04:00
|
|
|
bool Set::isFull()
|
|
|
|
{
|
|
|
|
for (uint8_t i=0; i<32; i++)
|
|
|
|
{
|
|
|
|
if (_mem[i] != 255) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-18 13:28:12 -05:00
|
|
|
int Set::first()
|
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
return findNext(0,0);
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int Set::next()
|
|
|
|
{
|
2016-01-03 13:11:17 -05:00
|
|
|
if (_current & 0x8000) return -1; // if current == -1
|
2015-03-07 12:46:16 -05:00
|
|
|
_current++;
|
|
|
|
uint8_t p = (uint8_t)_current / 8;
|
|
|
|
uint8_t q = (uint8_t)_current & 7;
|
|
|
|
return findNext(p, q);
|
|
|
|
}
|
|
|
|
|
2017-07-16 11:33:43 -04:00
|
|
|
int Set::findNext(const uint8_t p, uint8_t q)
|
2015-03-07 12:46:16 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = p; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
uint8_t b = _mem[i];
|
|
|
|
if (b != 0)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
uint8_t mask = 1 << q;
|
|
|
|
for (uint8_t j = q; j < 8; j++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
if (b & mask)
|
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
_current = i * 8 + j;
|
2015-03-07 12:46:16 -05:00
|
|
|
return _current;
|
|
|
|
}
|
|
|
|
mask <<= 1;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-07 12:46:16 -05:00
|
|
|
q = 0;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
_current = -1;
|
|
|
|
return _current;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Set::prev()
|
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
if (_current & 0x8000) return -1;
|
|
|
|
_current--;
|
|
|
|
uint8_t p = (uint8_t)_current / 8;
|
|
|
|
uint8_t q = (uint8_t)_current & 7;
|
|
|
|
return findPrev(p, q);
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int Set::last()
|
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
return findPrev(31, 7);
|
|
|
|
}
|
|
|
|
|
2017-07-16 11:33:43 -04:00
|
|
|
int Set::findPrev(const uint8_t p, uint8_t q)
|
2015-03-07 12:46:16 -05:00
|
|
|
{
|
|
|
|
uint8_t m = 1 << q;
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = p; i != 255; --i) // uint < 0
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
uint8_t b = _mem[i];
|
|
|
|
if (b != 0)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2015-03-07 12:46:16 -05:00
|
|
|
uint8_t mask = m;
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t j = q; j != 255; --j)
|
2015-03-07 12:46:16 -05:00
|
|
|
{
|
|
|
|
if (b & mask)
|
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
_current = i * 8 + j;
|
2015-03-07 12:46:16 -05:00
|
|
|
return _current;
|
|
|
|
}
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
2016-12-18 05:19:56 -05:00
|
|
|
m = 1 << 7;
|
2015-03-07 12:46:16 -05:00
|
|
|
q = 7;
|
2014-11-18 13:28:12 -05:00
|
|
|
}
|
2015-03-07 12:46:16 -05:00
|
|
|
_current = -1;
|
2014-11-18 13:28:12 -05:00
|
|
|
return _current;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// OPERATORS
|
|
|
|
//
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
Set Set::operator + (const Set &t) // union
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
Set s(false);
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
s._mem[i] = this->_mem[i] | t._mem[i];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
Set Set::operator - (const Set &t) // diff
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
Set s(false);
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
s._mem[i] = this->_mem[i] & ~t._mem[i];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
Set Set::operator * (const Set &t) // intersection
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
Set s(false);
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
s._mem[i] = this->_mem[i] & t._mem[i];
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
void Set::operator += (const Set &t) // union
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
_mem[i] |= t._mem[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
void Set::operator -= (const Set &t) // diff
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
_mem[i] &= ~t._mem[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
void Set::operator *= (const Set &t) // intersection
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
_mem[i] &= t._mem[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
bool Set::operator == (const Set &t) const // equal
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
if (_mem[i] != t._mem[i]) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
bool Set::operator != (const Set &t) const // not equal
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
if (_mem[i] != t._mem[i]) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:19:56 -05:00
|
|
|
bool Set::operator <= (const Set &t) const // subSet
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
2016-12-18 05:19:56 -05:00
|
|
|
for (uint8_t i = 0; i < 32; i++)
|
2014-11-18 13:28:12 -05:00
|
|
|
{
|
|
|
|
if ((_mem[i] & ~t._mem[i]) > 0) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// END OF FILE
|
|
|
|
//
|