diff --git a/src/Dictionaries/PolygonDictionaryImplementations.cpp b/src/Dictionaries/PolygonDictionaryImplementations.cpp index 4261d11fefc..3acb82e9191 100644 --- a/src/Dictionaries/PolygonDictionaryImplementations.cpp +++ b/src/Dictionaries/PolygonDictionaryImplementations.cpp @@ -76,22 +76,25 @@ std::shared_ptr GridPolygonDictionary::clone() const bool GridPolygonDictionary::find(const Point &point, size_t & id) const { - bool found = false; - auto cell = grid.find(point.get<0>(), point.get<1>()); + auto cell = grid.find(point.x(), point.y()); if (cell) { for (size_t i = 0; i < (cell->polygon_ids).size(); ++i) { const auto & candidate = (cell->polygon_ids)[i]; - if ((cell->is_covered_by)[i] || bg::covered_by(point, polygons[candidate])) + if (bg::covered_by(point, polygons[candidate])) { - found = true; id = candidate; - break; + return true; } } + if (cell->first_covered != FinalCell::kNone) + { + id = cell->first_covered; + return true; + } } - return found; + return false; } SmartPolygonDictionary::SmartPolygonDictionary( @@ -128,42 +131,26 @@ std::shared_ptr SmartPolygonDictionary::clone() const bool SmartPolygonDictionary::find(const Point & point, size_t & id) const { - /* - bool found = false; - double area = 0; - for (size_t i = 0; i < polygons.size(); ++i) - { - size_t unused; - if (buckets[i].find(point, unused)) - { - double new_area = areas[i]; - if (!found || new_area < area) - { - found = true; - id = i; - area = new_area; - } - } - } - return found; - */ - bool found = false; - auto cell = grid.find(point.get<0>(), point.get<1>()); + auto cell = grid.find(point.x(), point.y()); if (cell) { for (size_t i = 0; i < (cell->polygon_ids).size(); ++i) { const auto & candidate = (cell->polygon_ids)[i]; size_t unused; - if ((cell->is_covered_by)[i] || buckets[candidate].find(point, unused)) + if (buckets[candidate].find(point, unused)) { - found = true; id = candidate; - break; + return true; } } + if (cell->first_covered != FinalCell::kNone) + { + id = cell->first_covered; + return true; + } } - return found; + return false; } OneBucketPolygonDictionary::OneBucketPolygonDictionary( @@ -194,12 +181,13 @@ std::shared_ptr OneBucketPolygonDictionary::clone() con bool OneBucketPolygonDictionary::find(const Point & point, size_t & id) const { auto cell = index.find(point.x(), point.y()); - if (cell != nullptr) { - if (!cell->corresponding_ids.empty() && cell->index.find(point, id)) { + if (cell) { + if (!(cell->corresponding_ids).empty() && cell->index.find(point, id)) { id = cell->corresponding_ids[id]; return true; } - if (cell->first_covered != static_cast(-1)) { + if (cell->first_covered != FinalCellWithSlabs::kNone) + { id = cell->first_covered; return true; } diff --git a/src/Dictionaries/PolygonDictionaryUtils.cpp b/src/Dictionaries/PolygonDictionaryUtils.cpp index 2a7e0fa5fb5..ab59df3000c 100644 --- a/src/Dictionaries/PolygonDictionaryUtils.cpp +++ b/src/Dictionaries/PolygonDictionaryUtils.cpp @@ -11,15 +11,19 @@ namespace DB { -FinalCell::FinalCell(const std::vector & polygon_ids_, const std::vector & polygons_, const Box & box_): -polygon_ids(polygon_ids_) +FinalCell::FinalCell(const std::vector & polygon_ids_, const std::vector & polygons_, const Box & box_) { Polygon tmp_poly; bg::convert(box_, tmp_poly); - std::transform(polygon_ids.begin(), polygon_ids.end(), std::back_inserter(is_covered_by), [&](const auto id) + for (const auto id : polygon_ids_) { - return bg::covered_by(tmp_poly, polygons_[id]); - }); + if (bg::covered_by(tmp_poly, polygons_[id])) + { + first_covered = id; + break; + } + polygon_ids.push_back(id); + } } const FinalCell * FinalCell::find(Coord, Coord) const @@ -32,7 +36,8 @@ FinalCellWithSlabs::FinalCellWithSlabs(const std::vector & polygon_ids_, Polygon tmp_poly; bg::convert(box_, tmp_poly); std::vector intersections; - for (const auto id : polygon_ids_) { + for (const auto id : polygon_ids_) + { if (bg::covered_by(tmp_poly, polygons_[id])) { first_covered = id; diff --git a/src/Dictionaries/PolygonDictionaryUtils.h b/src/Dictionaries/PolygonDictionaryUtils.h index 6e001d02430..46556a16754 100644 --- a/src/Dictionaries/PolygonDictionaryUtils.h +++ b/src/Dictionaries/PolygonDictionaryUtils.h @@ -124,7 +124,9 @@ class FinalCell : public ICell public: explicit FinalCell(const std::vector & polygon_ids_, const std::vector & polygons_, const Box & box_); std::vector polygon_ids; - std::vector is_covered_by; + size_t first_covered = kNone; + + static constexpr size_t kNone = -1; private: [[nodiscard]] const FinalCell * find(Coord x, Coord y) const override; @@ -137,7 +139,9 @@ public: BucketsPolygonIndex index; std::vector corresponding_ids; - size_t first_covered = -1; + size_t first_covered = kNone; + + static constexpr size_t kNone = -1; private: [[nodiscard]] const FinalCellWithSlabs * find(Coord x, Coord y) const override;