1 #ifndef GAME_MWDIALOGUE_KEYWORDSEARCH_H
2 #define GAME_MWDIALOGUE_KEYWORDSEARCH_H
15 template <
typename string_t,
typename value_t>
20 typedef typename string_t::const_iterator
Point;
29 void seed (string_t keyword, value_t value)
44 typename Entry::childen_t::iterator current;
45 typename Entry::childen_t::iterator next;
52 value = current->second.mValue;
56 for (
Point i = ++keyword.begin(); i != keyword.end(); ++i)
59 if (next == current->second.mChildren.end())
63 value = next->second.mValue;
71 static bool sortMatches(
const Match& left,
const Match& right)
73 return left.mBeg < right.mBeg;
78 std::vector<Match> matches;
79 for (
Point i = beg; i != end; ++i)
103 std::vector< typename std::pair<int, typename Entry::childen_t::iterator> > candidates;
105 while ((j + 1) != end)
109 if (next == candidate->second.mChildren.end ())
111 if (candidate->second.mKeyword.size() > 0)
112 candidates.push_back(std::make_pair((j-i), candidate));
118 if (candidate->second.mKeyword.size() > 0)
119 candidates.push_back(std::make_pair((j-i), candidate));
122 if (candidates.empty())
126 std::reverse(candidates.begin(), candidates.end());
128 for (
typename std::vector< std::pair<int, typename Entry::childen_t::iterator> >::iterator it = candidates.begin();
129 it != candidates.end(); ++it)
131 candidate = it->second;
133 Point k = i + it->first;
134 typename string_t::const_iterator t = candidate->second.mKeyword.begin () + (k - i);
137 while (k != end && t != candidate->second.mKeyword.end ())
146 if (t != candidate->second.mKeyword.end ())
152 match.mValue = candidate->second.mValue;
155 matches.push_back(match);
161 while (!matches.empty())
163 int longestKeywordSize = 0;
164 typename std::vector<Match>::iterator longestKeyword = matches.begin();
165 for (
typename std::vector<Match>::iterator it = matches.begin(); it != matches.end(); ++it)
167 int size = it->mEnd - it->mBeg;
168 if (size > longestKeywordSize)
170 longestKeywordSize = size;
174 typename std::vector<Match>::iterator next = it;
177 if (next == matches.end())
180 if (it->mEnd <= next->mBeg)
186 Match keyword = *longestKeyword;
187 matches.erase(longestKeyword);
188 out.push_back(keyword);
190 for (
typename std::vector<Match>::iterator it = matches.begin(); it != matches.end();)
192 if (it->mBeg < keyword.mEnd && it->mEnd > keyword.mBeg)
193 it = matches.erase(it);
217 typename Entry::childen_t::iterator j = entry.mChildren.find (ch);
219 if (j == entry.mChildren.end ())
221 entry.mChildren [ch].mValue = (value);
222 entry.mChildren [ch].mKeyword = (keyword);
226 if (j->second.mKeyword.size () > 0)
228 if (keyword == j->second.mKeyword)
229 throw std::runtime_error (
"duplicate keyword inserted");
231 value_t pushValue = (j->second.mValue);
232 string_t pushKeyword = (j->second.mKeyword);
234 if (depth >= pushKeyword.size ())
235 throw std::runtime_error (
"unexpected");
237 if (depth+1 < pushKeyword.size())
239 seed_impl ( (pushKeyword), (pushValue), depth+1, j->second);
240 j->second.mKeyword.clear ();
243 if (depth+1 == keyword.size())
244 j->second.mKeyword = value;
246 seed_impl ( (keyword), (value), depth+1, j->second);
Basic quest/dialogue/topic entry.
Definition: journalentry.hpp:19
Point mBeg
Definition: keywordsearch.hpp:24
static bool ciEqual(const std::string &x, const std::string &y)
Definition: stringops.hpp:62
static bool sortMatches(const Match &left, const Match &right)
Definition: keywordsearch.hpp:71
void highlightKeywords(Point beg, Point end, std::vector< Match > &out)
Definition: keywordsearch.hpp:76
static char toLower(char c)
Definition: stringops.hpp:24
void clear()
Definition: keywordsearch.hpp:36
Point mEnd
Definition: keywordsearch.hpp:25
Entry mRoot
Definition: keywordsearch.hpp:251
bool containsKeyword(string_t keyword, value_t &value)
Definition: keywordsearch.hpp:42
Definition: keywordsearch.hpp:204
value_t mValue
Definition: keywordsearch.hpp:209
childen_t mChildren
Definition: keywordsearch.hpp:210
std::map< wchar_t, Entry > childen_t
Definition: keywordsearch.hpp:206
value_t mValue
Definition: keywordsearch.hpp:26
void seed(string_t keyword, value_t value)
Definition: keywordsearch.hpp:29
Definition: keywordsearch.hpp:22
void seed_impl(string_t keyword, value_t value, size_t depth, Entry &entry)
Definition: keywordsearch.hpp:213
Definition: keywordsearch.hpp:16
string_t mKeyword
Definition: keywordsearch.hpp:208
string_t::const_iterator Point
Definition: keywordsearch.hpp:20