refactor: add WordList type
This commit is contained in:
parent
dae5445efc
commit
48f05ddb4f
18 changed files with 111 additions and 92 deletions
|
@ -8,7 +8,7 @@
|
|||
using std::mutex, std::vector, std::thread, std::lock_guard, std::string,
|
||||
std::forward_list, std::string_view;
|
||||
|
||||
GroupedFinder::GroupedFinder(const std::vector<string> &word_list) {
|
||||
GroupedFinder::GroupedFinder(const WordList &word_list) {
|
||||
for (const auto &word : word_list) {
|
||||
groups_[word.front()].push_back(&word);
|
||||
}
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
|
||||
using std::string, std::forward_list, std::string_view;
|
||||
|
||||
LinearFinder::LinearFinder(const vector<string> &word_list)
|
||||
: word_list_(word_list) {}
|
||||
LinearFinder::LinearFinder(const WordList &word_list) : word_list_(word_list) {}
|
||||
|
||||
forward_list<const string *>
|
||||
LinearFinder::find_prefix(string_view search_term) const {
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
using std::mutex, std::thread, std::lock_guard, std::vector, std::forward_list,
|
||||
std::string, std::string_view;
|
||||
|
||||
ParallelFinder::ParallelFinder(const vector<string> &word_list)
|
||||
ParallelFinder::ParallelFinder(const WordList &word_list)
|
||||
: word_list_(word_list) {}
|
||||
|
||||
forward_list<const string *>
|
||||
|
@ -28,7 +28,7 @@ ParallelFinder::find_prefix(string_view search_term) const {
|
|||
: (thread_index + 1) * (word_list_size / thread_count);
|
||||
|
||||
threads.emplace_back(
|
||||
[](const vector<string> &word_list, const string_view &search_term,
|
||||
[](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;
|
||||
|
|
|
@ -3,9 +3,9 @@
|
|||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
using std::vector, std::forward_list, std::string, std::string_view;
|
||||
using std::forward_list, std::string, std::string_view;
|
||||
|
||||
SortedLinearFinder::SortedLinearFinder(const vector<string> &word_list) {
|
||||
SortedLinearFinder::SortedLinearFinder(const WordList &word_list) {
|
||||
std::transform(word_list.cbegin(), word_list.cend(),
|
||||
std::back_inserter(word_list_),
|
||||
[](const string &word) { return &word; });
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "tree_finder.h"
|
||||
|
||||
using std::string, std::string_view, std::forward_list, std::vector;
|
||||
using std::string, std::string_view, std::forward_list;
|
||||
|
||||
void SearchTreeNode::insert(string_view partial_word,
|
||||
const string *original_word) {
|
||||
|
@ -35,14 +35,13 @@ forward_list<const string *> SearchTreeNode::words() const {
|
|||
return results;
|
||||
};
|
||||
|
||||
SearchTree::SearchTree(const vector<string> &word_list) {
|
||||
SearchTree::SearchTree(const WordList &word_list) {
|
||||
for (const auto &word : word_list) {
|
||||
insert(string_view(word), &word);
|
||||
}
|
||||
}
|
||||
|
||||
TreeFinder::TreeFinder(const vector<string> &word_list)
|
||||
: search_tree_(word_list) {}
|
||||
TreeFinder::TreeFinder(const WordList &word_list) : search_tree_(word_list) {}
|
||||
|
||||
forward_list<const string *>
|
||||
TreeFinder::find_prefix(string_view search_term) const {
|
||||
|
|
59
lib_vector_search/src/word_list.cpp
Normal file
59
lib_vector_search/src/word_list.cpp
Normal file
|
@ -0,0 +1,59 @@
|
|||
#include "word_list.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
|
||||
WordList &WordList::multiply(size_t factor) {
|
||||
WordList result;
|
||||
result.reserve(size() * factor);
|
||||
|
||||
for (auto time = 0; time < factor; ++time) {
|
||||
std::copy(cbegin(), cend(), std::back_inserter(result));
|
||||
}
|
||||
|
||||
std::swap(*this, result);
|
||||
|
||||
return *this;
|
||||
};
|
||||
|
||||
WordList &WordList::shuffle() {
|
||||
std::random_device random_device;
|
||||
std::mt19937 random_number_generator(random_device());
|
||||
std::shuffle(begin(), end(), random_number_generator);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
WordList WordList::fourCaps() {
|
||||
const static std::string charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
WordList word_list;
|
||||
word_list.reserve(std::pow(charset_.length(), 4));
|
||||
|
||||
for (auto char_1 : charset_) {
|
||||
for (auto char_2 : charset_) {
|
||||
for (auto char_3 : charset_) {
|
||||
for (auto char_4 : charset_) {
|
||||
word_list.emplace_back(
|
||||
std::initializer_list<char>({char_1, char_2, char_3, char_4}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return word_list;
|
||||
};
|
||||
|
||||
WordList WordList::fromFile(const std::filesystem::path &path) {
|
||||
WordList word_list;
|
||||
|
||||
std::ifstream ifs(path);
|
||||
|
||||
std::string line;
|
||||
while (std::getline(ifs, line)) {
|
||||
word_list.emplace_back(line);
|
||||
}
|
||||
|
||||
return word_list;
|
||||
}
|
|
@ -1,32 +0,0 @@
|
|||
#include "word_list_generator.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
|
||||
const std::string WordListGenerator::charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
std::vector<std::string> WordListGenerator::generate(const size_t multiplier) {
|
||||
std::vector<std::string> word_list;
|
||||
word_list.reserve(multiplier * std::pow(charset_.length(), 4));
|
||||
|
||||
for (auto char_1 : charset_) {
|
||||
for (auto char_2 : charset_) {
|
||||
for (auto char_3 : charset_) {
|
||||
for (auto char_4 : charset_) {
|
||||
for (auto i = 0; i < multiplier; ++i) {
|
||||
word_list.emplace_back(
|
||||
std::initializer_list<char>({char_1, char_2, char_3, char_4}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::random_device random_device;
|
||||
std::mt19937 random_number_generator(random_device());
|
||||
|
||||
std::shuffle(word_list.begin(), word_list.end(), random_number_generator);
|
||||
|
||||
return word_list;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue