Compare commits
4 Commits
48f05ddb4f
...
4b42f4c12a
Author | SHA1 | Date |
---|---|---|
mandlm | 4b42f4c12a | |
mandlm | 029196237d | |
mandlm | 3b1446f049 | |
mandlm | 95cc7223e8 |
|
@ -4,15 +4,14 @@
|
|||
#include "word_list.h"
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
class GroupedFinder : public Finder {
|
||||
private:
|
||||
std::map<char, std::vector<const std::string *>> groups_;
|
||||
std::map<char, WordRefList> groups_;
|
||||
|
||||
public:
|
||||
GroupedFinder(const WordList &word_list);
|
||||
|
||||
virtual std::forward_list<const std::string *>
|
||||
find_prefix(std::string_view search_term) const override;
|
||||
find_prefix(std::string_view search_prefix) const override;
|
||||
};
|
||||
|
|
|
@ -11,5 +11,5 @@ public:
|
|||
ParallelFinder(const WordList &word_list);
|
||||
|
||||
std::forward_list<const std::string *>
|
||||
find_prefix(std::string_view search_term) const override;
|
||||
find_prefix(std::string_view search_prefix) const override;
|
||||
};
|
||||
|
|
|
@ -3,11 +3,9 @@
|
|||
#include "finder.h"
|
||||
#include "word_list.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class SortedLinearFinder : public Finder {
|
||||
private:
|
||||
std::vector<const std::string *> word_list_;
|
||||
WordRefList word_list_;
|
||||
|
||||
public:
|
||||
SortedLinearFinder(const WordList &word_list);
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <forward_list>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
@ -11,4 +13,19 @@ public:
|
|||
|
||||
static WordList fourCaps();
|
||||
static WordList fromFile(const std::filesystem::path &path);
|
||||
|
||||
static void find_prefix_in_range(
|
||||
const WordList &word_list, const std::string_view &search_prefix,
|
||||
size_t start_index, size_t end_index,
|
||||
std::forward_list<const std::string *> &result, std::mutex &result_mutex);
|
||||
};
|
||||
|
||||
class WordRefList : public std::vector<const std::string *> {
|
||||
public:
|
||||
WordRefList(const WordList &source);
|
||||
|
||||
static void find_prefix_in_range(
|
||||
const WordRefList &word_list, const std::string_view &search_prefix,
|
||||
size_t start_index, size_t end_index,
|
||||
std::forward_list<const std::string *> &result, std::mutex &result_mutex);
|
||||
};
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using std::mutex, std::vector, std::thread, std::lock_guard, std::string,
|
||||
std::forward_list, std::string_view;
|
||||
using std::mutex, std::vector, std::thread, std::string, std::forward_list,
|
||||
std::string_view;
|
||||
|
||||
GroupedFinder::GroupedFinder(const WordList &word_list) {
|
||||
for (const auto &word : word_list) {
|
||||
|
@ -14,9 +14,9 @@ GroupedFinder::GroupedFinder(const WordList &word_list) {
|
|||
}
|
||||
}
|
||||
|
||||
std::forward_list<const std::string *>
|
||||
GroupedFinder::find_prefix(std::string_view search_term) const {
|
||||
const auto group = groups_.find(search_term.front());
|
||||
forward_list<const string *>
|
||||
GroupedFinder::find_prefix(string_view search_prefix) const {
|
||||
const auto group = groups_.find(search_prefix.front());
|
||||
if (group == groups_.cend()) {
|
||||
return {};
|
||||
}
|
||||
|
@ -27,41 +27,26 @@ GroupedFinder::find_prefix(std::string_view search_term) const {
|
|||
const auto thread_count =
|
||||
std::min<size_t>(std::thread::hardware_concurrency(), word_list_size);
|
||||
|
||||
forward_list<const string *> matching_words;
|
||||
mutex matching_words_mutex;
|
||||
forward_list<const string *> result;
|
||||
mutex result_mutex;
|
||||
|
||||
vector<thread> search_threads;
|
||||
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
|
||||
const size_t first_word_index =
|
||||
thread_index * (word_list_size / thread_count);
|
||||
const size_t last_word_index =
|
||||
const size_t first_index = thread_index * (word_list_size / thread_count);
|
||||
|
||||
const size_t last_index =
|
||||
(thread_index == thread_count - 1)
|
||||
? word_list_size
|
||||
: (thread_index + 1) * (word_list_size / thread_count);
|
||||
|
||||
search_threads.emplace_back(
|
||||
[](const vector<const string *> &word_list,
|
||||
const string_view &search_term, forward_list<const string *> &result,
|
||||
size_t start_index, size_t end_index, mutex &result_mutex) {
|
||||
forward_list<const string *> thread_results;
|
||||
for (size_t index = start_index; index < end_index; ++index) {
|
||||
const auto ¤t_word = word_list[index];
|
||||
if (current_word->starts_with(search_term)) {
|
||||
thread_results.push_front(current_word);
|
||||
}
|
||||
}
|
||||
if (!thread_results.empty()) {
|
||||
const lock_guard<mutex> lock(result_mutex);
|
||||
result.merge(thread_results);
|
||||
}
|
||||
},
|
||||
cref(word_list), cref(search_term), ref(matching_words),
|
||||
first_word_index, last_word_index, ref(matching_words_mutex));
|
||||
WordRefList::find_prefix_in_range, cref(word_list), cref(search_prefix),
|
||||
first_index, last_index, ref(result), ref(result_mutex));
|
||||
}
|
||||
|
||||
for (auto &thread : search_threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
return matching_words;
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -3,51 +3,36 @@
|
|||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list,
|
||||
std::string, std::string_view;
|
||||
using std::mutex, std::thread, std::vector, std::forward_list, std::string,
|
||||
std::string_view;
|
||||
|
||||
ParallelFinder::ParallelFinder(const WordList &word_list)
|
||||
: word_list_(word_list) {}
|
||||
|
||||
forward_list<const string *>
|
||||
ParallelFinder::find_prefix(string_view search_term) const {
|
||||
ParallelFinder::find_prefix(string_view search_prefix) const {
|
||||
forward_list<const string *> result;
|
||||
mutex result_mutex;
|
||||
|
||||
const auto word_list_size = word_list_.size();
|
||||
const size_t word_list_size = word_list_.size();
|
||||
const size_t thread_count =
|
||||
std::min<size_t>(thread::hardware_concurrency(), word_list_size);
|
||||
|
||||
const size_t thread_count = thread::hardware_concurrency();
|
||||
|
||||
vector<thread> threads;
|
||||
vector<thread> search_threads;
|
||||
for (size_t thread_index = 0; thread_index < thread_count; ++thread_index) {
|
||||
const size_t first_word_index =
|
||||
thread_index * (word_list_size / thread_count);
|
||||
const size_t last_word_index =
|
||||
const size_t first_index = thread_index * (word_list_size / thread_count);
|
||||
|
||||
const size_t last_index =
|
||||
(thread_index == thread_count - 1)
|
||||
? word_list_size
|
||||
: (thread_index + 1) * (word_list_size / thread_count);
|
||||
|
||||
threads.emplace_back(
|
||||
[](const WordList &word_list, const string_view &search_term,
|
||||
forward_list<const string *> &result, size_t start_index,
|
||||
size_t end_index, mutex &result_mutex) {
|
||||
forward_list<const string *> thread_results;
|
||||
for (size_t index = start_index; index < end_index; ++index) {
|
||||
const auto ¤t_word = word_list[index];
|
||||
if (current_word.starts_with(search_term)) {
|
||||
thread_results.push_front(¤t_word);
|
||||
}
|
||||
}
|
||||
if (!thread_results.empty()) {
|
||||
const lock_guard<mutex> lock(result_mutex);
|
||||
result.merge(thread_results);
|
||||
}
|
||||
},
|
||||
cref(word_list_), cref(search_term), ref(result), first_word_index,
|
||||
last_word_index, ref(result_mutex));
|
||||
search_threads.emplace_back(
|
||||
WordList::find_prefix_in_range, cref(word_list_), cref(search_prefix),
|
||||
first_index, last_index, ref(result), ref(result_mutex));
|
||||
}
|
||||
|
||||
for (auto &thread : threads) {
|
||||
for (auto &thread : search_threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
#include "sorted_linear_finder.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
using std::forward_list, std::string, std::string_view;
|
||||
|
||||
SortedLinearFinder::SortedLinearFinder(const WordList &word_list) {
|
||||
std::transform(word_list.cbegin(), word_list.cend(),
|
||||
std::back_inserter(word_list_),
|
||||
[](const string &word) { return &word; });
|
||||
|
||||
SortedLinearFinder::SortedLinearFinder(const WordList &word_list)
|
||||
: word_list_(word_list) {
|
||||
std::sort(
|
||||
word_list_.begin(), word_list_.end(),
|
||||
[](const string *left, const string *right) { return *left < *right; });
|
||||
}
|
||||
|
||||
forward_list<const string *>
|
||||
SortedLinearFinder::find_prefix(string_view search_term) const {
|
||||
forward_list<const string *> matching_words;
|
||||
|
|
|
@ -57,3 +57,47 @@ WordList WordList::fromFile(const std::filesystem::path &path) {
|
|||
|
||||
return word_list;
|
||||
}
|
||||
|
||||
void WordList::find_prefix_in_range(
|
||||
const WordList &word_list, const std::string_view &search_prefix,
|
||||
size_t start_index, size_t end_index,
|
||||
std::forward_list<const std::string *> &result, std::mutex &result_mutex) {
|
||||
std::forward_list<const std::string *> local_results;
|
||||
|
||||
for (size_t index = start_index; index < end_index; ++index) {
|
||||
const auto ¤t_word = word_list[index];
|
||||
if (current_word.starts_with(search_prefix)) {
|
||||
local_results.push_front(¤t_word);
|
||||
}
|
||||
}
|
||||
|
||||
if (!local_results.empty()) {
|
||||
const std::lock_guard<std::mutex> lock(result_mutex);
|
||||
result.merge(local_results);
|
||||
}
|
||||
};
|
||||
|
||||
WordRefList::WordRefList(const WordList &source) {
|
||||
for (const auto &word : source) {
|
||||
push_back(&word);
|
||||
}
|
||||
}
|
||||
|
||||
void WordRefList::find_prefix_in_range(
|
||||
const WordRefList &word_list, const std::string_view &search_prefix,
|
||||
size_t start_index, size_t end_index,
|
||||
std::forward_list<const std::string *> &result, std::mutex &result_mutex) {
|
||||
std::forward_list<const std::string *> local_results;
|
||||
|
||||
for (size_t index = start_index; index < end_index; ++index) {
|
||||
const auto *current_word = word_list[index];
|
||||
if (current_word->starts_with(search_prefix)) {
|
||||
local_results.push_front(current_word);
|
||||
}
|
||||
}
|
||||
|
||||
if (!local_results.empty()) {
|
||||
const std::lock_guard<std::mutex> lock(result_mutex);
|
||||
result.merge(local_results);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -30,9 +30,8 @@ void test_finder_search(Finder &finder, string_view name,
|
|||
auto result = finder.find_prefix(search_term);
|
||||
find_timer.stop();
|
||||
|
||||
cout << name << "(" << search_term << ") took " << find_timer << endl;
|
||||
cout << "result list is " << std::distance(result.cbegin(), result.cend())
|
||||
<< " element(s) long" << endl;
|
||||
cout << name << "(" << search_term << ") took " << find_timer << " for "
|
||||
<< std::distance(result.cbegin(), result.cend()) << " results" << endl;
|
||||
}
|
||||
|
||||
template <typename FINDER>
|
||||
|
@ -44,7 +43,8 @@ void test_finder(const WordList &word_list, string_view finder_name) {
|
|||
constructor_timer.stop();
|
||||
cout << finder_name << " constructor took " << constructor_timer << endl;
|
||||
|
||||
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
|
||||
for (const auto &search_term :
|
||||
{"A", "Z", "AB", "ZY", "ABC", "ZYX", "ABCD", "ZYXW"}) {
|
||||
test_finder_search(finder, finder_name, search_term);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue