-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrie.cpp
More file actions
34 lines (30 loc) · 936 Bytes
/
trie.cpp
File metadata and controls
34 lines (30 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "trie.h"
void Trie::insert(std::string_view key, std::string_view value)
{
std::reference_wrapper<TrieNode> node = root;
for (char character : key)
{
auto [it, inserted] = node.get().children.emplace(character, nodes.emplace_back());
node = it->second.get();
}
node.get().target = value;
}
[[nodiscard]] std::pair<std::string_view, bool> Trie::searchFullMatch(std::span<const char> cachedData) const
{
std::reference_wrapper<const TrieNode> node = root;
for (char c : cachedData)
{
auto res = node.get().children.find(c);
if (res == node.get().children.end())
{
return std::make_pair(std::string_view(), false);
}
node = res->second.get();
}
// full match
if (node.get().target)
{
return std::make_pair(node.get().target.value(), true);
}
return std::make_pair(std::string_view(), false);
}