Skip to content

Commit f24d899

Browse files
committed
0.3.1 DistanceTable
1 parent 54c9c28 commit f24d899

File tree

11 files changed

+364
-43
lines changed

11 files changed

+364
-43
lines changed

libraries/DistanceTable/DistanceTable.cpp

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: DistanceTable.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.0
4+
// VERSION: 0.3.1
55
// PURPOSE: Arduino library to store a symmetrical distance table in less memory
66
// URL: https://github.com/RobTillaart/DistanceTable
77

@@ -22,7 +22,10 @@
2222
// 0.3.0 2022-01-06 add invert flag, add unit tests
2323
// add countAbove(), countBelow()
2424
// fix allocation + # elements
25-
25+
// 0.3.1 2022-07-22 fix set() get() test order
26+
// set() returns bool on success.
27+
// adds sum() and average()
28+
// add Pascal Triangle example.
2629

2730

2831

@@ -31,8 +34,9 @@
3134

3235
DistanceTable::DistanceTable(uint8_t dimension, float value)
3336
{
34-
// ATMEL 328 has ~2000 bytes RAM,
35-
// so roughly 30X30 = 900 floats(4Bytes) => 1740 bytes is max feasible
37+
// ATMEL 328 has ~2000 bytes RAM,
38+
// so roughly 30 x 30 = 900 floats (4 bytes) => 1740 bytes is max feasible
39+
// no check as other platforms allow larger tables
3640
_invert = false;
3741
_distanceTable = NULL;
3842
_dimension = dimension;
@@ -68,11 +72,11 @@ void DistanceTable::setAll(float value)
6872
};
6973

7074

71-
void DistanceTable::set(uint8_t x, uint8_t y, float value )
75+
bool DistanceTable::set(uint8_t x, uint8_t y, float value )
7276
{
73-
if ( x == y ) return;
7477
// comment next line to skip range check (squeeze performance)
75-
if ( (x >= _dimension) || (y >= _dimension)) return;
78+
if ( (x >= _dimension) || (y >= _dimension)) return false;
79+
if (x == y) return (value == 0.0);
7680

7781
if ( x < y )
7882
{
@@ -83,16 +87,17 @@ void DistanceTable::set(uint8_t x, uint8_t y, float value )
8387
uint16_t index = x;
8488
index = (index * (index - 1)) / 2 + y;
8589
_distanceTable[index] = value;
90+
return true;
8691
};
8792

8893

8994
float DistanceTable::get (uint8_t x, uint8_t y)
9095
{
91-
bool flag = false;
92-
if ( x == y ) return 0.0; // TODO even true when x and y are out of range??
9396
// comment next line to skip range check (squeeze performance)
9497
if ( (x >= _dimension) || (y >= _dimension)) return -1; // NAN ?
98+
if ( x == y ) return 0.0;
9599

100+
bool flag = false;
96101
if ( x < y )
97102
{
98103
uint8_t t = x; x = y; y = t;
@@ -189,6 +194,24 @@ float DistanceTable::maximum(uint8_t &x, uint8_t &y)
189194
}
190195

191196

197+
float DistanceTable::sum()
198+
{
199+
float sum = 0;
200+
for (uint16_t index = 0; index < _elements; index++)
201+
{
202+
sum += _distanceTable[index];
203+
}
204+
return sum * 2; // double it as it is symmetrical
205+
}
206+
207+
208+
float DistanceTable::average()
209+
{
210+
return sum() / _elements;
211+
}
212+
213+
214+
192215
uint16_t DistanceTable::count(float value, float epsilon)
193216
{
194217
uint16_t cnt = 0;

libraries/DistanceTable/DistanceTable.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: DistanceTable.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.3.0
5+
// VERSION: 0.3.1
66
// PURPOSE: Arduino library to store a symmetrical distance table in less memory
77
// URL: https://github.com/RobTillaart/DistanceTable
88
//
@@ -11,7 +11,7 @@
1111
#include "Arduino.h"
1212

1313

14-
#define DISTANCETABLE_LIB_VERSION (F("0.3.0"))
14+
#define DISTANCETABLE_LIB_VERSION (F("0.3.1"))
1515

1616

1717
class DistanceTable
@@ -22,7 +22,7 @@ class DistanceTable
2222

2323
void clear() { setAll(0); };
2424
void setAll(float value);
25-
void set(uint8_t x, uint8_t y, float value );
25+
bool set(uint8_t x, uint8_t y, float value );
2626
float get(uint8_t x, uint8_t y);
2727

2828

@@ -34,6 +34,8 @@ class DistanceTable
3434
// minimum and maximum skip x == y pairs as these are 0.
3535
float minimum(uint8_t &x, uint8_t &y);
3636
float maximum(uint8_t &x, uint8_t &y);
37+
float sum();
38+
float average();
3739

3840

3941
// epsilon allows 'almost equal' searches
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//
2+
// FILE: distanceTable_pascal.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo of memory efficient distance table class
5+
// DATE: 2015-06-18
6+
// URL: https://github.com/RobTillaart/DistanceTable
7+
//
8+
9+
10+
#include "DistanceTable.h"
11+
12+
13+
// above 27 layout fails
14+
// above 29 allocation fails
15+
#define MAXSIZE 27
16+
17+
DistanceTable dt(MAXSIZE);
18+
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.print("\nDistanceTable: ");
24+
Serial.println(DISTANCETABLE_LIB_VERSION);
25+
Serial.println("Pascals triangle");
26+
27+
dt.clear();
28+
29+
// FILL THE 1's
30+
for (int y = 1; y < MAXSIZE; y++)
31+
{
32+
dt.set(0, y, 1);
33+
}
34+
for (int x = 1; x < MAXSIZE; x++)
35+
{
36+
dt.set(x, x + 1, 1);
37+
}
38+
39+
// ADD EMPTY ELEMENTS PER ROW
40+
for (int y = 0; y < MAXSIZE; y++)
41+
{
42+
for (int x = 0; x < y; x++)
43+
{
44+
if (dt.get(x, y) == 0)
45+
{
46+
float p = dt.get(x - 1, y - 1) + dt.get(x, y - 1);
47+
dt.set(x, y, p);
48+
}
49+
}
50+
}
51+
52+
// PRINT LOWER HALF TRIANGLE
53+
Serial.println();
54+
for (uint8_t i = 0; i < MAXSIZE; i++)
55+
{
56+
for (uint8_t j = 0; j < i; j++)
57+
{
58+
Serial.print(dt.get(i, j), 0);
59+
Serial.print("\t");
60+
}
61+
Serial.println();
62+
}
63+
Serial.println();
64+
}
65+
66+
67+
void loop()
68+
{
69+
}
70+
71+
72+
// -- END OF FILE --

libraries/DistanceTable/examples/distanceTable_test/distanceTable_test.ino

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void setup()
5454
Serial.println(v1);
5555
Serial.println(a);
5656
Serial.println(b);
57+
Serial.println();
5758
delay(100);
5859

5960
start = micros();
@@ -64,8 +65,31 @@ void setup()
6465
Serial.println(v2);
6566
Serial.println(a);
6667
Serial.println(b);
68+
Serial.println();
6769
delay(100);
6870

71+
72+
start = micros();
73+
float v3 = dt.sum();
74+
stop = micros();
75+
Serial.print("sum:\t");
76+
Serial.println(stop - start);
77+
Serial.println(v3);
78+
Serial.println();
79+
delay(100);
80+
81+
82+
start = micros();
83+
float v4 = dt.average();
84+
stop = micros();
85+
Serial.print("average:\t");
86+
Serial.println(stop - start);
87+
Serial.println(v4);
88+
Serial.println();
89+
delay(100);
90+
91+
Serial.println("\n========================================\n");
92+
delay(100);
6993
dt.setInvert(true);
7094

7195
start = micros();
@@ -76,6 +100,7 @@ void setup()
76100
Serial.println(v1);
77101
Serial.println(a);
78102
Serial.println(b);
103+
Serial.println();
79104
delay(100);
80105

81106
start = micros();
@@ -86,6 +111,7 @@ void setup()
86111
Serial.println(v2);
87112
Serial.println(a);
88113
Serial.println(b);
114+
Serial.println();
89115
delay(100);
90116

91117
Serial.println("\n========================================\n");
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
DistanceTable: 0.3.0
3+
DistanceTable test 20x20:
4+
5+
========================================
6+
7+
190
8+
760
9+
10+
========================================
11+
12+
13+
0.00 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14 3.14
14+
3.14 0.00 5.14 6.14 7.14 8.14 9.14 10.14 11.14 12.14 13.14 14.14 15.14 16.14 17.14 18.14 19.14 20.14 21.14 22.14
15+
3.14 5.14 0.00 9.14 11.14 13.14 15.14 17.14 19.14 21.14 23.14 25.14 27.14 29.14 31.14 33.14 35.14 37.14 39.14 41.14
16+
3.14 6.14 9.14 0.00 15.14 18.14 21.14 24.14 27.14 30.14 33.14 36.14 39.14 42.14 45.14 48.14 51.14 54.14 57.14 60.14
17+
3.14 7.14 11.14 15.14 0.00 23.14 27.14 31.14 35.14 39.14 43.14 47.14 51.14 55.14 59.14 63.14 67.14 71.14 75.14 79.14
18+
3.14 8.14 13.14 18.14 23.14 0.00 33.14 38.14 43.14 48.14 53.14 58.14 63.14 68.14 73.14 78.14 83.14 88.14 93.14 98.14
19+
3.14 9.14 15.14 21.14 27.14 33.14 0.00 45.14 51.14 57.14 63.14 69.14 75.14 81.14 87.14 93.14 99.14 105.14 111.14 117.14
20+
3.14 10.14 17.14 24.14 31.14 38.14 45.14 0.00 59.14 66.14 73.14 80.14 87.14 94.14 101.14 108.14 115.14 122.14 129.14 136.14
21+
3.14 11.14 19.14 27.14 35.14 43.14 51.14 59.14 0.00 75.14 83.14 91.14 99.14 107.14 115.14 123.14 131.14 139.14 147.14 155.14
22+
3.14 12.14 21.14 30.14 39.14 48.14 57.14 66.14 75.14 0.00 93.14 102.14 111.14 120.14 129.14 138.14 147.14 156.14 165.14 174.14
23+
3.14 13.14 23.14 33.14 43.14 53.14 63.14 73.14 83.14 93.14 0.00 113.14 123.14 133.14 143.14 153.14 163.14 173.14 183.14 193.14
24+
3.14 14.14 25.14 36.14 47.14 58.14 69.14 80.14 91.14 102.14 113.14 0.00 135.14 146.14 157.14 168.14 179.14 190.14 201.14 212.14
25+
3.14 15.14 27.14 39.14 51.14 63.14 75.14 87.14 99.14 111.14 123.14 135.14 0.00 159.14 171.14 183.14 195.14 207.14 219.14 231.14
26+
3.14 16.14 29.14 42.14 55.14 68.14 81.14 94.14 107.14 120.14 133.14 146.14 159.14 0.00 185.14 198.14 211.14 224.14 237.14 250.14
27+
3.14 17.14 31.14 45.14 59.14 73.14 87.14 101.14 115.14 129.14 143.14 157.14 171.14 185.14 0.00 213.14 227.14 241.14 255.14 269.14
28+
3.14 18.14 33.14 48.14 63.14 78.14 93.14 108.14 123.14 138.14 153.14 168.14 183.14 198.14 213.14 0.00 243.14 258.14 273.14 288.14
29+
3.14 19.14 35.14 51.14 67.14 83.14 99.14 115.14 131.14 147.14 163.14 179.14 195.14 211.14 227.14 243.14 0.00 275.14 291.14 307.14
30+
3.14 20.14 37.14 54.14 71.14 88.14 105.14 122.14 139.14 156.14 173.14 190.14 207.14 224.14 241.14 258.14 275.14 0.00 309.14 326.14
31+
3.14 21.14 39.14 57.14 75.14 93.14 111.14 129.14 147.14 165.14 183.14 201.14 219.14 237.14 255.14 273.14 291.14 309.14 0.00 345.14
32+
3.14 22.14 41.14 60.14 79.14 98.14 117.14 136.14 155.14 174.14 193.14 212.14 231.14 250.14 269.14 288.14 307.14 326.14 345.14 0.00
33+
34+
35+
0.00 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14 -3.14
36+
3.14 0.00 -5.14 -6.14 -7.14 -8.14 -9.14 -10.14 -11.14 -12.14 -13.14 -14.14 -15.14 -16.14 -17.14 -18.14 -19.14 -20.14 -21.14 -22.14
37+
3.14 5.14 0.00 -9.14 -11.14 -13.14 -15.14 -17.14 -19.14 -21.14 -23.14 -25.14 -27.14 -29.14 -31.14 -33.14 -35.14 -37.14 -39.14 -41.14
38+
3.14 6.14 9.14 0.00 -15.14 -18.14 -21.14 -24.14 -27.14 -30.14 -33.14 -36.14 -39.14 -42.14 -45.14 -48.14 -51.14 -54.14 -57.14 -60.14
39+
3.14 7.14 11.14 15.14 0.00 -23.14 -27.14 -31.14 -35.14 -39.14 -43.14 -47.14 -51.14 -55.14 -59.14 -63.14 -67.14 -71.14 -75.14 -79.14
40+
3.14 8.14 13.14 18.14 23.14 0.00 -33.14 -38.14 -43.14 -48.14 -53.14 -58.14 -63.14 -68.14 -73.14 -78.14 -83.14 -88.14 -93.14 -98.14
41+
3.14 9.14 15.14 21.14 27.14 33.14 0.00 -45.14 -51.14 -57.14 -63.14 -69.14 -75.14 -81.14 -87.14 -93.14 -99.14 -105.14 -111.14 -117.14
42+
3.14 10.14 17.14 24.14 31.14 38.14 45.14 0.00 -59.14 -66.14 -73.14 -80.14 -87.14 -94.14 -101.14 -108.14 -115.14 -122.14 -129.14 -136.14
43+
3.14 11.14 19.14 27.14 35.14 43.14 51.14 59.14 0.00 -75.14 -83.14 -91.14 -99.14 -107.14 -115.14 -123.14 -131.14 -139.14 -147.14 -155.14
44+
3.14 12.14 21.14 30.14 39.14 48.14 57.14 66.14 75.14 0.00 -93.14 -102.14 -111.14 -120.14 -129.14 -138.14 -147.14 -156.14 -165.14 -174.14
45+
3.14 13.14 23.14 33.14 43.14 53.14 63.14 73.14 83.14 93.14 0.00 -113.14 -123.14 -133.14 -143.14 -153.14 -163.14 -173.14 -183.14 -193.14
46+
3.14 14.14 25.14 36.14 47.14 58.14 69.14 80.14 91.14 102.14 113.14 0.00 -135.14 -146.14 -157.14 -168.14 -179.14 -190.14 -201.14 -212.14
47+
3.14 15.14 27.14 39.14 51.14 63.14 75.14 87.14 99.14 111.14 123.14 135.14 0.00 -159.14 -171.14 -183.14 -195.14 -207.14 -219.14 -231.14
48+
3.14 16.14 29.14 42.14 55.14 68.14 81.14 94.14 107.14 120.14 133.14 146.14 159.14 0.00 -185.14 -198.14 -211.14 -224.14 -237.14 -250.14
49+
3.14 17.14 31.14 45.14 59.14 73.14 87.14 101.14 115.14 129.14 143.14 157.14 171.14 185.14 0.00 -213.14 -227.14 -241.14 -255.14 -269.14
50+
3.14 18.14 33.14 48.14 63.14 78.14 93.14 108.14 123.14 138.14 153.14 168.14 183.14 198.14 213.14 0.00 -243.14 -258.14 -273.14 -288.14
51+
3.14 19.14 35.14 51.14 67.14 83.14 99.14 115.14 131.14 147.14 163.14 179.14 195.14 211.14 227.14 243.14 0.00 -275.14 -291.14 -307.14
52+
3.14 20.14 37.14 54.14 71.14 88.14 105.14 122.14 139.14 156.14 173.14 190.14 207.14 224.14 241.14 258.14 275.14 0.00 -309.14 -326.14
53+
3.14 21.14 39.14 57.14 75.14 93.14 111.14 129.14 147.14 165.14 183.14 201.14 219.14 237.14 255.14 273.14 291.14 309.14 0.00 -345.14
54+
3.14 22.14 41.14 60.14 79.14 98.14 117.14 136.14 155.14 174.14 193.14 212.14 231.14 250.14 269.14 288.14 307.14 326.14 345.14 0.00
55+
56+
minimum: 1000
57+
1.00
58+
6
59+
5
60+
maximum: 1008
61+
345.14
62+
19
63+
18
64+
minimum: 1716
65+
-345.14
66+
18
67+
19
68+
maximum: 1572
69+
345.14
70+
19
71+
18
72+
73+
========================================
74+
75+
21.14
76+
COUNT: 3
77+
COUNT: 4
78+
COUNT: 0
79+
ABOVE: 286
80+
BELOW: 94

0 commit comments

Comments
 (0)