From 9ca019ef513cdaaea64a283dd8f12c9c57787627 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 22 Apr 2015 19:42:47 +0200 Subject: [PATCH] Keep list of section names sorted at all times Keeping it sorted when sections are inserted after startup improves the user experience and if we have code for that already, we might as well use it during startup too. --- src/menu.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/menu.cpp b/src/menu.cpp index d4e36c5..3bdb93d 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -78,7 +78,6 @@ Menu::Menu(GMenu2X& gmenu2x, Touchscreen &ts) readSections(GMENU2X_SYSTEM_DIR "/sections"); readSections(GMenu2X::getHome() + "/sections"); - sort(sections.begin(),sections.end(),case_less()); setSectionIndex(0); readLinks(); @@ -118,13 +117,9 @@ void Menu::readSections(std::string parentDir) if (!dirp) return; while ((dptr = readdir(dirp))) { - if (dptr->d_name[0] == '.' || dptr->d_type != DT_DIR) - continue; - - if (find(sections.begin(), sections.end(), dptr->d_name) == sections.end()) { - sections.emplace_back(dptr->d_name); - vector ll; - links.push_back(ll); + if (dptr->d_name[0] != '.' && dptr->d_type == DT_DIR) { + // Create section if it doesn't exist yet. + sectionNamed(dptr->d_name); } } @@ -516,11 +511,11 @@ bool Menu::addLink(string path, string file, string sectionName) { int Menu::sectionNamed(const char *sectionName) { - auto it = find(sections.begin(), sections.end(), sectionName); + auto it = lower_bound(sections.begin(), sections.end(), sectionName); int idx = it - sections.begin(); - if (it == sections.end()) { - sections.emplace_back(sectionName); - links.emplace_back(); + if (it == sections.end() || *it != sectionName) { + sections.emplace(it, sectionName); + links.emplace(links.begin() + idx); } return idx; }