liblevenshtein 4.0.0
A library for generating Finite State Transducers based on Levenshtein Automata.
Loading...
Searching...
No Matches
dawg_node.cpp
Go to the documentation of this file.
1#include <array>
2#include <iostream>
3
4#include "MurmurHash2.h"
5
7
8namespace liblevenshtein {
9
10DawgNode::DawgNode(std::map<char, DawgNode *> &edges, bool is_final)
11 : _edges(edges),
12 _is_final(is_final)
13{}
14
15DawgNode::DawgNode(bool is_final)
16 : _is_final(is_final)
17{}
18
19void DawgNode::is_final(bool is_final) {
21}
22
23auto DawgNode::is_final() const -> bool {
24 return _is_final;
25}
26
27void DawgNode::for_each_edge(const std::function<void(char, DawgNode *)> &fn) const {
28 for (const auto &[label, target] : _edges) {
29 fn(label, target);
30 }
31}
32
33auto DawgNode::transition(char label) const -> DawgNode * {
34 auto iter = _edges.find(label);
35 if (iter != _edges.end()) {
36 return iter->second;
37 }
38 return nullptr;
39}
40
41auto DawgNode::add_edge(char label, DawgNode *target) -> DawgNode * {
42 DawgNode* existing = transition(label);
43 delete existing;
44 _edges[label] = target;
45 return this;
46}
47
48#if _MSC_VER && !__INTEL_COMPILER
49#pragma warning(push)
50#pragma warning(disable : 5232)
51#endif
52
53auto DawgNode::operator==(const DawgNode &other) const -> bool {
54 if (is_final() != other.is_final()) {
55 return false;
56 }
57
58 if (_edges.size() != other._edges.size()) {
59 return false;
60 }
61
62 // NOLINTNEXTLINE(readability-use-anyofallof)
63 for (const auto &[label, expected_target] : _edges) {
65 if (actual_target == nullptr || *expected_target != *actual_target) {
66 return false;
67 }
68 }
69
70 return true;
71}
72
73#if _MSC_VER && !__INTEL_COMPILER
74#pragma warning(pop)
75#endif
76
77auto operator<<(std::ostream &out, const DawgNode &node) -> std::ostream & {
78 out << "DawgNode{is_final=" << (node.is_final() ? "true" : "false") << ", edges={";
79 int index = 0;
80 node.for_each_edge([&](char label, DawgNode *target) {
81 if (index > 0) {
82 out << ", ";
83 }
84 out << "'" << label << "':DawgNode{...}";
85 index += 1;
86 });
87 out << "}}";
88 return out;
89}
90
91} // namespace liblevenshtein
92
93auto std::hash<liblevenshtein::DawgNode>::operator()(
94 const liblevenshtein::DawgNode &node) const -> std::size_t {
95
96 uint64_t hash_code = 0xDEADBEEF;
97 std::array<uint64_t, 1> key = {0};
98
99 node.for_each_edge([&](char label, liblevenshtein::DawgNode *target) {
100 key[0] = (unsigned char) label;
101 hash_code = MurmurHash64A(key.data(), 1, hash_code);
102
103 key[0] = (*this)(*target);
104 hash_code = MurmurHash64A(key.data(), 1, hash_code);
105 });
106
107 key[0] = (uint64_t) node.is_final();
108 return MurmurHash64A(key.data(), 1, hash_code);
109}
Represents a position within one or more terms of a DAWG dictionary.
Definition dawg_node.h:20
std::map< char, DawgNode * > _edges
Outgoing edges from this node.
Definition dawg_node.h:97
bool _is_final
Whether this node represents a word boundary.
Definition dawg_node.h:100
auto operator==(const DawgNode &other) const -> bool
Determines whether this node is equivalent to another.
Definition dawg_node.cpp:53
auto is_final() const -> bool
Declares whether this node represents a word boundary, or whether it immediately follows an edge havi...
Definition dawg_node.cpp:23
void for_each_edge(const std::function< void(char, DawgNode *)> &fn) const
Iterates over each outgoing edge of this node and invokes a callback function with each edge's charac...
Definition dawg_node.cpp:27
auto add_edge(char label, DawgNode *target) -> DawgNode *
Adds a new outgoing edge to this node.
Definition dawg_node.cpp:41
auto transition(char label) const -> DawgNode *
Returns the target node, following this one, along the edge annotated with the given label.
Definition dawg_node.cpp:33
DawgNode(bool is_final=false)
Constructs a new DAWG node with an optional parameter that determines whether it is final (i....
Definition dawg_node.cpp:15
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition main.cpp:25
Various utilities regarding Levenshtein transducers.
Definition namespaces.dox:9
auto operator<<(std::ostream &out, const Dawg &dawg) -> std::ostream &
Definition dawg.cpp:67