diff options
author | vng <viktor.govako@gmail.com> | 2012-02-08 23:25:07 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:33:52 +0300 |
commit | c7913216d250cbf8cb173ca98f97e7592e22283b (patch) | |
tree | af483bc75353a0157daa479359730f754f64be18 | |
parent | dcad11d5c25dc6c1382f9a9188c5330285a07f99 (diff) |
[search] Getting rank and center from trie.
-rw-r--r-- | indexer/feature_utils.cpp | 20 | ||||
-rw-r--r-- | indexer/feature_utils.hpp | 2 | ||||
-rw-r--r-- | search/feature_offset_match.hpp | 37 | ||||
-rw-r--r-- | search/intermediate_result.cpp | 29 | ||||
-rw-r--r-- | search/intermediate_result.hpp | 7 | ||||
-rw-r--r-- | search/search_query.cpp | 11 | ||||
-rw-r--r-- | search/search_query.hpp | 6 |
7 files changed, 67 insertions, 45 deletions
diff --git a/indexer/feature_utils.cpp b/indexer/feature_utils.cpp index 051bf2b33e..0302354f80 100644 --- a/indexer/feature_utils.cpp +++ b/indexer/feature_utils.cpp @@ -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) diff --git a/indexer/feature_utils.hpp b/indexer/feature_utils.hpp index a4f07d27ef..93906eedbe 100644 --- a/indexer/feature_utils.hpp +++ b/indexer/feature_utils.hpp @@ -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. diff --git a/search/feature_offset_match.hpp b/search/feature_offset_match.hpp index 9c8d5ad9db..f86d26ac5d 100644 --- a/search/feature_offset_match.hpp +++ b/search/feature_offset_match.hpp @@ -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()) {} + : m_filter(filter), m_set(new SetType()) + { + } - void operator() (uint32_t offset) + void operator() (ValueT const & v) { - if (m_prevSet && !m_prevSet->count(offset)) + 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); } }; diff --git a/search/intermediate_result.cpp b/search/intermediate_result.cpp index f1c81ec1e3..2288951f5f 100644 --- a/search/intermediate_result.cpp +++ b/search/intermediate_result.cpp @@ -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, () ); diff --git a/search/intermediate_result.hpp b/search/intermediate_result.hpp index 3e6125f894..1884baba26 100644 --- a/search/intermediate_result.hpp +++ b/search/intermediate_result.hpp @@ -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; diff --git a/search/search_query.cpp b/search/search_query.cpp index cae9a99b2c..3362776a99 100644 --- a/search/search_query.cpp +++ b/search/search_query.cpp @@ -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; } diff --git a/search/search_query.hpp b/search/search_query.hpp index 2885a6827c..e400474453 100644 --- a/search/search_query.hpp +++ b/search/search_query.hpp @@ -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(); |