From feb49e2092b87ba8d89b66f7ac3bc50030fa56a8 Mon Sep 17 00:00:00 2001 From: Christoph Klaffl Date: Sat, 20 Oct 2018 19:21:44 +0200 Subject: [PATCH] optimized folder file list building by inserting the file names sorted --- src/window.cpp | 36 +++++++++++++++++++++++++++--------- src/window.h | 1 + 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/window.cpp b/src/window.cpp index 6abc89fc..3ed56e8e 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -350,6 +350,29 @@ void Window::dropEvent(QDropEvent *event) load_stl(event->mimeData()->urls().front().toLocalFile()); } +void Window::sorted_insert(QStringList& list, const QCollator& collator, const QString& value) +{ + int start = 0; + int end = list.size() - 1; + int index = 0; + while (start <= end){ + int mid = (start+end)/2; + if (list[mid] == value) { + return; + } + int compare = collator.compare(value, list[mid]); + if (compare < 0) { + end = mid-1; + index = mid; + } else { + start = mid+1; + index = start; + } + } + + list.insert(index, value); +} + void Window::build_folder_file_list() { QString current_folder_path = QFileInfo(current_file).absoluteDir().absolutePath(); @@ -363,21 +386,16 @@ void Window::build_folder_file_list() } lookup_folder = current_folder_path; + QCollator collator; + collator.setNumericMode(true); + QDirIterator dirIterator(lookup_folder, QStringList() << "*.stl", QDir::Files | QDir::Readable | QDir::Hidden); while (dirIterator.hasNext()) { dirIterator.next(); QString name = dirIterator.fileName(); - lookup_folder_files.append(name); + sorted_insert(lookup_folder_files, collator, name); } - - QCollator collator; - collator.setNumericMode(true); - - std::sort( - lookup_folder_files.begin(), - lookup_folder_files.end(), - collator); } QPair Window::get_file_neighbors() diff --git a/src/window.h b/src/window.h index e266d609..715eaed0 100644 --- a/src/window.h +++ b/src/window.h @@ -47,6 +47,7 @@ private slots: private: void rebuild_recent_files(); + void sorted_insert(QStringList& list, const QCollator& collator, const QString& value); void build_folder_file_list(); QPair get_file_neighbors();