From fb562f9ceb3e21278dbdb71e46159a5644e4472a Mon Sep 17 00:00:00 2001 From: Anatoly Serdtcev Date: Wed, 23 Jan 2019 20:43:47 +0300 Subject: [PATCH] [indexer] Fix for review --- indexer/indexer_tests/locality_index_test.cpp | 46 +++++++++++++++++++ indexer/locality_index.hpp | 6 ++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/indexer/indexer_tests/locality_index_test.cpp b/indexer/indexer_tests/locality_index_test.cpp index 27a9e56c7e..4e49f8e990 100644 --- a/indexer/indexer_tests/locality_index_test.cpp +++ b/indexer/indexer_tests/locality_index_test.cpp @@ -184,4 +184,50 @@ UNIT_TEST(LocalityIndexTopSizeTest) .size(), 8, ()); } + +UNIT_TEST(LocalityIndexWeightRankTest) +{ + m2::PointD queryPoint{0, 0}; + m2::PointD queryBorder{0, 2}; + + LocalityObjectVector objects; + objects.m_objects.resize(7); + // Enclose query point. + objects.m_objects[0].SetForTesting(1, m2::PointD{0, 0}); + objects.m_objects[1].SetForTesting(2, m2::PointD{0.000001, 0.000001}); // in the same lowermost cell + objects.m_objects[2].SetForTesting(3, m2::RectD{-1, -1, 1, 1}); + // Closest objects. + objects.m_objects[3].SetForTesting(4, m2::RectD{0.5, 0.5, 1.0, 1.0}); + objects.m_objects[4].SetForTesting(5, m2::PointD{1, 0}); + objects.m_objects[5].SetForTesting(6, m2::PointD{1, 1}); + objects.m_objects[6].SetForTesting(7, m2::RectD{1, 0, 1.1, 0.1}); + + vector localityIndex; + MemWriter> writer(localityIndex); + BuildGeoObjectsIndex(objects, writer, "tmp"); + MemReader reader(localityIndex.data(), localityIndex.size()); + + indexer::GeoObjectsIndex index(reader); + + vector> ids; + index.ForClosestToPoint( + [&ids](base::GeoObjectId const & id, auto weight) { ids.push_back({id.GetEncodedId(), weight}); }, + queryPoint, MercatorBounds::DistanceOnEarth(queryPoint, queryBorder), + 7 /* topSize */); + + TEST_EQUAL(ids.size(), 7, ()); + + // Enclose objects: "1", "2", "3". + TEST_EQUAL((map(ids.begin(), ids.begin() + 3)), + (map{{1, 1.0}, {2, 1.0}, {3, 1.0}}), ()); + // "4" + TEST_EQUAL(ids[3].first, 4, ()); + TEST_LESS(ids[3].second, 1.0, ()); + // "5", "6", "7" + TEST_EQUAL((set{ids[4].first, ids[5].first, ids[6].first}), (set{5, 6, 7}), ()); + TEST(ids[4].second < ids[3].second, ()); + TEST(ids[5].second < ids[3].second, ()); + TEST(ids[6].second < ids[3].second, ()); +} + } // namespace diff --git a/indexer/locality_index.hpp b/indexer/locality_index.hpp index c58ce2c251..dc712bcca4 100644 --- a/indexer/locality_index.hpp +++ b/indexer/locality_index.hpp @@ -101,9 +101,11 @@ public: auto const cell = m2::CellId::FromInt64(cellNumber, cellDepth); auto const distance = chebyshevDistance(cell.XY()); - CHECK_GREATER(distance, 1, ()); + auto const cellSize = 2 * cell.Radius(); + auto const roudDistance = (distance + cellSize - 1) / cellSize * cellSize; + CHECK_GREATER(roudDistance, 1, ()); - return 1.0 / distance; + return 1.0 / roudDistance; }; auto insertObject = [&] (int64_t cellNumber, uint64_t storedId) {