-
Notifications
You must be signed in to change notification settings - Fork 852
/
Copy pathtrienode.cpp
117 lines (92 loc) · 2.39 KB
/
trienode.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*******************************************************************
Part of the Fritzing project - http://fritzing.org
Copyright (c) 2007-2019 Fritzing
Fritzing is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Fritzing is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Fritzing. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
#include "trienode.h"
#include "../debugdialog.h"
#include <QXmlStreamReader>
TrieLeaf::TrieLeaf()
{
}
TrieLeaf::~TrieLeaf()
{
}
TrieNode::TrieNode(QChar c)
{
m_char = c;
m_leafData = nullptr;
m_isLeaf = false;
m_caseInsensitive = false;
}
TrieNode::~TrieNode()
{
Q_FOREACH (TrieNode * node, m_children) {
delete node;
}
m_children.clear();
}
void TrieNode::addString(QString & string, bool caseInsensitive, TrieLeaf * leaf) {
if (string.isEmpty()) {
m_leafData = leaf;
m_isLeaf = true;
return;
}
QChar in(string.at(0));
QString next = string.remove(0, 1);
QChar c0, c1;
if (caseInsensitive) {
c0 = in.toLower();
c1 = in.toUpper();
}
else {
c0 = c1 = in;
}
addStringAux(c0, next, caseInsensitive, leaf);
if (c0 != c1) {
addStringAux(c1, next, caseInsensitive, leaf);
}
}
void TrieNode::addStringAux(QChar c, QString & next, bool caseInsensitive, TrieLeaf * leaf) {
bool gotc = false;
Q_FOREACH (TrieNode * node, m_children) {
if (node->matchesChar(c)) {
node->addString(next, caseInsensitive, leaf);
gotc = true;
}
}
if (!gotc) {
auto * child = new TrieNode(c);
m_children.append(child);
child->addString(next, caseInsensitive, leaf);
}
}
bool TrieNode::matchesChar(QChar c) {
return (c == m_char);
}
bool TrieNode::matches(QString & string, TrieLeaf * & leaf)
{
if (string.isEmpty()) {
if (m_isLeaf) {
leaf = m_leafData;
return true;
}
return false;
}
QChar in(string.at(0));
Q_FOREACH (TrieNode * node, m_children) {
if (node->matchesChar(in)) {
return node->matches(string.remove(0, 1), leaf);
}
}
return false;
}