Update trie to return better results for search.
This commit is contained in:
@@ -6,6 +6,10 @@ namespace bed::internal::trie {
|
|||||||
template <typename T = void>
|
template <typename T = void>
|
||||||
struct Trie {
|
struct Trie {
|
||||||
using V = std::conditional_t<std::is_void_v<T>, std::monostate, T>;
|
using V = std::conditional_t<std::is_void_v<T>, std::monostate, T>;
|
||||||
|
using SearchResult = std::conditional_t<
|
||||||
|
std::is_void_v<T>,
|
||||||
|
std::string,
|
||||||
|
std::pair<std::string, V &>>;
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
std::string edge;
|
std::string edge;
|
||||||
@@ -113,8 +117,8 @@ struct Trie {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> search(std::string_view prefix) {
|
std::vector<SearchResult> search(std::string_view prefix) {
|
||||||
std::vector<std::string> result;
|
std::vector<SearchResult> result;
|
||||||
Node *current = &root;
|
Node *current = &root;
|
||||||
std::string key;
|
std::string key;
|
||||||
uint64_t pos = 0;
|
uint64_t pos = 0;
|
||||||
@@ -145,10 +149,15 @@ struct Trie {
|
|||||||
static void collect(
|
static void collect(
|
||||||
const Node &node,
|
const Node &node,
|
||||||
std::string &key,
|
std::string &key,
|
||||||
std::vector<std::string> &result
|
std::vector<SearchResult> &result
|
||||||
) {
|
) {
|
||||||
if (node.value)
|
if (node.value) {
|
||||||
|
if constexpr (std::is_void_v<T>) {
|
||||||
result.push_back(key);
|
result.push_back(key);
|
||||||
|
} else {
|
||||||
|
result.emplace_back(key, *node.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
for (auto *child : node.children) {
|
for (auto *child : node.children) {
|
||||||
const auto old_size = key.size();
|
const auto old_size = key.size();
|
||||||
key += child->edge;
|
key += child->edge;
|
||||||
|
|||||||
Reference in New Issue
Block a user