GY-63_MS5611/libraries/SparseMatrix/SparseMatrix.cpp

173 lines
2.9 KiB
C++
Raw Normal View History

2022-07-12 14:20:20 -04:00
//
// FILE: SparseMatrix.cpp
// AUTHOR: Rob Tillaart
2022-11-25 08:22:25 -05:00
// VERSION: 0.1.4
2022-07-12 14:20:20 -04:00
// DATE: 2022-07-12
// PURPOSE: Arduino library for sparse matrices
2022-07-14 11:28:24 -04:00
// URL: https://github.com/RobTillaart/SparseMatrix
2022-07-12 14:20:20 -04:00
#include "SparseMatrix.h"
2022-07-14 11:28:24 -04:00
SparseMatrix::SparseMatrix(uint16_t sz)
2022-07-12 14:20:20 -04:00
{
_count = 0;
_size = sz;
2022-07-14 11:28:24 -04:00
if ( _size > SPARSEMATRIX_MAX_SIZE)
{
_size = SPARSEMATRIX_MAX_SIZE;
}
2022-07-12 14:20:20 -04:00
_x = (uint8_t *) malloc(sz);
_y = (uint8_t *) malloc(sz);
_value = (float *) malloc(sz * sizeof(float));
// catch malloc error
if (_x && _y && _value) return;
2022-07-13 11:47:55 -04:00
// if malloc error set size to zero.
2022-07-12 14:20:20 -04:00
_size = 0;
}
SparseMatrix::~SparseMatrix()
{
if (_x) free(_x);
if (_y) free(_y);
if (_value) free(_value);
}
2022-07-14 11:28:24 -04:00
uint16_t SparseMatrix::size()
2022-07-12 14:20:20 -04:00
{
return _size;
}
2022-07-14 11:28:24 -04:00
uint16_t SparseMatrix::count()
2022-07-12 14:20:20 -04:00
{
return _count;
}
2022-07-13 11:47:55 -04:00
void SparseMatrix::clear()
{
_count = 0;
}
2022-07-12 14:20:20 -04:00
float SparseMatrix::sum()
{
float _sum = 0;
2022-07-14 11:28:24 -04:00
for (uint16_t i = 0; i < _count; i++)
2022-07-12 14:20:20 -04:00
{
_sum += _value[i];
}
return _sum;
}
bool SparseMatrix::set(uint8_t x, uint8_t y, float value)
{
2022-07-14 11:28:24 -04:00
int32_t pos = findPos(x, y);
2022-07-12 14:20:20 -04:00
// existing element
if (pos > -1)
{
2022-07-16 07:42:58 -04:00
_value[pos] = value;
if (_value[pos] == 0.0) removeElement(pos);
2022-07-13 11:47:55 -04:00
return true;
}
// does not exist => new element ?
2022-07-16 07:42:58 -04:00
return newElement(x, y, value);
2022-07-13 11:47:55 -04:00
}
bool SparseMatrix::add(uint8_t x, uint8_t y, float value)
{
2022-07-14 11:28:24 -04:00
int32_t pos = findPos(x, y);
2022-07-13 11:47:55 -04:00
// existing element
if (pos > -1)
{
_value[pos] += value;
2022-07-16 07:42:58 -04:00
if (_value[pos] == 0.0) removeElement(pos);
2022-07-12 14:20:20 -04:00
return true;
}
2022-07-13 11:47:55 -04:00
// does not exist => new element ?
2022-07-16 07:42:58 -04:00
return newElement(x, y, value);
2022-07-12 14:20:20 -04:00
}
float SparseMatrix::get(uint8_t x, uint8_t y)
{
2022-07-14 11:28:24 -04:00
int32_t pos = findPos(x, y);
2022-07-12 14:20:20 -04:00
if (pos > -1)
{
return _value[pos];
}
return 0;
}
2022-07-16 07:42:58 -04:00
void SparseMatrix::boundingBox(uint8_t &minX, uint8_t &maxX, uint8_t &minY, uint8_t &maxY)
{
uint8_t _minx = 255, _maxx = 0,
_miny = 255, _maxy = 0;
for (uint16_t i = 0; i < _count; i++)
{
if (_x[i] < _minx) _minx = _x[i];
if (_x[i] > _maxx) _maxx = _x[i];
if (_y[i] < _miny) _miny = _y[i];
if (_y[i] > _maxy) _maxy = _y[i];
}
minX = _minx;
maxX = _maxx;
minY = _miny;
maxY = _maxy;
}
2022-07-14 11:28:24 -04:00
//////////////////////////////////////////////////////
//
// PRIVATE
//
int32_t SparseMatrix::findPos(uint8_t x, uint8_t y)
2022-07-12 14:20:20 -04:00
{
2022-07-14 11:28:24 -04:00
// linear search - not optimized.
for (uint16_t i = 0; i < _count; i++)
2022-07-12 14:20:20 -04:00
{
if ((_x[i] == x) && (_y[i] == y))
{
2022-07-14 11:28:24 -04:00
return (int32_t)i;
2022-07-12 14:20:20 -04:00
}
}
return -1;
}
2022-07-16 07:42:58 -04:00
void SparseMatrix::removeElement(uint16_t pos)
{
_count--;
// move last element
// efficiency (keep sorted) is no requirement.
if (pos == _count) return;
_x[pos] = _x[_count];
_y[pos] = _y[_count];
_value[pos] = _value[_count];
}
bool SparseMatrix::newElement(uint8_t x, uint8_t y, float value)
{
if (value == 0.0) return true;
if (_count >= _size) return false;
_x[_count] = x;
_y[_count] = y;
_value[_count] = value;
_count++;
return true;
}
2022-07-12 14:20:20 -04:00
// -- END OF FILE --
2022-07-14 11:28:24 -04:00