mirror of
https://github.com/tumic0/GPXSee.git
synced 2024-11-28 05:34:47 +01:00
A little bit more sane binary search
This commit is contained in:
parent
cb8c19a2bc
commit
14a9c0a8cb
@ -1,4 +1,4 @@
|
||||
#include "bitstream.h"
|
||||
#include "bitstream.h"
|
||||
#include "nodfile.h"
|
||||
|
||||
|
||||
@ -257,7 +257,7 @@ bool NODFile::nodeBlock(Handle &hdl, quint32 nodeOffset,
|
||||
quint32 offsetSize = (_indexFlags & 3) + 1;
|
||||
|
||||
while (low <= high) {
|
||||
quint32 m = ((low + high) / 2);
|
||||
int m = ((low + high) / 2);
|
||||
quint32 offset = _indexRecordSize * m + _indexOffset;
|
||||
quint32 blockOffset, prevBlockOffset;
|
||||
|
||||
@ -507,40 +507,32 @@ bool NODFile::linkType(Handle &hdl, const BlockInfo &blockInfo, quint8 linkId,
|
||||
quint32 offset = ((blockInfo.hdr.s10 * blockInfo.hdr.se + 7) >> 3) + 0x13
|
||||
+ blockInfo.offset + _blockOffset + ((blockInfo.hdr.s0 >> 0xb) & 1)
|
||||
+ blockInfo.hdr.s11 * 3;
|
||||
quint32 low = 0;
|
||||
quint32 high = blockInfo.hdr.s12 - 1;
|
||||
quint32 pos = blockInfo.hdr.s12;
|
||||
int low = 0;
|
||||
int high = blockInfo.hdr.s12 - 1;
|
||||
quint16 val;
|
||||
|
||||
if (high > 1) {
|
||||
do {
|
||||
pos = (low + high) / 2;
|
||||
while (low <= high) {
|
||||
int m = ((low + high) / 2);
|
||||
|
||||
if (!(seek(hdl, offset + _blockRecordSize * pos)
|
||||
&& readUInt16(hdl, val)))
|
||||
if (!(seek(hdl, offset + _blockRecordSize * m) && readUInt16(hdl, val)))
|
||||
return false;
|
||||
|
||||
if ((val >> 8) <= linkId)
|
||||
low = pos;
|
||||
else
|
||||
high = pos;
|
||||
} while (low + 1 < high);
|
||||
if ((val >> 8) < linkId)
|
||||
low = m + 1;
|
||||
else if ((val >> 8) > linkId)
|
||||
high = m - 1;
|
||||
else {
|
||||
type = (val & 0x3f) << 8;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(seek(hdl, offset + _blockRecordSize * low) && readUInt16(hdl, val)))
|
||||
if (high < 0)
|
||||
return false;
|
||||
|
||||
type = val & 0x3f;
|
||||
|
||||
if ((low < high) && (pos != high)) {
|
||||
if (!(seek(hdl, offset + _blockRecordSize * high)
|
||||
if (blockInfo.hdr.s12 > 1 && !(seek(hdl, offset + _blockRecordSize * high)
|
||||
&& readUInt16(hdl, val)))
|
||||
return false;
|
||||
if ((val >> 8) <= linkId)
|
||||
type = (val & 0x3f);
|
||||
}
|
||||
|
||||
type <<= 8;
|
||||
type = (val & 0x3f) << 8;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user