1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-17 21:10:39 +02:00

Optimise duplicate checking and sorting in FileLister::browse

Using a std::set makes both duplicate checking and sorting run in
O(n log n).
This commit is contained in:
Nebuleon Fumika 2014-08-13 01:54:12 +00:00 committed by Maarten ter Huurne
parent bd7eac32ba
commit f1e92425ac

View File

@ -31,6 +31,7 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <set>
using namespace std; using namespace std;
@ -61,9 +62,11 @@ void FileLister::setShowFiles(bool showFiles)
void FileLister::browse(const string& path, bool clean) void FileLister::browse(const string& path, bool clean)
{ {
if (clean) { set<string, case_less> directorySet;
directories.clear(); set<string, case_less> fileSet;
files.clear(); if (!clean) {
directorySet.insert(directories.begin(), directories.end());
fileSet.insert(files.begin(), files.end());
} }
if (showDirectories || showFiles) { if (showDirectories || showFiles) {
@ -100,19 +103,13 @@ void FileLister::browse(const string& path, bool clean)
if (!showDirectories) if (!showDirectories)
continue; continue;
if (std::find(directories.begin(), directories.end(), file) != directories.end()) directorySet.insert(file);
continue;
directories.push_back(file);
} else { } else {
if (!showFiles) if (!showFiles)
continue; continue;
if (std::find(files.begin(), files.end(), file) != files.end())
continue;
if (filter.empty()) { if (filter.empty()) {
files.push_back(file); fileSet.insert(file);
continue; continue;
} }
@ -121,7 +118,7 @@ void FileLister::browse(const string& path, bool clean)
if (!it->empty()) if (!it->empty())
continue; continue;
files.push_back(file); fileSet.insert(file);
break; break;
} }
@ -138,7 +135,7 @@ void FileLister::browse(const string& path, bool clean)
file_lowercase.begin(), ::tolower); file_lowercase.begin(), ::tolower);
if (file_lowercase.compare(0, it->length(), *it) == 0) { if (file_lowercase.compare(0, it->length(), *it) == 0) {
files.push_back(file); fileSet.insert(file);
break; break;
} }
} }
@ -147,9 +144,10 @@ void FileLister::browse(const string& path, bool clean)
} }
closedir(dirp); closedir(dirp);
sort(files.begin(), files.end(), case_less());
sort(directories.begin(), directories.end(), case_less());
} }
directories = vector<string>(directorySet.begin(), directorySet.end());
files = vector<string>(fileSet.begin(), fileSet.end());
} }
unsigned int FileLister::size() unsigned int FileLister::size()