mirror of
https://github.com/eried/portapack-mayhem.git
synced 2024-10-01 01:26:06 -04:00
added binary search tree lookup to airlines.db
This commit is contained in:
parent
3cb9e1849b
commit
4a0a243530
@ -293,7 +293,7 @@ ADSBRxDetailsView::ADSBRxDetailsView(
|
||||
bool found = false;
|
||||
size_t number_of_airlines = 0;
|
||||
std::string airline_code;
|
||||
size_t c;
|
||||
//size_t c;
|
||||
|
||||
add_children({
|
||||
&labels,
|
||||
@ -320,18 +320,37 @@ ADSBRxDetailsView::ADSBRxDetailsView(
|
||||
// Search for 3-letter code
|
||||
number_of_airlines = (db_file.size() / 68); // determine number of airlines in file
|
||||
airline_code = entry_copy.callsign.substr(0, 3);
|
||||
c = 0;
|
||||
do {
|
||||
db_file.read(file_buffer, 4);
|
||||
if (!file_buffer[0])
|
||||
break;
|
||||
if (!airline_code.compare(0, 4, file_buffer))
|
||||
found = true;
|
||||
else
|
||||
c++;
|
||||
} while (!found && (c < number_of_airlines));
|
||||
//c = 0;
|
||||
//do {
|
||||
// db_file.read(file_buffer, 4);
|
||||
// if (!file_buffer[0])
|
||||
// break;
|
||||
// if (!airline_code.compare(0, 4, file_buffer))
|
||||
// found = true;
|
||||
// else
|
||||
// c++;
|
||||
//} while (!found && (c < number_of_airlines));
|
||||
|
||||
// binary search
|
||||
int first = 0, // First search element
|
||||
last = number_of_airlines - 1, // Last search element
|
||||
middle, // Mid point of search
|
||||
position = -1; // Position of search value
|
||||
while (!found && first <= last) {
|
||||
middle = (first + last) / 2; // Calculate mid point
|
||||
db_file.seek(middle * 4);
|
||||
db_file.read(file_buffer, 3);
|
||||
if (file_buffer == airline_code) { // If value is found at mid
|
||||
found = true;
|
||||
position = middle;
|
||||
}
|
||||
else if (file_buffer > airline_code) // If value is in lower half
|
||||
last = middle - 1;
|
||||
else
|
||||
first = middle + 1; // If value is in upper half
|
||||
}
|
||||
|
||||
if (found) {
|
||||
if (position > -1) {
|
||||
db_file.seek((number_of_airlines * 4) + (c << 6)); // seek starting after index
|
||||
db_file.read(file_buffer, 32);
|
||||
text_airline.set(file_buffer);
|
||||
|
Loading…
Reference in New Issue
Block a user