From c7e8f2a3e3b06409b2a8627935570af43ef1dace Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 9 Aug 2017 15:40:58 +0300 Subject: [PATCH] Review fixes. --- search/features_layer_matcher.hpp | 3 +-- search/point_rect_matcher.hpp | 9 +++++---- search/search_tests/segment_tree_tests.cpp | 6 +++--- search/segment_tree.cpp | 2 +- search/segment_tree.hpp | 14 ++++++++++---- 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/search/features_layer_matcher.hpp b/search/features_layer_matcher.hpp index 35af53ad0f..0d2392e0ac 100644 --- a/search/features_layer_matcher.hpp +++ b/search/features_layer_matcher.hpp @@ -143,8 +143,7 @@ private: } } - PointRectMatcher matcher; - matcher.Match(poiCenters, buildingRects, [&](size_t poiId, size_t buildingId) { + PointRectMatcher::Match(poiCenters, buildingRects, [&](size_t poiId, size_t buildingId) { ASSERT_LESS(poiId, pois.size(), ()); ASSERT_LESS(buildingId, buildings.size(), ()); fn(pois[poiId], buildings[buildingId]); diff --git a/search/point_rect_matcher.hpp b/search/point_rect_matcher.hpp index b23c439a76..e72daa74e2 100644 --- a/search/point_rect_matcher.hpp +++ b/search/point_rect_matcher.hpp @@ -31,13 +31,14 @@ public: size_t m_id = 0; }; - // Finds matching between |points| and |rects|, calls |fn| on - // matched pairs. + // For each point tries to find a rect containing the point. If + // there are several rects containing the point, selects an + // arbitrary one. Calls |fn| on matched pairs (point-id, rect-id). // // Complexity: O(n * log(n)), where n = |points| + |rects|. template - void Match(std::vector const & points, std::vector const & rects, - Fn && fn) + static void Match(std::vector const & points, std::vector const & rects, + Fn && fn) { std::vector events; events.reserve(points.size() + 2 * rects.size()); diff --git a/search/search_tests/segment_tree_tests.cpp b/search/search_tests/segment_tree_tests.cpp index 3ebd8f76ee..9e28a574b0 100644 --- a/search/search_tests/segment_tree_tests.cpp +++ b/search/search_tests/segment_tree_tests.cpp @@ -40,9 +40,9 @@ UNIT_TEST(SegmentTree_Smoke) UNIT_TEST(SegmentTree_Simple) { - vector segments = {Segment(-10000, -10000, 0 /* id */), Segment(-10, -6, 1 /* id */), - Segment(-5, -2, 2 /* id */)}; - TEST(is_sorted(segments.begin(), segments.end()), ()); + vector segments = {Segment(-10000 /* from */, -10000 /* to */, 0 /* id */), + Segment(-10, -6, 1), Segment(-5, -2, 2)}; + CHECK(is_sorted(segments.begin(), segments.end()), ()); SegmentTree tree(segments); diff --git a/search/segment_tree.cpp b/search/segment_tree.cpp index b254cdf318..3cc2f68a02 100644 --- a/search/segment_tree.cpp +++ b/search/segment_tree.cpp @@ -24,8 +24,8 @@ size_t CeilPow2Minus1(size_t n) SegmentTree::SegmentTree(vector const & segments) : m_tree(CeilPow2Minus1(segments.size())) { - BuildTree(0 /* index */, segments, 0 /* left */, m_tree.size() /* right */); ASSERT(is_sorted(segments.begin(), segments.end()), ()); + BuildTree(0 /* index */, segments, 0 /* left */, m_tree.size() /* right */); } void SegmentTree::Add(Segment const & segment) diff --git a/search/segment_tree.hpp b/search/segment_tree.hpp index 8d0cf82e27..adc85a1834 100644 --- a/search/segment_tree.hpp +++ b/search/segment_tree.hpp @@ -15,9 +15,9 @@ namespace search class SegmentTree { public: - double static constexpr kNegativeInfinity = -std::numeric_limits::max(); - double static constexpr kPositiveInfinity = std::numeric_limits::max(); - size_t static constexpr kInvalidId = std::numeric_limits::max(); + auto static constexpr kNegativeInfinity = -std::numeric_limits::max(); + auto static constexpr kPositiveInfinity = std::numeric_limits::max(); + auto static constexpr kInvalidId = std::numeric_limits::max(); struct Segment { @@ -47,8 +47,14 @@ public: struct Node { + // Segment corresponding to the node. Segment m_segment; + + // Maximum value among all right bounds of non-deleted segments in + // the subtree. double m_to = kNegativeInfinity; + + // True when corresponding segment is deleted. bool m_deleted = true; }; @@ -58,7 +64,7 @@ public: void Add(Segment const & segment); void Erase(Segment const & segment); - // Calls |fn| on any segment containing |x|. + // Calls |fn| on arbitrary segment containing |x|. template void Find(double x, Fn && fn) const {