From a73d2f349d653586cd328532cfe2eaa11a312c50 Mon Sep 17 00:00:00 2001 From: Arthur Petukhovsky Date: Wed, 6 May 2020 19:15:54 +0300 Subject: [PATCH 1/4] Try HashMap from common lib --- dbms/src/Dictionaries/PolygonDictionaryUtils.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp index 12a2f1edb56..0f387f67b53 100644 --- a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp +++ b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -343,8 +344,8 @@ bool BucketsPolygonIndex::find(const Point & point, size_t & id) const } /** point is considired inside when ray down from point crosses odd number of edges */ - std::map is_inside; - std::map on_the_edge; + HashMap is_inside; + HashMap on_the_edge; size_t pos = std::upper_bound(this->sorted_x.begin() + 1, this->sorted_x.end() - 1, x) - this->sorted_x.begin() - 1; From bdd7c1014e768654fd2e43c36e11492818817da3 Mon Sep 17 00:00:00 2001 From: Andrey Chulkov Date: Wed, 6 May 2020 19:32:07 +0300 Subject: [PATCH 2/4] compilation error fix --- dbms/src/Dictionaries/PolygonDictionaryUtils.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp index 0f387f67b53..835c6c950f2 100644 --- a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp +++ b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp @@ -394,20 +394,20 @@ bool BucketsPolygonIndex::find(const Point & point, size_t & id) const } while (pos != 0); bool found = false; - for (auto & [polygon_id, inside] : is_inside) + for (const auto & item : is_inside) { - if (inside) + if (item.getMapped()) { found = true; - id = polygon_id; + id = item.getKey(); } } - for (auto & [polygon_id, is_edge] : on_the_edge) + for (const auto & item : on_the_edge) { - if (is_edge) + if (item.getMapped()) { found = true; - id = polygon_id; + id = item.getKey(); } } From 531a9981ab70935044c2a974d0d3a4654f513dce Mon Sep 17 00:00:00 2001 From: Arthur Petukhovsky Date: Wed, 6 May 2020 19:45:03 +0300 Subject: [PATCH 3/4] Replate parity map with vector --- .../Dictionaries/PolygonDictionaryUtils.cpp | 46 +++++++++++-------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp index 835c6c950f2..ad11a6babf2 100644 --- a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp +++ b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp @@ -3,7 +3,6 @@ #include #include -#include #include #include @@ -327,6 +326,18 @@ bool BucketsPolygonIndex::Edge::compare2(const Edge & a, const Edge & b) return a.polygon_id < b.polygon_id; } +namespace +{ + inline void update_result(bool & found, size_t & id, const size_t & new_id) + { + if (!found || new_id < id) + { + found = true; + id = new_id; + } + } +} + bool BucketsPolygonIndex::find(const Point & point, size_t & id) const { /** TODO: maybe we should check for vertical line? */ @@ -343,13 +354,14 @@ bool BucketsPolygonIndex::find(const Point & point, size_t & id) const return false; } + bool found = false; + /** point is considired inside when ray down from point crosses odd number of edges */ - HashMap is_inside; - HashMap on_the_edge; + std::vector intersections; size_t pos = std::upper_bound(this->sorted_x.begin() + 1, this->sorted_x.end() - 1, x) - this->sorted_x.begin() - 1; - /** pos += n */ + /** Here we doing: pos += n */ pos += this->edges_index_tree.size() / 2; do { @@ -367,7 +379,8 @@ bool BucketsPolygonIndex::find(const Point & point, size_t & id) const { if (l.x() == x && y >= l.y() && y <= r.y()) { - on_the_edge[polygon_id] = true; + /** point is on the edge */ + update_result(found, id, polygon_id); } continue; } @@ -385,29 +398,22 @@ bool BucketsPolygonIndex::find(const Point & point, size_t & id) const } if (edge_y == y) { - on_the_edge[polygon_id] = true; + /** point is on the edge */ + update_result(found, id, polygon_id); } - is_inside[polygon_id] ^= true; + intersections.emplace_back(polygon_id); } pos >>= 1; } while (pos != 0); - bool found = false; - for (const auto & item : is_inside) + std::sort(intersections.begin(), intersections.end()); + for (size_t i = 0; i < intersections.size(); i += 2) { - if (item.getMapped()) + if (i + 1 == intersections.size() || intersections[i] != intersections[i + 1]) { - found = true; - id = item.getKey(); - } - } - for (const auto & item : on_the_edge) - { - if (item.getMapped()) - { - found = true; - id = item.getKey(); + update_result(found, id, intersections[i]); + break; } } From a4acc49e6ebd38409ee17e56aa1c55463186aa8e Mon Sep 17 00:00:00 2001 From: Andrei Chulkov Date: Wed, 6 May 2020 20:29:07 +0300 Subject: [PATCH 4/4] add reserve --- contrib/base64 | 2 +- contrib/poco | 2 +- dbms/src/Dictionaries/PolygonDictionaryUtils.cpp | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/contrib/base64 b/contrib/base64 index 5257626d2be..95ba56a9b04 160000 --- a/contrib/base64 +++ b/contrib/base64 @@ -1 +1 @@ -Subproject commit 5257626d2be17a3eb23f79be17fe55ebba394ad2 +Subproject commit 95ba56a9b041f9933f5cd2bbb2ee4e083468c20a diff --git a/contrib/poco b/contrib/poco index 1f3e4638f25..860574c9398 160000 --- a/contrib/poco +++ b/contrib/poco @@ -1 +1 @@ -Subproject commit 1f3e4638f250ad4d028a2499af20d4185463e07d +Subproject commit 860574c93980d887a89df141edd9ca2fb0024fa3 diff --git a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp index ad11a6babf2..78ff76b3e33 100644 --- a/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp +++ b/dbms/src/Dictionaries/PolygonDictionaryUtils.cpp @@ -358,6 +358,7 @@ bool BucketsPolygonIndex::find(const Point & point, size_t & id) const /** point is considired inside when ray down from point crosses odd number of edges */ std::vector intersections; + intersections.reserve(10); size_t pos = std::upper_bound(this->sorted_x.begin() + 1, this->sorted_x.end() - 1, x) - this->sorted_x.begin() - 1;