Compare commits
2 Commits
4b42f4c12a
...
70200735d9
Author | SHA1 | Date |
---|---|---|
mandlm | 70200735d9 | |
mandlm | 32be1eaa95 |
|
@ -1,6 +1,7 @@
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "./ui_mainwindow.h"
|
#include "./ui_mainwindow.h"
|
||||||
|
|
||||||
|
#include "bucket_finder.h"
|
||||||
#include "finder.h"
|
#include "finder.h"
|
||||||
#include "grouped_finder.h"
|
#include "grouped_finder.h"
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
|
@ -32,6 +33,7 @@ void MainWindow::setupAlgorithmSelector() {
|
||||||
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
|
search_algorithms_.appendRow(new QStandardItem("Parallel search"));
|
||||||
search_algorithms_.appendRow(new QStandardItem("Tree search"));
|
search_algorithms_.appendRow(new QStandardItem("Tree search"));
|
||||||
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
search_algorithms_.appendRow(new QStandardItem("Grouped search"));
|
||||||
|
search_algorithms_.appendRow(new QStandardItem("Bucket search"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::setupWordListSourceSelector() {
|
void MainWindow::setupWordListSourceSelector() {
|
||||||
|
@ -114,6 +116,9 @@ void MainWindow::createSelectedFinder() {
|
||||||
case 4:
|
case 4:
|
||||||
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
finder_ = std::make_unique<GroupedFinder>(word_list_);
|
||||||
break;
|
break;
|
||||||
|
case 5:
|
||||||
|
finder_ = std::make_unique<BucketFinder>(word_list_);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,9 @@ add_library(
|
||||||
src/tree_finder.cpp
|
src/tree_finder.cpp
|
||||||
include/tree_finder.h
|
include/tree_finder.h
|
||||||
src/grouped_finder.cpp
|
src/grouped_finder.cpp
|
||||||
include/grouped_finder.h)
|
include/grouped_finder.h
|
||||||
|
src/bucket_finder.cpp
|
||||||
|
include/bucket_finder.h)
|
||||||
|
|
||||||
target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
target_include_directories(vector_search PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,30 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "finder.h"
|
||||||
|
#include "word_list.h"
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
class Bucket {
|
||||||
|
private:
|
||||||
|
std::map<const char, WordRefList> groups_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Bucket() = default;
|
||||||
|
|
||||||
|
void insert(const WordList &word_list, size_t first_index, size_t last_index);
|
||||||
|
|
||||||
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class BucketFinder : public Finder {
|
||||||
|
private:
|
||||||
|
std::vector<Bucket> buckets_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
BucketFinder(const WordList &word_list);
|
||||||
|
|
||||||
|
std::forward_list<const std::string *>
|
||||||
|
find_prefix(std::string_view search_term) const override;
|
||||||
|
};
|
|
@ -11,6 +11,7 @@ public:
|
||||||
WordList &multiply(size_t factor);
|
WordList &multiply(size_t factor);
|
||||||
WordList &shuffle();
|
WordList &shuffle();
|
||||||
|
|
||||||
|
static WordList oneCap();
|
||||||
static WordList fourCaps();
|
static WordList fourCaps();
|
||||||
static WordList fromFile(const std::filesystem::path &path);
|
static WordList fromFile(const std::filesystem::path &path);
|
||||||
|
|
||||||
|
@ -22,6 +23,7 @@ public:
|
||||||
|
|
||||||
class WordRefList : public std::vector<const std::string *> {
|
class WordRefList : public std::vector<const std::string *> {
|
||||||
public:
|
public:
|
||||||
|
WordRefList() = default;
|
||||||
WordRefList(const WordList &source);
|
WordRefList(const WordList &source);
|
||||||
|
|
||||||
static void find_prefix_in_range(
|
static void find_prefix_in_range(
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
#include "bucket_finder.h"
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
#include <strings.h>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
void Bucket::insert(const WordList &word_list, size_t first_index,
|
||||||
|
size_t last_index) {
|
||||||
|
for (size_t index = first_index; index < last_index; ++index) {
|
||||||
|
const auto ¤t_word = word_list[index];
|
||||||
|
groups_[current_word.front()].push_back(¤t_word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::forward_list<const std::string *>
|
||||||
|
Bucket::find_prefix(std::string_view search_term) const {
|
||||||
|
const auto group = groups_.find(search_term.front());
|
||||||
|
if (group == groups_.cend()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::forward_list<const std::string *> result;
|
||||||
|
for (const auto *word : group->second) {
|
||||||
|
if (word->starts_with(search_term)) {
|
||||||
|
result.push_front(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
BucketFinder::BucketFinder(const WordList &word_list) {
|
||||||
|
if (word_list.empty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t word_list_size = word_list.size();
|
||||||
|
const size_t bucket_count =
|
||||||
|
std::min<size_t>(std::thread::hardware_concurrency(), word_list_size);
|
||||||
|
const size_t bucket_size = word_list_size / bucket_count;
|
||||||
|
|
||||||
|
buckets_.resize(bucket_count);
|
||||||
|
|
||||||
|
std::vector<std::thread> insert_threads;
|
||||||
|
for (auto bucket_index = 0; bucket_index < bucket_count; ++bucket_index) {
|
||||||
|
const size_t first_index = bucket_index * bucket_size;
|
||||||
|
const size_t last_index = (bucket_index == bucket_count - 1)
|
||||||
|
? word_list_size
|
||||||
|
: (bucket_index + 1) * bucket_size;
|
||||||
|
|
||||||
|
auto &bucket = buckets_[bucket_index];
|
||||||
|
|
||||||
|
insert_threads.emplace_back([&bucket, &word_list, first_index, last_index] {
|
||||||
|
bucket.insert(word_list, first_index, last_index);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &thread : insert_threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::forward_list<const std::string *>
|
||||||
|
BucketFinder::find_prefix(std::string_view search_term) const {
|
||||||
|
std::forward_list<const std::string *> result;
|
||||||
|
std::mutex result_mutex;
|
||||||
|
|
||||||
|
std::vector<std::thread> search_threads;
|
||||||
|
for (const auto &bucket : buckets_) {
|
||||||
|
|
||||||
|
search_threads.emplace_back([&] {
|
||||||
|
auto thread_result = bucket.find_prefix(search_term);
|
||||||
|
if (!thread_result.empty()) {
|
||||||
|
std::lock_guard<std::mutex> result_lock(result_mutex);
|
||||||
|
result.merge(thread_result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &thread : search_threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
|
@ -25,6 +25,19 @@ WordList &WordList::shuffle() {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WordList WordList::oneCap() {
|
||||||
|
const static std::string charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
|
||||||
|
WordList word_list;
|
||||||
|
word_list.reserve(charset_.length());
|
||||||
|
|
||||||
|
for (auto char_1 : charset_) {
|
||||||
|
word_list.emplace_back(std::initializer_list<char>({char_1}));
|
||||||
|
}
|
||||||
|
|
||||||
|
return word_list;
|
||||||
|
};
|
||||||
|
|
||||||
WordList WordList::fourCaps() {
|
WordList WordList::fourCaps() {
|
||||||
const static std::string charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
const static std::string charset_ = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "bucket_finder.h"
|
||||||
#include "grouped_finder.h"
|
#include "grouped_finder.h"
|
||||||
#include "linear_finder.h"
|
#include "linear_finder.h"
|
||||||
#include "parallel_finder.h"
|
#include "parallel_finder.h"
|
||||||
|
@ -54,6 +55,8 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
auto word_list = generate_word_list(5);
|
auto word_list = generate_word_list(5);
|
||||||
|
|
||||||
|
test_finder<BucketFinder>(word_list, "bucket finder");
|
||||||
|
|
||||||
test_finder<LinearFinder>(word_list, "linear finder");
|
test_finder<LinearFinder>(word_list, "linear finder");
|
||||||
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
|
test_finder<SortedLinearFinder>(word_list, "sorted linear finder");
|
||||||
test_finder<ParallelFinder>(word_list, "parallel finder");
|
test_finder<ParallelFinder>(word_list, "parallel finder");
|
||||||
|
|
Loading…
Reference in New Issue