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

190 lines
2.8 KiB
C++
Raw Normal View History

2023-05-18 15:22:02 -04:00
//
// FILE: TOPMIN.cpp
// AUTHOR: Rob Tillaart
2023-11-23 07:46:04 -05:00
// VERSION: 0.2.1
2023-05-18 15:22:02 -04:00
// DATE: 2023-05-18
2023-11-23 07:46:04 -05:00
// PURPOSE: Arduino library to track top n minima.
2023-05-18 15:22:02 -04:00
// URL: https://github.com/RobTillaart/TOPMIN
#include "TOPMIN.h"
TOPMIN::TOPMIN(uint8_t size)
{
_size = size;
if (_size < 3) _size = 3;
_count = 0;
_arr = (float *) malloc(_size * sizeof(float));
}
TOPMIN::~TOPMIN()
{
if (_arr) free(_arr);
}
uint8_t TOPMIN::count()
{
2023-06-16 03:46:29 -04:00
return _count;
2023-05-18 15:22:02 -04:00
}
uint8_t TOPMIN::size()
{
2023-06-16 03:46:29 -04:00
return _size;
2023-05-18 15:22:02 -04:00
}
void TOPMIN::reset()
{
_count = 0;
}
bool TOPMIN::add(float value)
{
2023-06-18 04:25:40 -04:00
if (_arr == NULL) return false;
2023-05-18 15:22:02 -04:00
// initial
if (_count == 0)
{
_arr[_count] = value;
_count++;
return true;
}
// not all elements filled.
if (_count < _size)
{
int i = _count - 1;
while ((i >= 0) && (value > _arr[i]))
{
_arr[i + 1] = _arr[i];
i--;
}
_arr[i + 1] = value;
_count++;
return true;
}
// too small (or equal)
if (value >= _arr[0]) return false;
// insert
int i = 1;
while ((i < _size) && (value < _arr[i]))
{
_arr[i - 1] = _arr[i];
i++;
}
_arr[i - 1] = value;
return true;
}
2023-06-18 04:25:40 -04:00
bool TOPMIN::fill(float value)
2023-05-18 15:22:02 -04:00
{
2023-06-18 04:25:40 -04:00
if (_arr == NULL) return false;
2023-05-18 15:22:02 -04:00
for (int i = 0; i < _size; i++)
{
_arr[i] = value;
}
_count = _size;
2023-06-18 04:25:40 -04:00
return true;
}
float TOPMIN::getValue(uint8_t index)
{
if ((_arr == NULL) || (index >= _count)) return NAN;
return _arr[_count - 1 - index];
2023-05-18 15:22:02 -04:00
}
2023-06-16 03:46:29 -04:00
////////////////////////////////////////////////////
//
// DERIVED
//
TOPMINext::TOPMINext(uint8_t size) : TOPMIN(size)
{
_tag = (uint32_t *) malloc(_size * sizeof(uint32_t));
}
TOPMINext::TOPMINext()
{
if (_tag) free(_tag);
}
bool TOPMINext::add(float value, uint32_t tag)
{
2023-06-18 04:25:40 -04:00
if (_arr == NULL) return false;
if (_tag == NULL) return false;
2023-06-16 03:46:29 -04:00
// initial
if (_count == 0)
{
_arr[_count] = value;
_tag[_count] = tag;
_count++;
return true;
}
// not all elements filled.
if (_count < _size)
{
int i = _count - 1;
while ((i >= 0) && (value > _arr[i]))
{
_arr[i + 1] = _arr[i];
_tag[i + 1] = _tag[i];
i--;
}
_arr[i + 1] = value;
_tag[i + 1] = tag;
_count++;
return true;
}
// too small (or equal)
if (value >= _arr[0]) return false;
// insert
int i = 1;
while ((i < _size) && (value < _arr[i]))
{
_arr[i - 1] = _arr[i];
_tag[i - 1] = _tag[i];
i++;
}
_arr[i - 1] = value;
_tag[i - 1] = tag;
return true;
}
2023-06-18 04:25:40 -04:00
bool TOPMINext::fill(float value, uint32_t tag)
2023-06-16 03:46:29 -04:00
{
2023-06-18 04:25:40 -04:00
if (_arr == NULL) return false;
if (_tag == NULL) return false;
2023-06-16 03:46:29 -04:00
for (int i = 0; i < _size; i++)
{
_arr[i] = value;
_tag[i] = tag;
}
_count = _size;
2023-06-18 04:25:40 -04:00
return true;
}
uint32_t TOPMINext::getTag(uint8_t index)
{
if ((_tag == NULL) || (index >= _count)) return 0xFFFFFFFF;
return _tag[_count - 1 - index];
2023-06-16 03:46:29 -04:00
}
2023-05-18 15:22:02 -04:00
// -- END OF FILE --