diff --git a/QVectorSearch/mainwindow.cpp b/QVectorSearch/mainwindow.cpp index a274c71..545b0a2 100644 --- a/QVectorSearch/mainwindow.cpp +++ b/QVectorSearch/mainwindow.cpp @@ -7,8 +7,8 @@ #include "timer.h" #include "word_list_generator.h" -#include #include +#include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { @@ -51,8 +51,8 @@ void MainWindow::search(const QString &search_term) { timer.stop(); std::stringstream status_message; - status_message << "search took " << timer << ", found " << results.size() - << " results"; + status_message << "search took " << timer << ", found " + << std::distance(results.begin(), results.end()) << " results"; ui->mainStatusBar->showMessage(QString::fromStdString(status_message.str())); showResults(results); @@ -71,7 +71,8 @@ std::unique_ptr MainWindow::createSelectedFinder() const { } } -void MainWindow::showResults(const std::vector &results) { +void MainWindow::showResults( + const std::forward_list &results) { QStringList ui_results; for (auto word : results) { ui_results.append(QString::fromStdString(*word)); diff --git a/QVectorSearch/mainwindow.h b/QVectorSearch/mainwindow.h index 15ea3f8..2a2f80e 100644 --- a/QVectorSearch/mainwindow.h +++ b/QVectorSearch/mainwindow.h @@ -29,7 +29,7 @@ private: void generateWordList(); void search(const QString &search_term); std::unique_ptr createSelectedFinder() const; - void showResults(const std::vector &results); + void showResults(const std::forward_list &results); private slots: void on_searchInput_textChanged(const QString &search_term); diff --git a/lib_vector_search/include/finder.h b/lib_vector_search/include/finder.h index 9543adf..1266392 100644 --- a/lib_vector_search/include/finder.h +++ b/lib_vector_search/include/finder.h @@ -1,12 +1,14 @@ #pragma once +#include #include -#include -using std::string, std::string_view, std::vector; +using std::string, std::string_view, std::forward_list; class Finder { public: virtual ~Finder() = default; - virtual vector find_prefix(string_view search_term) const = 0; + + virtual forward_list + find_prefix(string_view search_term) const = 0; }; diff --git a/lib_vector_search/include/linear_finder.h b/lib_vector_search/include/linear_finder.h index e9f3419..646c248 100644 --- a/lib_vector_search/include/linear_finder.h +++ b/lib_vector_search/include/linear_finder.h @@ -2,11 +2,16 @@ #include "finder.h" +#include + +using std::vector; + class LinearFinder : public Finder { private: const vector &word_list_; public: LinearFinder(const vector &word_list); - vector find_prefix(string_view search_term) const override; + forward_list + find_prefix(string_view search_term) const override; }; diff --git a/lib_vector_search/include/parallel_finder.h b/lib_vector_search/include/parallel_finder.h index 556a167..7226e0c 100644 --- a/lib_vector_search/include/parallel_finder.h +++ b/lib_vector_search/include/parallel_finder.h @@ -2,12 +2,16 @@ #include "finder.h" +#include + class ParallelFinder : public Finder { private: const size_t thread_count_; - const vector &word_list_; + const std::vector &word_list_; public: - ParallelFinder(const vector &word_list, size_t thread_count); - vector find_prefix(string_view search_term) const override; + ParallelFinder(const std::vector &word_list, size_t thread_count); + + std::forward_list + find_prefix(std::string_view search_term) const override; }; diff --git a/lib_vector_search/src/linear_finder.cpp b/lib_vector_search/src/linear_finder.cpp index f6c76c1..0c4705e 100644 --- a/lib_vector_search/src/linear_finder.cpp +++ b/lib_vector_search/src/linear_finder.cpp @@ -3,13 +3,13 @@ LinearFinder::LinearFinder(const vector &word_list) : word_list_(word_list) {} -vector +forward_list LinearFinder::find_prefix(string_view search_term) const { - vector matching_words; + forward_list matching_words; for (const auto ¤t_word : word_list_) { if (current_word.starts_with(search_term)) { - matching_words.push_back(¤t_word); + matching_words.push_front(¤t_word); } } diff --git a/lib_vector_search/src/parallel_finder.cpp b/lib_vector_search/src/parallel_finder.cpp index d83b97a..5bfbbaa 100644 --- a/lib_vector_search/src/parallel_finder.cpp +++ b/lib_vector_search/src/parallel_finder.cpp @@ -3,15 +3,15 @@ #include #include -using std::mutex, std::thread, std::lock_guard; +using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list; ParallelFinder::ParallelFinder(const vector &word_list, size_t thread_count) : word_list_(word_list), thread_count_(thread_count) {} -vector +forward_list ParallelFinder::find_prefix(string_view search_term) const { - vector result; + forward_list result; mutex result_mutex; vector threads; @@ -25,13 +25,13 @@ ParallelFinder::find_prefix(string_view search_term) const { threads.emplace_back( [](const vector &word_list, const string_view &search_term, - vector &result, size_t start_index, size_t end_index, - mutex &result_mutex) { + forward_list &result, size_t start_index, + size_t end_index, mutex &result_mutex) { for (size_t index = start_index; index < end_index; ++index) { const auto ¤t_word = word_list[index]; if (current_word.starts_with(search_term)) { const lock_guard lock(result_mutex); - result.push_back(¤t_word); + result.push_front(¤t_word); } } }, diff --git a/vector_search_cli/main.cpp b/vector_search_cli/main.cpp index ace12f7..dd7682e 100644 --- a/vector_search_cli/main.cpp +++ b/vector_search_cli/main.cpp @@ -35,7 +35,8 @@ void test_linear_finder(const vector &word_list) { find_timer.stop(); cout << "linear finder took " << find_timer << endl; - cout << "result list is " << result.size() << " element(s) long" << endl; + cout << "result list is " << std::distance(result.cbegin(), result.cend()) + << " element(s) long" << endl; } void test_parallel_finder(const vector &word_list) { @@ -54,7 +55,8 @@ void test_parallel_finder(const vector &word_list) { find_timer.stop(); cout << "parallel finder took " << find_timer << endl; - cout << "result list is " << result.size() << " element(s) long" << endl; + cout << "result list is " << std::distance(result.cbegin(), result.cend()) + << " element(s) long" << endl; } int main(int argc, char *argv[]) {