refactor: move searching to static library
This commit is contained in:
parent
8ed4b9ac71
commit
32a1cd7533
11 changed files with 20 additions and 2 deletions
17
lib_vector_search/src/linear_finder.cpp
Normal file
17
lib_vector_search/src/linear_finder.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "linear_finder.h"
|
||||
|
||||
LinearFinder::LinearFinder(const vector<string> &word_list)
|
||||
: word_list_(word_list) {}
|
||||
|
||||
vector<const string *>
|
||||
LinearFinder::find_prefix(string_view search_term) const {
|
||||
vector<const string *> matching_words;
|
||||
|
||||
for (const auto ¤t_word : word_list_) {
|
||||
if (current_word.starts_with(search_term)) {
|
||||
matching_words.push_back(¤t_word);
|
||||
}
|
||||
}
|
||||
|
||||
return matching_words;
|
||||
}
|
47
lib_vector_search/src/parallel_finder.cpp
Normal file
47
lib_vector_search/src/parallel_finder.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "parallel_finder.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
using std::mutex, std::thread, std::lock_guard;
|
||||
|
||||
ParallelFinder::ParallelFinder(const vector<string> &word_list,
|
||||
size_t thread_count)
|
||||
: word_list_(word_list), thread_count_(thread_count) {}
|
||||
|
||||
vector<const string *>
|
||||
ParallelFinder::find_prefix(string_view search_term) const {
|
||||
vector<const string *> result;
|
||||
mutex result_mutex;
|
||||
|
||||
vector<thread> 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 =
|
||||
(thread_index == thread_count_ - 1)
|
||||
? word_list_.size()
|
||||
: (thread_index + 1) * (word_list_.size() / thread_count_);
|
||||
|
||||
threads.emplace_back(
|
||||
[](const vector<string> &word_list, const string_view &search_term,
|
||||
vector<const string *> &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<mutex> lock(result_mutex);
|
||||
result.push_back(¤t_word);
|
||||
}
|
||||
}
|
||||
},
|
||||
cref(word_list_), cref(search_term), ref(result), first_word_index,
|
||||
last_word_index, ref(result_mutex));
|
||||
}
|
||||
|
||||
for (auto &thread : threads) {
|
||||
thread.join();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
17
lib_vector_search/src/timer.cpp
Normal file
17
lib_vector_search/src/timer.cpp
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "timer.h"
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
Timer::Timer(std::string_view name) : name_(name) { start(); };
|
||||
|
||||
void Timer::start() { start_ = std::chrono::high_resolution_clock::now(); }
|
||||
|
||||
void Timer::stop() {
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto duration =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(end - start_);
|
||||
|
||||
std::cout << name_ << " took " << duration << std::endl;
|
||||
}
|
34
lib_vector_search/src/word_list_generator.cpp
Normal file
34
lib_vector_search/src/word_list_generator.cpp
Normal file
|
@ -0,0 +1,34 @@
|
|||
#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 = 10;
|
||||
|
||||
std::vector<std::string> result;
|
||||
result.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) {
|
||||
result.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(result.begin(), result.end(), random_number_generator);
|
||||
|
||||
return result;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue