feat: add sorted linear finder
This commit is contained in:
parent
291d8d198e
commit
26d3839832
5 changed files with 80 additions and 6 deletions
|
@ -13,6 +13,8 @@ add_library(
|
|||
include/timer.h
|
||||
src/linear_finder.cpp
|
||||
include/linear_finder.h
|
||||
src/sorted_linear_finder.cpp
|
||||
include/sorted_linear_finder.h
|
||||
src/parallel_finder.cpp
|
||||
include/parallel_finder.h
|
||||
src/tree_finder.cpp
|
||||
|
|
16
lib_vector_search/include/sorted_linear_finder.h
Normal file
16
lib_vector_search/include/sorted_linear_finder.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include "finder.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
class SortedLinearFinder : public Finder {
|
||||
private:
|
||||
std::vector<const std::string *> word_list_;
|
||||
|
||||
public:
|
||||
SortedLinearFinder(const std::vector<std::string> &word_list);
|
||||
|
||||
std::forward_list<const std::string *>
|
||||
find_prefix(std::string_view search_term) const override;
|
||||
};
|
32
lib_vector_search/src/sorted_linear_finder.cpp
Normal file
32
lib_vector_search/src/sorted_linear_finder.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include "sorted_linear_finder.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
using std::vector, std::forward_list, std::string, std::string_view;
|
||||
|
||||
SortedLinearFinder::SortedLinearFinder(const vector<string> &word_list) {
|
||||
std::transform(word_list.cbegin(), word_list.cend(),
|
||||
std::back_inserter(word_list_),
|
||||
[](const string &word) { return &word; });
|
||||
|
||||
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;
|
||||
|
||||
bool in_range = false;
|
||||
for (const auto *word : word_list_) {
|
||||
if (word->starts_with(search_term)) {
|
||||
matching_words.push_front(word);
|
||||
in_range = true;
|
||||
} else if (in_range) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return matching_words;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue