mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ prevent overflow; refactor memory usage;
This commit is contained in:
parent
ebc796d4f8
commit
4eb699209f
@ -1,24 +1,30 @@
|
||||
//
|
||||
// FILE: DistanceTable.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.01
|
||||
// VERSION: 0.1.2
|
||||
// PURPOSE: library for a memory efficient DistanceTable for Arduino
|
||||
// URL:
|
||||
//
|
||||
// Released to the public domain
|
||||
//
|
||||
|
||||
// 0.1.00 - initial version
|
||||
// 0.1.2 - prevent overflow; refactor memory usage;
|
||||
// 0.1.01 - refactor
|
||||
// 0.1.00 - initial version
|
||||
|
||||
#include "DistanceTable.h"
|
||||
|
||||
DistanceTable::DistanceTable(uint8_t size)
|
||||
{
|
||||
// MUST: size > 1
|
||||
// ATMEL 328 has 2000 bytes mem,
|
||||
// so roughly 30X30 = 900 floats(4Bytes) => 1740 bytes is max feasible
|
||||
_size = size;
|
||||
uint16_t s = size * (size-1)/2;
|
||||
_distanceTable = (double*) malloc(s * sizeof(double));
|
||||
_store = _size;
|
||||
_store *= (_size-1)/2;
|
||||
_distanceTable = (double *) malloc(_store * sizeof(double));
|
||||
if (_distanceTable == NULL) _size = 0;
|
||||
clear();
|
||||
}
|
||||
|
||||
DistanceTable::~DistanceTable()
|
||||
@ -28,33 +34,28 @@ DistanceTable::~DistanceTable()
|
||||
|
||||
void DistanceTable::clear()
|
||||
{
|
||||
uint16_t s = _size * (_size-1)/2;
|
||||
for (uint16_t index = 0; index < s; index++)
|
||||
{
|
||||
_distanceTable[index] = 0;
|
||||
}
|
||||
for (uint16_t index = 0; index <_store; index++) _distanceTable[index] = 0;
|
||||
};
|
||||
|
||||
void DistanceTable::set(uint8_t x, uint8_t y, double value )
|
||||
{
|
||||
if ( x >= _size || y >= _size) return;
|
||||
if ( x == y ) return;
|
||||
// need swap?
|
||||
if ( x < y )
|
||||
if ( x >= _size || y >= _size) return;
|
||||
if ( x < y )
|
||||
{
|
||||
uint8_t t = x; x = y; y = t;
|
||||
}
|
||||
// prevent overflow by moving to 16 bit
|
||||
uint16_t index = x;
|
||||
index = index * (index-1)/2 + y;
|
||||
_distanceTable[index] = value;
|
||||
};
|
||||
|
||||
double DistanceTable::get(uint8_t x, uint8_t y)
|
||||
double DistanceTable::get (uint8_t x, uint8_t y)
|
||||
{
|
||||
if ( x >= _size || y >= _size) return -1;
|
||||
if ( x == y ) return 0;
|
||||
// need swap?
|
||||
if ( x < y )
|
||||
if ( x >= _size || y >= _size) return -1;
|
||||
if ( x < y )
|
||||
{
|
||||
uint8_t t = x; x = y; y = t;
|
||||
}
|
||||
@ -63,19 +64,19 @@ double DistanceTable::get(uint8_t x, uint8_t y)
|
||||
return _distanceTable[index];
|
||||
};
|
||||
|
||||
|
||||
// triangular dump
|
||||
void DistanceTable::dump()
|
||||
{
|
||||
uint16_t index = 0;
|
||||
for (uint16_t i = 0; i < _size-1; i++)
|
||||
for (uint8_t i = 0; i < _size-1; i++)
|
||||
{
|
||||
for (uint16_t j = 0; j <= i; j++)
|
||||
for (uint8_t j = 0; j <= i; j++)
|
||||
{
|
||||
Serial.print(_distanceTable[index++]);
|
||||
Serial.print("\t");
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
};
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: DistanceTable.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.01
|
||||
// VERSION: 0.1.2
|
||||
// PURPOSE: memory efficient DistanceTable for Arduino
|
||||
// URL:
|
||||
//
|
||||
@ -13,12 +13,12 @@
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define DISTANCETABLE_LIB_VERSION "0.1.01"
|
||||
#define DISTANCETABLE_LIB_VERSION "0.1.2"
|
||||
|
||||
class DistanceTable
|
||||
{
|
||||
public:
|
||||
DistanceTable(uint8_t);
|
||||
explicit DistanceTable(uint8_t);
|
||||
~DistanceTable();
|
||||
|
||||
void clear();
|
||||
@ -27,8 +27,8 @@ public:
|
||||
void dump();
|
||||
|
||||
protected:
|
||||
int _error;
|
||||
uint8_t _size;
|
||||
uint16_t _store;
|
||||
double * _distanceTable;
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=DistanceTable
|
||||
version=0.1.1
|
||||
version=0.1.2
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Library for a memory efficient DistanceTable for Arduino.
|
||||
|
Loading…
x
Reference in New Issue
Block a user