forked from organicmaps/organicmaps
[search] Introduce IntermediateResult.
This commit is contained in:
parent
7a0a13de85
commit
1982758732
8 changed files with 70 additions and 32 deletions
|
@ -1098,7 +1098,7 @@ void FrameWork<TModel>::AddRedrawCommandSure()
|
|||
engine.Search(text, m_navigator.Screen().GlobalRect(), callback);
|
||||
|
||||
// Empty name indicates last element.
|
||||
callback(search::Result(string(), m2::RectD(), 0));
|
||||
callback(search::Result(string(), m2::RectD()));
|
||||
}
|
||||
|
||||
template class FrameWork<model::FeaturesFetcher>;
|
||||
|
|
31
search/intermediate_result.cpp
Normal file
31
search/intermediate_result.cpp
Normal file
|
@ -0,0 +1,31 @@
|
|||
#include "intermediate_result.hpp"
|
||||
#include "../base/string_utils.hpp"
|
||||
|
||||
namespace search
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
|
||||
IntermediateResult::IntermediateResult(FeatureType const & feature,
|
||||
string const & displayName,
|
||||
int matchPenalty)
|
||||
: m_str(displayName), m_rect(feature.GetLimitRect(-1)), m_matchPenalty(matchPenalty)
|
||||
{
|
||||
}
|
||||
|
||||
bool IntermediateResult::operator < (IntermediateResult const & o) const
|
||||
{
|
||||
return m_matchPenalty < o.m_matchPenalty;
|
||||
}
|
||||
|
||||
Result IntermediateResult::GenerateFinalResult() const
|
||||
{
|
||||
#ifdef DEBUG
|
||||
return Result(m_str + ' ' + strings::to_string(m_matchPenalty), m_rect);
|
||||
#else
|
||||
return Result(m_str, m_rect);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace search::impl
|
||||
} // namespace search
|
26
search/intermediate_result.hpp
Normal file
26
search/intermediate_result.hpp
Normal file
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
#include "result.hpp"
|
||||
#include "../indexer/feature.hpp"
|
||||
|
||||
namespace search
|
||||
{
|
||||
namespace impl
|
||||
{
|
||||
|
||||
class IntermediateResult
|
||||
{
|
||||
public:
|
||||
IntermediateResult(FeatureType const & feature, string const & displayName, int matchPenalty);
|
||||
|
||||
bool operator < (IntermediateResult const & o) const;
|
||||
|
||||
Result GenerateFinalResult() const;
|
||||
|
||||
private:
|
||||
string m_str;
|
||||
m2::RectD m_rect;
|
||||
int m_matchPenalty;
|
||||
};
|
||||
|
||||
} // namespace search::impl
|
||||
} // namespace search
|
|
@ -48,8 +48,8 @@ struct FeatureProcessor
|
|||
512, 256 * max(0, int(m_query.m_prefix.size()) - 1),
|
||||
&KeywordMatch, &PrefixMatch);
|
||||
feature.ForEachNameRef(matcher);
|
||||
m_query.AddResult(Result(matcher.GetBestPrefixMatch(), feature.GetLimitRect(-1),
|
||||
matcher.GetMatchScore()));
|
||||
m_query.AddResult(IntermediateResult(
|
||||
feature, matcher.GetBestPrefixMatch(), matcher.GetMatchScore()));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -63,24 +63,19 @@ void Query::Search(function<void (Result const &)> const & f)
|
|||
results.reserve(m_resuts.size());
|
||||
while (!m_resuts.empty())
|
||||
{
|
||||
results.push_back(m_resuts.top());
|
||||
results.push_back(m_resuts.top().GenerateFinalResult());
|
||||
m_resuts.pop();
|
||||
}
|
||||
for (vector<Result>::const_reverse_iterator it = results.rbegin(); it != results.rend(); ++it)
|
||||
f(*it);
|
||||
}
|
||||
|
||||
void Query::AddResult(Result const & result)
|
||||
void Query::AddResult(IntermediateResult const & result)
|
||||
{
|
||||
m_resuts.push(result);
|
||||
while (m_resuts.size() > 10)
|
||||
m_resuts.pop();
|
||||
}
|
||||
|
||||
bool Query::ResultBetter::operator ()(Result const & r1, Result const & r2) const
|
||||
{
|
||||
return r1.GetPenalty() < r2.GetPenalty();
|
||||
}
|
||||
|
||||
} // namespace search::impl
|
||||
} // namespace search
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "engine.hpp"
|
||||
#include "intermediate_result.hpp"
|
||||
#include "result.hpp"
|
||||
#include "../geometry/rect2d.hpp"
|
||||
#include "../base/string_utils.hpp"
|
||||
|
@ -25,12 +26,7 @@ public:
|
|||
void Search(function<void (Result const &)> const & f);
|
||||
|
||||
// Add result for scoring.
|
||||
void AddResult(Result const & result);
|
||||
|
||||
struct ResultBetter
|
||||
{
|
||||
bool operator() (Result const & r1, Result const & r2) const;
|
||||
};
|
||||
void AddResult(IntermediateResult const & result);
|
||||
|
||||
string m_queryText;
|
||||
vector<strings::UniString> m_keywords;
|
||||
|
@ -41,7 +37,7 @@ public:
|
|||
IndexType const * m_pIndex;
|
||||
IndexType::Query m_indexQuery;
|
||||
|
||||
priority_queue<Result, vector<Result>, ResultBetter> m_resuts;
|
||||
priority_queue<IntermediateResult> m_resuts;
|
||||
};
|
||||
|
||||
} // namespace search::impl
|
||||
|
|
|
@ -1,21 +1,11 @@
|
|||
#include "result.hpp"
|
||||
#include "../base/base.hpp"
|
||||
#include "../base/string_utils.hpp"
|
||||
|
||||
namespace search
|
||||
{
|
||||
|
||||
Result::Result(string const & str, m2::RectD const & rect, int penalty)
|
||||
: m_str(str), m_rect(rect), m_penalty(penalty)
|
||||
Result::Result(string const & str, m2::RectD const & rect)
|
||||
: m_str(str), m_rect(rect)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
if (!str.empty())
|
||||
{
|
||||
m_str.push_back(' ');
|
||||
m_str += strings::to_string(penalty);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
} // namespace search
|
||||
|
|
|
@ -9,16 +9,14 @@ namespace search
|
|||
class Result
|
||||
{
|
||||
public:
|
||||
Result(string const & str, m2::RectD const & rect, int penalty);
|
||||
Result(string const & str, m2::RectD const & rect);
|
||||
|
||||
string GetString() const { return m_str; }
|
||||
m2::RectD GetRect() const { return m_rect; }
|
||||
int GetPenalty() const { return m_penalty; }
|
||||
|
||||
private:
|
||||
string m_str;
|
||||
m2::RectD m_rect;
|
||||
int m_penalty;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ include($$ROOT_DIR/common.pri)
|
|||
HEADERS += \
|
||||
delimiters.hpp \
|
||||
engine.hpp \
|
||||
intermediate_result.hpp \
|
||||
keyword_matcher.hpp \
|
||||
query.hpp \
|
||||
result.hpp \
|
||||
|
@ -20,6 +21,7 @@ HEADERS += \
|
|||
SOURCES += \
|
||||
delimiters.cpp \
|
||||
engine.cpp \
|
||||
intermediate_result.cpp \
|
||||
keyword_matcher.cpp \
|
||||
query.cpp \
|
||||
result.cpp \
|
||||
|
|
Loading…
Add table
Reference in a new issue