Feature175 binarysearch (#218)

* #175
added a binary search for address and assoc table
can be enabled by defining USE_BINSEARCH

* Corrected BIN_SEARCH

* AddressTableObject::contains minimal improve

* Review: changed ntohs to htons

* corrected byte order during access to sorted array

Co-authored-by: SirSydom <com@sirsydom.de>
Co-authored-by: Waldemar Porscha <wp@porscha.eu>
This commit is contained in:
mumpf 2022-10-21 10:03:11 +02:00 committed by GitHub
parent a0134e6cfb
commit 7bf3696a3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 6 deletions

View File

@ -37,9 +37,28 @@ uint16_t AddressTableObject::getGroupAddress(uint16_t tsap)
uint16_t AddressTableObject::getTsap(uint16_t addr)
{
uint16_t size = entryCount();
#ifdef USE_BINSEARCH
uint16_t low,high,i;
low = 1;
high = size;
while(low <= high)
{
i = (low+high)/2;
uint16_t ga = ntohs(_groupAddresses[i]);
if (ga == addr)
return i;
if(addr < ga)
high = i - 1;
else
low = i + 1 ;
}
#else
for (uint16_t i = 1; i <= size; i++)
if (ntohs(_groupAddresses[i]) == addr)
return i;
#endif
return 0;
}
@ -58,12 +77,7 @@ const uint8_t* AddressTableObject::restore(const uint8_t* buffer)
bool AddressTableObject::contains(uint16_t addr)
{
uint16_t size = entryCount();
for (uint16_t i = 1; i <= size; i++)
if (ntohs(_groupAddresses[i]) == addr)
return true;
return false;
return (getTsap(addr) > 0);
}
void AddressTableObject::beforeStateChange(LoadState& newState)

View File

@ -50,11 +50,35 @@ const uint8_t* AssociationTableObject::restore(const uint8_t* buffer)
int32_t AssociationTableObject::translateAsap(uint16_t asap)
{
uint16_t entries = entryCount();
#ifdef USE_BINSEARCH
uint16_t low,high,i;
low = 0;
high = entries-1;
while(low <= high)
{
i = (low+high)/2;
uint16_t asap_i = getASAP(i);
if (asap_i == asap)
{
// as the binary search does not hit the first element in a list with identical items,
// search downwards to return the first occurence in the table
while(getASAP(--i) == asap)
;
return getTSAP(i+1);
}
if(asap_i > asap)
high = i - 1;
else
low = i + 1 ;
}
#else
for (uint16_t i = 0; i < entries; i++)
{
if (getASAP(i) == asap)
return getTSAP(i);
}
#endif
return -1;
}