|
| 1 | + |
| 2 | +[](https://github.com/marketplace/actions/arduino_ci) |
| 3 | +[](https://github.com/RobTillaart/SparseArray/actions/workflows/arduino-lint.yml) |
| 4 | +[](https://github.com/RobTillaart/SparseArray/actions/workflows/jsoncheck.yml) |
| 5 | +[](https://github.com/RobTillaart/SparseArray/blob/master/LICENSE) |
| 6 | +[](https://github.com/RobTillaart/SparseArray/releases) |
| 7 | + |
| 8 | + |
| 9 | +# SparseArray |
| 10 | + |
| 11 | +Arduino library for sparse arrays of floats. |
| 12 | + |
| 13 | + |
| 14 | +TODO REDO DOCUMENTATION. |
| 15 | + |
| 16 | + |
| 17 | +## Description |
| 18 | + |
| 19 | +SparseArray is an **experimental** library to implement a one |
| 20 | +dimensional sparse array of floats (a.k.a. vector) on an Arduino. |
| 21 | +A sparse array is a mn array with mostly zeros and a low percentage |
| 22 | +non-zero values. |
| 23 | +The purpose of this library is efficient storage in memory. |
| 24 | + |
| 25 | +The maximum array that can be represented is 65535 elements |
| 26 | +with a theoretical maximum of 65535 non-zero elements. |
| 27 | +(although that does not make sense due to overhead) |
| 28 | +In practice the library limits this to 1000 non-zero elements. |
| 29 | +Note: 255 elements would still fit in an UNO's 2K memory. |
| 30 | + |
| 31 | +Relates to: |
| 32 | +- https://github.com/RobTillaart/SparseMatrix |
| 33 | +- https://github.com/RobTillaart/distanceTable |
| 34 | + |
| 35 | +Note: this library is derived from SparseMatrix. |
| 36 | + |
| 37 | + |
| 38 | +#### Implementation |
| 39 | + |
| 40 | +The implementation is based on 2 arrays holding ``` x, value``` |
| 41 | +where value is float, and x is an uint16_t. |
| 42 | +That are 6 bytes per element. |
| 43 | +The number of elements that the sparse array object can hold are |
| 44 | +given as parameter to the constructor. |
| 45 | +If the space cannot be allocated the size is set to zero. |
| 46 | + |
| 47 | +In the future other data types should be possible. |
| 48 | + |
| 49 | + |
| 50 | +#### Performance |
| 51 | + |
| 52 | +The elements are not kept sorted or indexed so optimizations might be |
| 53 | +possible but are not investigated yet. |
| 54 | +There is however a test sketch to monitor the performance of |
| 55 | +the most important functions. |
| 56 | + |
| 57 | +Accessing elements internally is done with a linear search, |
| 58 | +which becomes (much) slower if the number of elements is increasing. |
| 59 | +This means that although in theory there can be 65535 elements, |
| 60 | +in practice a few 100 can already become annoyingly slow. |
| 61 | +To keep performance a bit the library has a limit build in. |
| 62 | +Check the .h file for **SPARSEARRAY_MAX_SIZE 1000** |
| 63 | + |
| 64 | + |
| 65 | +## Interface |
| 66 | + |
| 67 | +```cpp |
| 68 | +#include "SparseArray.h" |
| 69 | +``` |
| 70 | + |
| 71 | +### Constructor + meta |
| 72 | + |
| 73 | +- **SparseArray(uint16_t size)** constructor. |
| 74 | +Parameter is the maximum number of elements in the sparse array. |
| 75 | +Note this number is limited to **SPARSEARRAY_MAX_SIZE 1000**. |
| 76 | +If the space requested cannot be allocated size will be set to 0. |
| 77 | +- **uint16_t size()** maximum number of elements. |
| 78 | +If this is zero, a problem occurred with allocation happened. |
| 79 | +- **uint16_t count()** current number of elements in the array. |
| 80 | +Should be between 0 and size. |
| 81 | +- **float sum()** sum of all elements ( != 0 ) in the array. |
| 82 | +- **void clear()** resets the array to all zero's again. |
| 83 | + |
| 84 | + |
| 85 | +### Access |
| 86 | + |
| 87 | +- **bool set(uint16_t x, float value)** gives an element in the array a value. |
| 88 | +If the value is set to zero, it is removed from the internal store. |
| 89 | +Returns false if the internal store is full, true otherwise. |
| 90 | +- **float get(uint16_t x)** returns a value from the array. |
| 91 | +- **bool add(uint16_t x, float value)** adds value to an element in the array. |
| 92 | +If needed a new internal element is created. |
| 93 | +If the sum is zero, the element is removed from the internal store. |
| 94 | +Returns false if the internal store is full, true otherwise. |
| 95 | +- **void boundingSegment(uint16_t &minX, uint16_t &maxX)** |
| 96 | +Returns the bounding box in which all values != 0 are located. |
| 97 | +This can be useful for printing or processing the non zero elements. |
| 98 | + |
| 99 | + |
| 100 | +## Future |
| 101 | + |
| 102 | +- documentation |
| 103 | +- test |
| 104 | +- keep in sync with SparseMatrix where possible |
| 105 | +- merge into one class hierarchy? |
| 106 | +- dump should be in the class? |
| 107 | + - or as static function... |
| 108 | + - stream as param dump(Stream str, ... |
| 109 | + |
| 110 | +#### ideas |
| 111 | + |
| 112 | +- array { uint32_t, float }; for logging millis/micros + measurement |
| 113 | + delta coding of time stamp? if it fit in 16 bit? |
| 114 | + => sounds like a class on its own. |
| 115 | + |
| 116 | + |
0 commit comments