GY-63_MS5611/libraries/SparseArray
2023-11-22 10:48:03 +01:00
..
.github 0.1.1 SparseArray + github actions 2023-02-25 14:53:46 +01:00
examples 0.1.0 SparseArray 2022-07-18 15:54:11 +02:00
test 0.1.0 SparseArray 2022-07-18 15:54:11 +02:00
.arduino-ci.yml 0.1.1 SparseArray 2022-11-25 14:20:28 +01:00
CHANGELOG.md 0.1.2 SparseArray 2023-11-22 10:48:03 +01:00
keywords.txt 0.1.0 SparseArray 2022-07-18 15:54:11 +02:00
library.json 0.1.2 SparseArray 2023-11-22 10:48:03 +01:00
library.properties 0.1.2 SparseArray 2023-11-22 10:48:03 +01:00
LICENSE 0.1.1 SparseArray + github actions 2023-02-25 14:53:46 +01:00
README.md 0.1.2 SparseArray 2023-11-22 10:48:03 +01:00
SparseArray.cpp 0.1.2 SparseArray 2023-11-22 10:48:03 +01:00
SparseArray.h 0.1.2 SparseArray 2023-11-22 10:48:03 +01:00

Arduino CI Arduino-lint JSON check GitHub issues

License: MIT GitHub release PlatformIO Registry

SparseArray

Arduino library for sparse arrays of floats.

Description

SparseArray is an experimental library to implement a one dimensional sparse array of floats (a.k.a. vector) on an Arduino. A sparse array is an array with mostly zeros and a low percentage non-zero values. The purpose of this library is efficient storage in memory.

The maximum array size this library can represent is 65535 elements with a theoretical maximum of 65535 non-zero elements. (although that does not make sense due to overhead) In practice the library limits the number of non-zero elements to 1000. Optionally adjust SPARSEARRAY_MAX_SIZE in the .h file.

Note: 255 non-zero elements would fit in an UNO's 2K memory.

Note: this library is derived from SparseMatrix.

Implementation

The implementation is based on 2 arrays holding x, value where value is float, and x is an uint16_t. That are 6 bytes per element. The number of elements that the sparse array object can hold is given as parameter to the constructor. If the space cannot be allocated the size is set to zero.

In the future other data types should be possible.

Relates to https://github.com/RobTillaart/SET

Performance

The elements are not kept sorted or indexed so optimizations might be possible but are not investigated yet. There is however a test sketch to monitor the performance of the most important functions.

Accessing elements internally is done with a linear search, which becomes (much) slower if the number of elements is increasing. This means that although in theory there can be 65535 elements, in practice a few 100 can already become annoyingly slow. To keep performance a bit the library has a limit build in. Check the .h file for SPARSEARRAY_MAX_SIZE 1000

Interface

#include "SparseArray.h"

Constructor + meta

  • SparseArray(uint16_t size) constructor. Parameter is the maximum number of elements in the sparse array. Note this number is limited to SPARSEARRAY_MAX_SIZE 1000. If the space requested cannot be allocated size will be set to 0.
  • uint16_t size() maximum number of elements. If this is zero, a problem occurred with allocation happened.
  • uint16_t count() current number of elements in the array. Should be between 0 and size.
  • float sum() sum of all elements ( != 0 ) in the array.
  • void clear() resets the array to all zero's again.

Access

  • bool set(uint16_t x, float value) gives an element in the array a value. If the value is set to zero, it is removed from the internal store. Returns false if the internal store is full, true otherwise.
  • float get(uint16_t x) returns a value from the array.
  • bool add(uint16_t x, float value) adds value to an element in the array. If needed a new internal element is created. If the sum is zero, the element is removed from the internal store. Returns false if the internal store is full, true otherwise.
  • void boundingSegment(uint16_t &minX, uint16_t &maxX) Returns the bounding box in which all values != 0 are located. This can be useful for printing or processing the non zero elements.

Future

Must

  • improve documentation

Should

  • do test
  • investigate optimizations
    • derived sorted class ==> insertSort
  • keep in sync with SparseMatrix where possible
    • merge into one class hierarchy?
  • Template class for the data type
    • better than SparseArray64 SparseArray32 SparseArray16 SparseArray8 (signed / unsigned?)

Could

  • dump should be in the class?
    • or as static function...
    • stream as param dump(Stream str, ...)

ideas

  • array { uint32_t, float }; for logging millis/micros + measurement delta coding of time stamp? if it fit in 16 bit? => sounds like a class on its own.
  • investigate index [] operator for get() and set()
    • would break with sparse matrix

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,