-
Notifications
You must be signed in to change notification settings - Fork 3
/
Trie_impl.cpp
167 lines (138 loc) · 4.29 KB
/
Trie_impl.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Trie.hpp"
namespace LibWheel
{
UninitializedException::UninitializedException(const std::string& s)
: runtime_error(s)
{}
template <typename CharType, typename MatchType>
Trie<CharType, MatchType>::Node::Node()
: match(NULL), transitions()
{}
template <typename CharType, typename MatchType>
Trie<CharType, MatchType>::Trie()
: tree()
{
Node node;
tree.push_back(node);
}
template <typename CharType, typename MatchType>
Trie<CharType, MatchType>::~Trie()
{}
template <typename CharType, typename MatchType>
void
Trie<CharType, MatchType>::addString(const StringType& str, const MatchType& match)
{
Node* cur;
Node* next = &(*tree.begin());
typename Node::TransitionsType::iterator transition;
for (typename StringType::const_iterator i=str.begin(); i!=str.end(); ++i)
{
cur = next;
transition = cur->transitions.find(*i);
if (transition == cur->transitions.end())
{
// we don't have this character; add it
Node node;
tree.push_back(node);
next = &(*(--tree.end()));
cur->transitions.insert(std::pair<CharType, Node*>(*i, next));
}
else
{
// we already have this character; move on to the next
next = transition->second;
}
}
// we're now at the end node for this string
// fail silently if there's already a match here
if (next->match == NULL)
next->match = &match;
}
/*template <typename CharType, typename MatchType>
const typename Trie<CharType, MatchType>::Node*
Trie<CharType, MatchType>::getInitialState() const
{
return &(*tree.begin());
}*/
template <typename CharType, typename MatchType>
const MatchType*
Trie<CharType, MatchType>::search(const StringType& str) const
{
const Node* cur = &(*tree.begin());
typename Node::TransitionsType::const_iterator transition;
// follow the tree
for (typename StringType::const_iterator i=str.begin(); i!=str.end(); ++i)
{
transition = cur->transitions.find(*i);
if (transition == cur->transitions.end())
return NULL;
else
cur = transition->second;
}
// return the match, if there is one
return cur->match;
}
template <typename CharType, typename MatchType>
const MatchType*
Trie<CharType, MatchType>::search(const CharType* str, size_t strlen) const
{
const Node* cur = &(*tree.begin());
typename Node::TransitionsType::const_iterator transition;
assert(str != NULL);
// follow the tree
for (unsigned i=0; i<strlen; i++)
{
transition = cur->transitions.find(str[i]);
if (transition == cur->transitions.end())
return NULL;
else
cur = transition->second;
}
// return the match, if there is one
return cur->match;
}
} // namespace LibWheel
#if 0
#include <iostream>
#include <boost/cstdint.hpp>
int main()
{
LibWheel::Trie<uint16_t, std::string> tree;
const std::string* str;
std::vector<uint16_t> vec1;
std::vector<uint16_t> vec2;
std::vector<uint16_t> vec3;
std::vector<uint16_t> vec4;
std::string str1 = "123";
std::string str2 = "124";
std::string str3 = "312";
vec1.push_back(1);
vec1.push_back(2);
vec1.push_back(3);
vec2.push_back(1);
vec2.push_back(2);
vec2.push_back(4);
vec3.push_back(3);
vec3.push_back(1);
vec3.push_back(2);
vec3.push_back(1);
vec3.push_back(2);
vec3.push_back(2);
tree.addString(vec1, str1);
tree.addString(vec2, str2);
tree.addString(vec3, str3);
str = tree.search(vec1);
if (str)
std::cout << "found " << *str << std::endl;
str = tree.search(vec2);
if (str)
std::cout << "found " << *str << std::endl;
str = tree.search(vec3);
if (str)
std::cout << "found " << *str << std::endl;
str = tree.search(vec4);
if (str)
std::cout << "found " << *str << std::endl;
return 0;
}
#endif