0.3.3 DistanceTable

This commit is contained in:
Rob Tillaart 2023-09-25 20:41:00 +02:00
parent f6dbdb9143
commit 67b03021cc
6 changed files with 67 additions and 42 deletions

View File

@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.3] - 2023-09-25
- update changelog
- update readme.md
## [0.3.2] - 2022-11.02 ## [0.3.2] - 2022-11.02
- add changelog.md - add changelog.md
- add rp2040 to build-CI - add rp2040 to build-CI
@ -56,15 +61,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.3] - 2017-07-27 ## [0.1.3] - 2017-07-27
- Fix issue #33 - Fix issue #33
## [0.1.2] ## [0.1.2] - 2017-07-15
- fix overflow - fix overflow
- add some error detection - add some error detection
- revert float to float to memory - revert float to float to memory
## [0.1.01] ## [0.1.01] - 2015-06-19
- refactor - refactor
## [0.1.00] ## [0.1.00] - 2015-06-18
- initial version - initial version

View File

@ -1,11 +1,9 @@
// //
// FILE: DistanceTable.cpp // FILE: DistanceTable.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.3.2 // VERSION: 0.3.3
// PURPOSE: Arduino library to store a symmetrical distance table in less memory // PURPOSE: Arduino library to store a symmetrical distance table in less memory
// URL: https://github.com/RobTillaart/DistanceTable // URL: https://github.com/RobTillaart/DistanceTable
//
// HISTORY: see changelog.md
#include "DistanceTable.h" #include "DistanceTable.h"
@ -53,7 +51,7 @@ void DistanceTable::setAll(float value)
bool DistanceTable::set(uint8_t x, uint8_t y, float value ) bool DistanceTable::set(uint8_t x, uint8_t y, float value )
{ {
// comment next line to skip range check (squeeze performance) // comment next line to skip range check (squeeze performance)
if ( (x >= _dimension) || (y >= _dimension)) return false; if ( (x >= _dimension) || (y >= _dimension)) return false;
if (x == y) return (value == 0.0); if (x == y) return (value == 0.0);
@ -62,7 +60,7 @@ bool DistanceTable::set(uint8_t x, uint8_t y, float value )
uint8_t t = x; x = y; y = t; // swap uint8_t t = x; x = y; y = t; // swap
if (_invert) value = -value; if (_invert) value = -value;
} }
// prevent overflow by moving to 16 bit // prevent overflow by moving to 16 bit
uint16_t index = x; uint16_t index = x;
index = (index * (index - 1)) / 2 + y; index = (index * (index - 1)) / 2 + y;
_distanceTable[index] = value; _distanceTable[index] = value;
@ -72,7 +70,7 @@ bool DistanceTable::set(uint8_t x, uint8_t y, float value )
float DistanceTable::get (uint8_t x, uint8_t y) float DistanceTable::get (uint8_t x, uint8_t y)
{ {
// comment next line to skip range check (squeeze performance) // comment next line to skip range check (squeeze performance)
if ( (x >= _dimension) || (y >= _dimension)) return -1; // NAN ? if ( (x >= _dimension) || (y >= _dimension)) return -1; // NAN ?
if ( x == y ) return 0.0; if ( x == y ) return 0.0;
@ -90,7 +88,7 @@ float DistanceTable::get (uint8_t x, uint8_t y)
}; };
// triangular dump // triangular dump
void DistanceTable::dump(Print * stream) void DistanceTable::dump(Print * stream)
{ {
stream->println(); stream->println();
@ -204,7 +202,7 @@ uint16_t DistanceTable::count(float value, float epsilon)
} }
} }
if (_invert) return cnt; if (_invert) return cnt;
return cnt * 2; // count the symmetrical elements too. return cnt * 2; // count the symmetrical elements too.
} }
@ -238,9 +236,9 @@ uint16_t DistanceTable::countBelow(float value)
} }
} }
if (_invert) return cnt; if (_invert) return cnt;
return cnt * 2; // count the symmetrical elements too. return cnt * 2; // count the symmetrical elements too.
} }
// --- END OF FILE --- // --- END OF FILE ---

View File

@ -2,7 +2,7 @@
// //
// FILE: DistanceTable.h // FILE: DistanceTable.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.3.2 // VERSION: 0.3.3
// PURPOSE: Arduino library to store a symmetrical distance table in less memory // PURPOSE: Arduino library to store a symmetrical distance table in less memory
// URL: https://github.com/RobTillaart/DistanceTable // URL: https://github.com/RobTillaart/DistanceTable
// //
@ -11,7 +11,7 @@
#include "Arduino.h" #include "Arduino.h"
#define DISTANCETABLE_LIB_VERSION (F("0.3.2")) #define DISTANCETABLE_LIB_VERSION (F("0.3.3"))
class DistanceTable class DistanceTable
@ -66,5 +66,5 @@ protected:
}; };
// --- END OF FILE --- // --- END OF FILE ---

View File

@ -15,9 +15,9 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/DistanceTable" "url": "https://github.com/RobTillaart/DistanceTable"
}, },
"version": "0.3.2", "version": "0.3.3",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "*",
"platforms": "*", "platforms": "*",
"headers": "DistanceTable.h" "headers": "DistanceTable.h"
} }

View File

@ -1,9 +1,9 @@
name=DistanceTable name=DistanceTable
version=0.3.2 version=0.3.3
author=Rob Tillaart <rob.tillaart@gmail.com> author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com> maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for a memory efficient DistanceTable for Arduino. sentence=Library for a memory efficient DistanceTable for Arduino.
paragraph= paragraph=
category=Data Processing category=Data Processing
url=https://github.com/RobTillaart/DistanceTable url=https://github.com/RobTillaart/DistanceTable
architectures=* architectures=*

View File

@ -1,9 +1,12 @@
[![Arduino CI](https://github.com/RobTillaart/Distancetable/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) [![Arduino CI](https://github.com/RobTillaart/DistanceTable/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/DistanceTable/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DistanceTable/actions/workflows/arduino-lint.yml) [![Arduino-lint](https://github.com/RobTillaart/DistanceTable/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/DistanceTable/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/DistanceTable/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DistanceTable/actions/workflows/jsoncheck.yml) [![JSON check](https://github.com/RobTillaart/DistanceTable/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/DistanceTable/actions/workflows/jsoncheck.yml)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/Distancetable/blob/master/LICENSE) [![GitHub issues](https://img.shields.io/github/issues/RobTillaart/DistanceTable.svg)](https://github.com/RobTillaart/DistanceTable/issues)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/Distancetable.svg?maxAge=3600)](https://github.com/RobTillaart/Distancetable/releases)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/DistanceTable/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/DistanceTable.svg?maxAge=3600)](https://github.com/RobTillaart/DistanceTable/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/DistanceTable.svg)](https://registry.platformio.org/libraries/robtillaart/DistanceTable)
# DistanceTable # DistanceTable
@ -25,9 +28,11 @@ Within the 2K RAM of an Arduino one could store normally a 21 x 21 matrix (1764
The class therefore saves around 50% of RAM. The class therefore saves around 50% of RAM.
The price is performance as it takes more time to access the elements. The price is performance as it takes more time to access the elements.
Relates to: #### Related
- https://github.com/RobTillaart/SparseArray
- https://github.com/RobTillaart/SparseMatrix - https://github.com/RobTillaart/SparseArray
- https://github.com/RobTillaart/SparseMatrix
- https://www.codeproject.com/Tips/5368686/From-Linear-to-Upper-Triangular-Matrix-Indices
## Interface ## Interface
@ -41,7 +46,7 @@ Relates to:
- **DistanceTable(uint8_t size, float value = 0.0)** Constructor, allocates memory and - **DistanceTable(uint8_t size, float value = 0.0)** Constructor, allocates memory and
sets initial value to all elements. sets initial value to all elements.
If memory cannot be allocated, the size will be set to 0. If memory cannot be allocated, the size will be set to 0.
- **~DistanceTable();** Destructor, frees allocated memory. - **~DistanceTable()** Destructor, frees allocated memory.
- **void clear()** sets all entries to 0.0. - **void clear()** sets all entries to 0.0.
- **void setAll(float value)** sets all entries to value. - **void setAll(float value)** sets all entries to value.
- **bool set(uint8_t x, uint8_t y, float value )** sets a value for (x,y) and automatically for (y, x). - **bool set(uint8_t x, uint8_t y, float value )** sets a value for (x,y) and automatically for (y, x).
@ -92,12 +97,12 @@ functions need to test it.
In practice this means slower, especially when the flag is set to true. In practice this means slower, especially when the flag is set to true.
Some application thoughts: Some application thoughts:
- difference in height - difference in height between points on a route or map.
- energy levels e.g. transitions of electrons, other physics - energy levels e.g. transitions of electrons, other physics phenomena.
- transactions between players of a game - transactions between players of a game, + for one is - for the other.
- hold a 2D colour space - hold a 2D colour space
Note: this function is not tested extensively. Note: this functionality is not tested extensively.
### Debug ### Debug
@ -117,24 +122,32 @@ See examples.
## Future ## Future
#### Must
- improve documentation - improve documentation
#### Should
- improve unit tests - improve unit tests
- rethink class hierarchy (see ideas below).
#### Could
- add examples - add examples
- Note: table can be used for other symmetrical 2D tables. - Note: table can be used for other symmetrical 2D tables.
- And therefore include negative values - And therefore include negative values
- investigate behaviour of **count()** functions a bit more. - investigate behaviour of **count()** functions a bit more.
- include diagonal? - include diagonal?
- could the "non-inverted" distance table be a derived or base class?
- more performant
#### derived or related classes
- int64_t int32_t int16_t int8_t + unsigned variants? (8 variations? invert flag)
- Template class? (release 0.4.0 ?)
- should the "non-inverted" distance table be a derived (performant) class?
- diagonal not zero class, - diagonal not zero class,
- uses a more bytes but allows also extra functionality while staying symmetrical. - uses more bytes but allows also extra functionality while staying symmetrical.
- what if dimension(x) = 2x dimension(y) - non square tables.
- is there a spectrum of sparsity of a matrix?
- int64_t int32_t int16_t int8_t + unsigned variants? (8 variations? invert flag)
- Template class? (release 0.4.0 ?)
#### won't #### won't
@ -143,3 +156,12 @@ See examples.
- Hamilton paths? - Hamilton paths?
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,