added a binary search for address and assoc table
can be enabled by defining USE_BINSEARCH
This commit is contained in:
SirSydom 2022-02-19 23:18:39 +01:00
parent 53425e2ef7
commit ef24cdc7de
2 changed files with 62 additions and 1 deletions

View File

@ -36,9 +36,28 @@ uint16_t AddressTableObject::getGroupAddress(uint16_t tsap)
uint16_t AddressTableObject::getTsap(uint16_t addr)
{
uint16_t size = entryCount();
#ifdef USE_BINSEARCH
addr = htons(addr);
uint16_t low,high,i;
low = 0;
high = size-1;
while(low <= high)
{
i = (low+high)/2;
if (_groupAddresses[i] == addr)
return i;
if(addr < _groupAddresses[i])
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,10 +77,28 @@ const uint8_t* AddressTableObject::restore(const uint8_t* buffer)
bool AddressTableObject::contains(uint16_t addr)
{
uint16_t size = entryCount();
#ifdef USE_BINSEARCH
addr = htons(addr);
uint16_t low,high,i;
low = 0;
high = size-1;
while(low <= high)
{
i = (low+high)/2;
if (_groupAddresses[i] == addr)
return true;
if(addr < _groupAddresses[i])
high = i - 1;
else
low = i + 1 ;
}
#else
for (uint16_t i = 1; i <= size; i++)
if (ntohs(_groupAddresses[i]) == addr)
return true;
#endif
return false;
}

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 getASAP(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;
}