VectorSearch/vector_search_cli/main.cpp

95 lines
2.5 KiB
C++
Raw Normal View History

2024-03-20 15:37:00 +00:00
#include "grouped_finder.h"
#include "linear_finder.h"
#include "parallel_finder.h"
#include "timer.h"
2024-03-20 14:05:25 +00:00
#include "tree_finder.h"
#include "word_list_generator.h"
#include <cstdlib>
2024-03-18 11:12:08 +00:00
#include <iostream>
#include <thread>
using std::string, std::string_view, std::vector, std::thread, std::cout,
std::endl;
vector<string> generate_word_list() {
cout << "\ngenerating word list" << endl;
Timer generator_timer;
auto word_list = WordListGenerator().generate();
generator_timer.stop();
cout << "word list generator took " << generator_timer << endl;
cout << "word list is " << word_list.size() << " element(s) long" << endl;
return word_list;
}
2024-03-20 15:37:00 +00:00
void test_finder_search(Finder &finder, std::string_view name,
std::string_view search_term) {
Timer find_timer;
2024-03-20 15:37:00 +00:00
auto result = finder.find_prefix(search_term);
find_timer.stop();
2024-03-20 15:37:00 +00:00
cout << name << "(" << search_term << ") took " << find_timer << endl;
cout << "result list is " << std::distance(result.cbegin(), result.cend())
<< " element(s) long" << endl;
}
2024-03-20 15:37:00 +00:00
void test_finder(Finder &finder, std::string_view name) {
for (const auto &search_term : {"A", "AB", "ABC", "ABCD"}) {
test_finder_search(finder, name, search_term);
}
}
2024-03-20 15:37:00 +00:00
void test_linear_finder(const vector<string> &word_list) {
cout << "\nrunning linear finder" << endl;
2024-03-20 15:37:00 +00:00
LinearFinder linear_finder(word_list);
2024-03-20 15:37:00 +00:00
test_finder(linear_finder, "linear finder");
}
2024-03-20 15:37:00 +00:00
void test_parallel_finder(const vector<string> &word_list) {
cout << "\nrunning parallel finder" << endl;
2024-03-20 15:37:00 +00:00
ParallelFinder parallel_finder(word_list);
test_finder(parallel_finder, "parallel finder");
}
2024-03-18 11:12:08 +00:00
2024-03-20 14:05:25 +00:00
void test_tree_finder(const vector<string> &word_list) {
cout << "\nrunning tree finder" << endl;
Timer constructor_timer;
TreeFinder tree_finder(word_list);
constructor_timer.stop();
cout << "tree finder constructor took " << constructor_timer << endl;
2024-03-20 15:37:00 +00:00
test_finder(tree_finder, "tree finder");
}
2024-03-20 14:05:25 +00:00
2024-03-20 15:37:00 +00:00
void test_grouped_finder(const vector<string> &word_list) {
cout << "\nrunning grouped finder" << endl;
Timer constructor_timer;
GroupedFinder grouped_finder(word_list);
constructor_timer.stop();
cout << "grouped finder constructor took " << constructor_timer << endl;
test_finder(grouped_finder, "grouped_finder");
2024-03-20 14:05:25 +00:00
}
2024-03-18 11:12:08 +00:00
int main(int argc, char *argv[]) {
cout << "\n== VectorSearch ==" << endl;
auto word_list = generate_word_list();
test_linear_finder(word_list);
test_parallel_finder(word_list);
2024-03-20 14:05:25 +00:00
test_tree_finder(word_list);
2024-03-20 15:37:00 +00:00
test_grouped_finder(word_list);
2024-03-18 11:12:08 +00:00
return EXIT_SUCCESS;
2024-03-18 11:12:08 +00:00
}