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
6
#include "
liblevenshtein/collection/dawg_node.h
"
7
8
namespace
liblevenshtein
{
9
10
DawgNode::DawgNode
(std::map<char, DawgNode *> &
edges
,
bool
is_final)
11
: _edges(
edges
),
12
_is_final(is_final)
13
{}
14
15
DawgNode::DawgNode
(
bool
is_final)
16
: _is_final(is_final)
17
{}
18
19
void
DawgNode::is_final
(
bool
is_final) {
20
_is_final
=
is_final
;
21
}
22
23
auto
DawgNode::is_final
()
const
->
bool
{
24
return
_is_final
;
25
}
26
27
void
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
33
auto
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
41
auto
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
53
auto
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) {
64
DawgNode
*
actual_target
=
other
.
transition
(label);
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
77
auto
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
93
auto
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
}
liblevenshtein::DawgNode
Represents a position within one or more terms of a DAWG dictionary.
Definition
dawg_node.h:20
liblevenshtein::DawgNode::_edges
std::map< char, DawgNode * > _edges
Outgoing edges from this node.
Definition
dawg_node.h:97
liblevenshtein::DawgNode::_is_final
bool _is_final
Whether this node represents a word boundary.
Definition
dawg_node.h:100
liblevenshtein::DawgNode::operator==
auto operator==(const DawgNode &other) const -> bool
Determines whether this node is equivalent to another.
Definition
dawg_node.cpp:53
liblevenshtein::DawgNode::is_final
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
liblevenshtein::DawgNode::for_each_edge
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
liblevenshtein::DawgNode::add_edge
auto add_edge(char label, DawgNode *target) -> DawgNode *
Adds a new outgoing edge to this node.
Definition
dawg_node.cpp:41
liblevenshtein::DawgNode::transition
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
liblevenshtein::DawgNode::DawgNode
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
dawg_node.h
query
void query(ll::Dawg *dawg, const std::string &query_term, std::size_t max_distance)
Definition
main.cpp:25
liblevenshtein
Various utilities regarding Levenshtein transducers.
Definition
namespaces.dox:9
liblevenshtein::operator<<
auto operator<<(std::ostream &out, const Dawg &dawg) -> std::ostream &
Definition
dawg.cpp:67
src
liblevenshtein
collection
dawg_node.cpp
Generated by
1.10.0