mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.1 TOPMIN
This commit is contained in:
parent
396fc05ddb
commit
b425778412
@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.1.1] - 2023-06-13
|
||||
- add **TOPMINext**, holds a value + tag (e.g. timestamp or counter)
|
||||
- add examples.
|
||||
- changed **get()** into **getValue()**
|
||||
- update readme.md
|
||||
- minor edits.
|
||||
|
||||
|
||||
## [0.1.0] - 2023-05-18
|
||||
- initial version
|
||||
|
||||
|
@ -10,16 +10,35 @@
|
||||
|
||||
Arduino library to track the top N minima.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
This experimental library tracks the top N minima of a series of values.
|
||||
It can be used e.g. to track the top 5 lowest temperatures over a given
|
||||
period of time.
|
||||
|
||||
The library implements two classes:
|
||||
- **TOPMIN** just tracks the values. (smaller footprint).
|
||||
- **TOPMINext** also tracks a user defined 32-bit tag (e.g. timestamp).
|
||||
|
||||
The latter is more useful as it allows to link the top N values to a certain time
|
||||
or day, an index in a database, or another indirect reference.
|
||||
It can even be used to store a second parameter e.g. what was the humidity for
|
||||
the top N temperatures.
|
||||
|
||||
The library can be used for different applications:
|
||||
- track top N minima temperature / humidity over a given period of time.
|
||||
- track top N UV minima intensity during the day.
|
||||
- track top N trading volumes or $$.
|
||||
- track top N smallest fish caught in a contest.
|
||||
- track top N minimum number of visitors during a day.
|
||||
|
||||
The 32 bit tag, can be filled with a 32 bit number like a timestamp, bit can also
|
||||
be used more creatively e.g. 2x 16 bit numbers or even 32 booleans.
|
||||
Any mapping is possible (but not part of the library).
|
||||
|
||||
|
||||
#### Links
|
||||
|
||||
- https://github.com/RobTillaart/TOPMIN
|
||||
- https://github.com/RobTillaart/TOPMAX
|
||||
- https://github.com/RobTillaart/TOPMIN
|
||||
- https://github.com/RobTillaart/runningAverage
|
||||
- https://github.com/RobTillaart/MINMAX
|
||||
@ -31,37 +50,74 @@ period of time.
|
||||
#include "TOPMIN.h"
|
||||
```
|
||||
|
||||
#### TOPMIN
|
||||
|
||||
- **TOPMIN(uint8_t size = 5)** Constructor, defines the number of elements it can hold.
|
||||
Default number of elements is 5. If size < 3 it becomes 3.
|
||||
- **uint8_t count()** returns the number of elements in the internal array.
|
||||
Default number of elements is 5. If **size** < 3 it will be set to 3.
|
||||
The maximum size is currently 255.
|
||||
- **uint8_t count()** returns the number of elements in the internal array. 0.. size.
|
||||
- **uint8_t size()** returns the maximum number of elements in the internal array.
|
||||
- **void reset()** reset the internal counter to 0, clearing the system.
|
||||
- **bool add(float value)** add a value to the TOPMIN object to check it needs to be in the top N of minima.
|
||||
- **float get(uint8_t index)** get an element of the internal array.
|
||||
index == count().
|
||||
- **void reset()** reset the internal counter to 0, logical clearing of the system.
|
||||
- **bool add(float value)** add a value to the TOPMIN object to check of it needs to be
|
||||
in the top N of minima.
|
||||
- **float getValue(uint8_t index)** get an element of the internal array.
|
||||
The index must be <= **count()**, if not the function currently returns **NaN**.
|
||||
This may or may not be a valid value, so the user should guard the **index** parameter carefully.
|
||||
- **bool fill(float value)** convenience function to fill the internal array
|
||||
with a single value e.g. 0.
|
||||
|
||||
|
||||
#### TOPMINext
|
||||
|
||||
Derived from TOPMIN, extended with a tag field.
|
||||
|
||||
- **TOPMINext(uint8_t size = 5)** Constructor, defines the number of elements it can hold.
|
||||
Default number of elements is 5. If **size** < 3 it will be set to 3.
|
||||
The maximum size is currently 255.
|
||||
- **bool add(float value, uint32_t tag)** add a value to the TOPMINext object to check if
|
||||
it needs to be in the top N of minima. If so add the 32-bit **tag** too (at same index).
|
||||
The 32-bit **tag** is typical an index, counter or timestamp, but any semantic is possible.
|
||||
- **uint32_t getTag(uint8_t index)** get the tag from an element of the internal array.
|
||||
The index must be <= **count()**, if not the function currently returns **0xFFFFFFFF**.
|
||||
This may or may not be a valid value, so the user should guard the **index** parameter carefully.
|
||||
- **bool fill(float value, uint32_t tag)** convenience function to fill the internal array
|
||||
with a single value e.g. 0. (tag idem).
|
||||
|
||||
|
||||
# Future
|
||||
|
||||
#### Must
|
||||
|
||||
- keep functional in sync with TOPMIN
|
||||
- documentation
|
||||
- keep functional in sync with TOPMAX.
|
||||
- improve documentation.
|
||||
- 0.2.0
|
||||
- reverse **getValue() / getTag()** be
|
||||
- index = 0 must be the absolute minima (maxima).
|
||||
- if you change size of TOPMIN object the "winner" gets a different index
|
||||
- better would be to have to reverse ```index = count() - index```
|
||||
- then less code adaptions are needed.
|
||||
|
||||
#### Should
|
||||
|
||||
- add unit tests.
|
||||
- for the TOPMINext version if possible
|
||||
- add more examples.
|
||||
|
||||
- example creative use of tag field.
|
||||
- example performance measurements (depends on size + inserted values).
|
||||
(Marsaglia PRNG, to be repeatable over platforms).
|
||||
|
||||
#### Could
|
||||
|
||||
- create template class
|
||||
- create derived class that holds an index or timestamp
|
||||
- uint32_t extra per element.
|
||||
- create template class.
|
||||
- **bool check(float value)** and **bool check(float value, uint32_t tag)**
|
||||
- if (value < getValue(0)) or so.
|
||||
- checks if this value would be added to the TOP-N
|
||||
- **TOP** class, in which the condition can be set as parameter.
|
||||
- a function() returning true or false when comparing 2 values.
|
||||
|
||||
|
||||
|
||||
#### Wont
|
||||
|
||||
- how to handle double/triple etc. entries with same value
|
||||
- they are handled as unique elements, that is what I had in mind.
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: TOPMIN.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// VERSION: 0.1.1
|
||||
// DATE: 2023-05-18
|
||||
// PURPOSE: Arduino library to track top n maxima.
|
||||
// URL: https://github.com/RobTillaart/TOPMIN
|
||||
@ -27,13 +27,13 @@ TOPMIN::~TOPMIN()
|
||||
|
||||
uint8_t TOPMIN::count()
|
||||
{
|
||||
return _count;
|
||||
return _count;
|
||||
}
|
||||
|
||||
|
||||
uint8_t TOPMIN::size()
|
||||
{
|
||||
return _size;
|
||||
return _size;
|
||||
}
|
||||
|
||||
|
||||
@ -82,7 +82,7 @@ bool TOPMIN::add(float value)
|
||||
}
|
||||
|
||||
|
||||
float TOPMIN::get(uint8_t index)
|
||||
float TOPMIN::getValue(uint8_t index)
|
||||
{
|
||||
if (index > _count) return NAN;
|
||||
return _arr[index];
|
||||
@ -99,5 +99,83 @@ void TOPMIN::fill(float value)
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
//
|
||||
// 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)
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
||||
uint32_t TOPMINext::getTag(uint8_t index)
|
||||
{
|
||||
if (index > _count) return 0xFFFFFFFF;
|
||||
return _tag[index];
|
||||
}
|
||||
|
||||
|
||||
void TOPMINext::fill(float value, uint32_t tag)
|
||||
{
|
||||
for (int i = 0; i < _size; i++)
|
||||
{
|
||||
_arr[i] = value;
|
||||
_tag[i] = tag;
|
||||
}
|
||||
_count = _size;
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -2,13 +2,13 @@
|
||||
//
|
||||
// FILE: TOPMIN.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// VERSION: 0.1.1
|
||||
// DATE: 2023-05-18
|
||||
// PURPOSE: Arduino library to track top n minima.
|
||||
// URL: https://github.com/RobTillaart/TOPMIN
|
||||
|
||||
|
||||
#define TOPMIN_LIB_VERSION (F("0.1.0"))
|
||||
#define TOPMIN_LIB_VERSION (F("0.1.1"))
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
@ -23,17 +23,36 @@ public:
|
||||
uint8_t count();
|
||||
uint8_t size();
|
||||
void reset();
|
||||
bool add(float value);
|
||||
float get(uint8_t index);
|
||||
void fill(float value);
|
||||
virtual bool add(float value);
|
||||
virtual void fill(float value);
|
||||
float getValue(uint8_t index);
|
||||
|
||||
|
||||
private:
|
||||
protected:
|
||||
uint8_t _size;
|
||||
uint8_t _count;
|
||||
float * _arr;
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED
|
||||
//
|
||||
class TOPMINext : public TOPMIN
|
||||
{
|
||||
public:
|
||||
TOPMINext(uint8_t size = 5);
|
||||
TOPMINext();
|
||||
|
||||
bool add(float value, uint32_t tag);
|
||||
uint32_t getTag(uint8_t index);
|
||||
void fill(float value, uint32_t tag);
|
||||
|
||||
private:
|
||||
uint32_t * _tag;
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -35,7 +35,7 @@ void loop()
|
||||
Serial.print("\t");
|
||||
for (int i = 0; i < tm.count(); i++)
|
||||
{
|
||||
Serial.print(tm.get(i));
|
||||
Serial.print(tm.getValue(i));
|
||||
Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
@ -47,4 +47,3 @@ void loop()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -0,0 +1,51 @@
|
||||
//
|
||||
// FILE: TOPMINext_count.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: TOPMIN demo
|
||||
// URL: https://github.com/RobTillaart/TOPMIN
|
||||
|
||||
|
||||
#include "TOPMIN.h"
|
||||
|
||||
|
||||
TOPMINext tme(5);
|
||||
uint32_t cnt = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("TOPMIN_LIB_VERSION: ");
|
||||
Serial.println(TOPMIN_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
tme.fill(10000, 0);
|
||||
tme.reset();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
int x = random(10000);
|
||||
|
||||
Serial.print(cnt++);
|
||||
Serial.print("\t");
|
||||
Serial.print(x);
|
||||
Serial.print("\t");
|
||||
for (int i = 0; i < tme.count(); i++)
|
||||
{
|
||||
Serial.print(tme.getValue(i));
|
||||
Serial.print(":(");
|
||||
Serial.print(tme.getTag(i));
|
||||
Serial.print(")\t");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
tme.add(x, cnt); // tag the minima with the "sample counter".
|
||||
|
||||
delay(random(200));
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -0,0 +1,51 @@
|
||||
//
|
||||
// FILE: TOPMINext_millis.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: TOPMIN demo
|
||||
// URL: https://github.com/RobTillaart/TOPMIN
|
||||
|
||||
|
||||
#include "TOPMIN.h"
|
||||
|
||||
|
||||
TOPMINext tme(5);
|
||||
uint32_t cnt = 0;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("TOPMIN_LIB_VERSION: ");
|
||||
Serial.println(TOPMIN_LIB_VERSION);
|
||||
Serial.println();
|
||||
|
||||
tme.fill(0, 0);
|
||||
tme.reset();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
int x = random(10000);
|
||||
|
||||
Serial.print(cnt++);
|
||||
Serial.print("\t");
|
||||
Serial.print(x);
|
||||
Serial.print("\t");
|
||||
for (int i = 0; i < tme.count(); i++)
|
||||
{
|
||||
Serial.print(tme.getValue(i));
|
||||
Serial.print(":(");
|
||||
Serial.print(tme.getTag(i));
|
||||
Serial.print(")\t");
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
tme.add(x, millis());
|
||||
|
||||
delay(random(200));
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -3,16 +3,20 @@
|
||||
|
||||
# Data types (KEYWORD1)
|
||||
TOPMIN KEYWORD1
|
||||
TOPMINext KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
count KEYWORD2
|
||||
size KEYWORD2
|
||||
reset KEYWORD2
|
||||
|
||||
add KEYWORD2
|
||||
get KEYWORD2
|
||||
fill KEYWORD2
|
||||
|
||||
getValue KEYWORD2
|
||||
getTag KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
TOPMIN_LIB_VERSION LITERAL1
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/TOPMIN"
|
||||
},
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=TOPMIN
|
||||
version=0.1.0
|
||||
version=0.1.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library to track top n minima.
|
||||
|
@ -75,7 +75,7 @@ unittest(test_add)
|
||||
}
|
||||
|
||||
|
||||
unittest(test_get)
|
||||
unittest(test_getValue)
|
||||
{
|
||||
TOPMIN tm(5);
|
||||
assertEqual(5, tm.size());
|
||||
@ -84,12 +84,12 @@ unittest(test_get)
|
||||
{
|
||||
tm.add(i);
|
||||
int idx = tm.count();
|
||||
assertEqualFloat(i, tm.get(idx - 1), 0.001);
|
||||
assertEqualFloat(i, tm.getValue(idx - 1), 0.001);
|
||||
}
|
||||
|
||||
for (int i = 0; i < tm.count(); i++)
|
||||
{
|
||||
fprintf(stderr, "%f\t", tm.get(i));
|
||||
fprintf(stderr, "%f\t", tm.getValue(i));
|
||||
}
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
|
Loading…
Reference in New Issue
Block a user