[search] Getting rank and center from trie.

This commit is contained in:
vng 2012-02-08 22:25:07 +03:00 committed by Alex Zolotarev
parent dcad11d5c2
commit c7913216d2
7 changed files with 68 additions and 46 deletions

View file

@ -51,6 +51,7 @@ public:
m_TypeSmallVillage[2] = GetType("place", "farm");
}
/*
void CorrectScaleForVisibility(TypesHolder const & types, int & scale) const
{
pair<int, int> const scaleR = feature::DrawableScaleRangeForText(types);
@ -66,21 +67,22 @@ public:
return ((scale != scaleNew) ? scales::GetRectForLevel(scaleNew, rect.Center(), 1.0) : rect);
}
*/
m2::RectD GetViewport(TypesHolder const & types, m2::RectD const & limitRect) const
m2::RectD GetViewport(TypesHolder const & types, m2::PointD const & center) const
{
if (types.GetGeoType() != feature::GEOM_POINT)
return CorrectRectForScales(types, limitRect);
int const upperScale = scales::GetUpperScale();
int scale = upperScale;
for (size_t i = 0; i < types.Size(); ++i)
scale = min(scale, GetScaleForType(types[i]));
CorrectScaleForVisibility(types, scale);
if (scale == upperScale)
{
// get minimal draw text scale for feature type, that not mentioned in GetScaleForType
scale = feature::DrawableScaleRangeForText(types).first;
}
m2::PointD const centerXY = limitRect.Center();
return scales::GetRectForLevel(scale, centerXY, 1.0);
return scales::GetRectForLevel(scale, center, 1.0);
}
uint8_t GetSearchRank(TypesHolder const & types, m2::PointD const & pt, uint32_t population) const
@ -208,9 +210,9 @@ FeatureEstimator const & GetFeatureEstimator()
} // namespace feature::impl
m2::RectD GetFeatureViewport(TypesHolder const & types, m2::RectD const & limitRect)
m2::RectD GetFeatureViewport(TypesHolder const & types, m2::PointD const & center)
{
return impl::GetFeatureEstimator().GetViewport(types, limitRect);
return impl::GetFeatureEstimator().GetViewport(types, center);
}
uint8_t GetSearchRank(TypesHolder const & types, m2::PointD const & pt, uint32_t population)

View file

@ -10,7 +10,7 @@ namespace feature
class TypesHolder;
/// Get viewport to show given feature. Used in search.
m2::RectD GetFeatureViewport(TypesHolder const & types, m2::RectD const & limitRect);
m2::RectD GetFeatureViewport(TypesHolder const & types, m2::PointD const & center);
/// Get search rank for a feature.
/// Roughly, rank + 1 means that feature is 1.x times more popular.

View file

@ -82,7 +82,7 @@ void FullMatchInTrie(TrieIterator const & trieRoot,
if (!pIter || symbolsMatched != s.size())
return;
for (size_t i = 0; i < pIter->m_value.size(); ++i)
f(pIter->m_value[i].m_featureId);
f(pIter->m_value[i]);
}
template <typename F>
@ -114,7 +114,7 @@ void PrefixMatchInTrie(TrieIterator const & trieRoot,
scoped_ptr<search::TrieIterator> pIter(trieQueue.top());
trieQueue.pop();
for (size_t i = 0; i < pIter->m_value.size(); ++i)
f(pIter->m_value[i].m_featureId);
f(pIter->m_value[i]);
for (size_t i = 0; i < pIter->m_edge.size(); ++i)
trieQueue.push(pIter->GoToEdge(i));
}
@ -122,7 +122,24 @@ void PrefixMatchInTrie(TrieIterator const & trieRoot,
template <class FilterT> class OffsetIntersecter
{
typedef unordered_set<uint32_t> SetType;
typedef trie::ValueReader::ValueType ValueT;
struct HashFn
{
size_t operator() (ValueT const & v) const
{
return (boost::hash_value(v.m_featureId));
}
};
struct EqualFn
{
bool operator() (ValueT const & v1, ValueT const & v2) const
{
return (v1.m_featureId == v2.m_featureId);
}
};
typedef unordered_set<ValueT, HashFn, EqualFn> SetType;
FilterT const & m_filter;
scoped_ptr<SetType> m_prevSet;
@ -130,17 +147,19 @@ template <class FilterT> class OffsetIntersecter
public:
explicit OffsetIntersecter(FilterT const & filter)
: m_filter(filter), m_set(new SetType()) {}
void operator() (uint32_t offset)
: m_filter(filter), m_set(new SetType())
{
if (m_prevSet && !m_prevSet->count(offset))
}
void operator() (ValueT const & v)
{
if (m_prevSet && !m_prevSet->count(v))
return;
if (!m_filter(offset))
if (!m_filter(v.m_featureId))
return;
m_set->insert(offset);
m_set->insert(v);
}
void NextStep()
@ -154,7 +173,7 @@ public:
template <class ToDo> void ForEachResult(ToDo & toDo) const
{
for (SetType::const_iterator i = m_prevSet->begin(); i != m_prevSet->end(); ++i)
for (typename SetType::const_iterator i = m_prevSet->begin(); i != m_prevSet->end(); ++i)
toDo(*i);
}
};

View file

@ -6,6 +6,7 @@
#include "../indexer/feature.hpp"
#include "../indexer/feature_utils.hpp"
#include "../indexer/mercator.hpp"
#include "../indexer/scales.hpp"
#include "../geometry/angles.hpp"
#include "../geometry/distance_on_sphere.hpp"
@ -20,12 +21,12 @@ namespace impl
{
IntermediateResult::IntermediateResult(m2::RectD const & viewportRect, m2::PointD const & pos,
FeatureType const & f,
FeatureType const & f, m2::PointD const & center, uint8_t rank,
string const & displayName, string const & fileName)
: m_types(f),
m_str(displayName),
m_rect(f.GetLimitRect(-2)),
m_resultType(RESULT_FEATURE)
m_resultType(RESULT_FEATURE),
m_center(center), m_searchRank(rank)
{
ASSERT_GREATER(m_types.Size(), 0, ());
@ -33,45 +34,40 @@ IntermediateResult::IntermediateResult(m2::RectD const & viewportRect, m2::Point
if (!fileName.empty())
m_region.SetName(fileName);
else
m_region.SetPoint(m_rect.Center());
m_region.SetPoint(m_center);
CalcCommonParams(viewportRect, pos);
m_searchRank = feature::GetSearchRank(m_types, m_rect.Center(), f.GetPopulation());
}
IntermediateResult::IntermediateResult(m2::RectD const & viewportRect, m2::PointD const & pos,
double lat, double lon, double precision)
: m_str("(" + strings::to_string(lat) + ", " + strings::to_string(lon) + ")"),
m_rect(MercatorBounds::LonToX(lon - precision), MercatorBounds::LatToY(lat - precision),
MercatorBounds::LonToX(lon + precision), MercatorBounds::LatToY(lat + precision)),
m_center(m2::PointD(MercatorBounds::LonToX(lon), MercatorBounds::LatToY(lat))),
m_resultType(RESULT_LATLON), m_searchRank(255)
{
CalcCommonParams(viewportRect, pos);
// get region info
m_region.SetPoint(m2::PointD(MercatorBounds::LonToX(lon),
MercatorBounds::LatToY(lat)));
m_region.SetPoint(m_center);
}
void IntermediateResult::CalcCommonParams(m2::RectD const & viewportRect, m2::PointD const & pos)
{
m2::PointD const center = m_rect.Center();
// Check if point is valid (see Query::empty_pos_value).
if (pos.x > -500 && pos.y > -500)
{
ASSERT ( my::between_s(-180.0, 180.0, pos.x), (pos.x) );
ASSERT ( my::between_s(-180.0, 180.0, pos.y), (pos.y) );
m_distance = ResultDistance(pos, center);
m_distance = ResultDistance(pos, m_center);
}
else
{
// empty destance
// empty distance
m_distance = -1.0;
}
m_viewportDistance = ViewportDistance(viewportRect, center);
m_viewportDistance = ViewportDistance(viewportRect, m_center);
}
IntermediateResult::IntermediateResult(string const & name, int penalty)
@ -142,11 +138,12 @@ Result IntermediateResult::GenerateFinalResult(
+ ' ' + strings::to_string(static_cast<int>(m_searchRank))
#endif
,
GetBestType(), feature::GetFeatureViewport(m_types, m_rect),
GetBestType(), feature::GetFeatureViewport(m_types, m_center),
m_distance);
case RESULT_LATLON:
return Result(m_str, info.m_name, info.m_flag, string(), 0, m_rect, m_distance);
return Result(m_str, info.m_name, info.m_flag, string(), 0,
scales::GetRectForLevel(scales::GetUpperScale(), m_center, 1.0), m_distance);
default:
ASSERT_EQUAL ( m_resultType, RESULT_CATEGORY, () );

View file

@ -36,9 +36,8 @@ public:
// For RESULT_FEATURE.
IntermediateResult(m2::RectD const & viewportRect, m2::PointD const & pos,
FeatureType const & f,
string const & displayName,
string const & fileName);
FeatureType const & f, m2::PointD const & center, uint8_t rank,
string const & displayName, string const & fileName);
// For RESULT_LATLON.
IntermediateResult(m2::RectD const & viewportRect, m2::PointD const & pos,
@ -124,7 +123,7 @@ private:
void GetRegion(storage::CountryInfoGetter const * pInfo, storage::CountryInfo & info) const;
} m_region;
m2::RectD m_rect;
m2::PointD m_center;
double m_distance;
int m_viewportDistance;

View file

@ -359,13 +359,14 @@ void Query::FlushResults(Results & res)
}
}
void Query::AddFeatureResult(FeatureType const & f, string const & fName)
void Query::AddFeatureResult(FeatureType const & f, TrieValueT const & val, string const & fName)
{
uint32_t penalty;
string name;
GetBestMatchName(f, penalty, name);
AddResult(ValueT(new impl::IntermediateResult(m_viewport, m_position, f, name, fName)));
AddResult(ValueT(new impl::IntermediateResult(
m_viewport, m_position, f, val.m_pt, val.m_rank, name, fName)));
}
namespace impl
@ -436,18 +437,18 @@ public:
{
}
void operator() (uint32_t offset)
void operator() (trie::ValueReader::ValueType const & value)
{
++m_count;
FeatureType feature;
m_featuresVector.Get(offset, feature);
m_featuresVector.Get(value.m_featureId, feature);
//#ifdef DEBUG
// DoFindByName doFind("ул. Карбышева");
// feature.ForEachNameRef(doFind);
//#endif
m_query.AddFeatureResult(feature, m_fName);
m_query.AddFeatureResult(feature, value, m_fName);
}
size_t GetCount() const { return m_count; }

View file

@ -1,6 +1,8 @@
#pragma once
#include "intermediate_result.hpp"
#include "../indexer/search_trie.hpp"
#include "../geometry/rect2d.hpp"
#include "../base/buffer_vector.hpp"
@ -41,6 +43,8 @@ public:
// Vector of pairs (string_to_suggest, min_prefix_length_to_suggest).
typedef vector<pair<strings::UniString, uint8_t> > StringsToSuggestVectorT;
typedef trie::ValueReader::ValueType TrieValueT;
Query(Index const * pIndex,
CategoriesMapT const * pCategories,
StringsToSuggestVectorT const * pStringsToSuggest,
@ -71,7 +75,7 @@ private:
typedef shared_ptr<ResultT> ValueT;
void AddResult(ValueT const & result);
void AddFeatureResult(FeatureType const & f, string const & fName);
void AddFeatureResult(FeatureType const & f, TrieValueT const & val, string const & fName);
void FlushResults(Results & res);
void UpdateViewportOffsets();
void SearchFeatures();