From 4a0a243530fe1701887b4b10036802743dffe472 Mon Sep 17 00:00:00 2001 From: Arjan Onwezen Date: Sun, 12 Dec 2021 04:48:11 -0500 Subject: [PATCH] added binary search tree lookup to airlines.db --- firmware/application/apps/ui_adsb_rx.cpp | 43 +++++++++++++++++------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/firmware/application/apps/ui_adsb_rx.cpp b/firmware/application/apps/ui_adsb_rx.cpp index d065163a..d192f58a 100644 --- a/firmware/application/apps/ui_adsb_rx.cpp +++ b/firmware/application/apps/ui_adsb_rx.cpp @@ -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);