try removing virtual functions from grid

This commit is contained in:
Andrey Chulkov 2020-05-07 02:16:00 +03:00
parent 83a71a35a0
commit 602cc0eedd
2 changed files with 26 additions and 40 deletions

View File

@ -11,9 +11,10 @@
namespace DB
{
FinalCell::FinalCell(std::vector<size_t> polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_):
Cell::Cell(std::vector<size_t> polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_):
polygon_ids(std::move(polygon_ids_))
{
is_final = true;
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)
@ -22,15 +23,10 @@ polygon_ids(std::move(polygon_ids_))
});
}
const FinalCell * FinalCell::find(Float64, Float64) const
{
return this;
}
DividedCell::DividedCell(std::vector<std::unique_ptr<ICell>> children_): children(std::move(children_)) {}
const FinalCell * DividedCell::find(Float64 x, Float64 y) const
const Cell * Cell::find(Float64 x, Float64 y) const
{
if (is_final)
return this;
auto x_ratio = x * GridRoot::kSplit;
auto y_ratio = y * GridRoot::kSplit;
auto x_bin = static_cast<int>(x_ratio);
@ -38,6 +34,9 @@ const FinalCell * DividedCell::find(Float64 x, Float64 y) const
return children[y_bin + x_bin * GridRoot::kSplit]->find(x_ratio - x_bin, y_ratio - y_bin);
}
Cell::Cell(std::vector<std::unique_ptr<Cell>> children_): children(std::move(children_)) {}
GridRoot::GridRoot(const size_t min_intersections_, const size_t max_depth_, const std::vector<Polygon> & polygons_):
kMinIntersections(min_intersections_), kMaxDepth(max_depth_), polygons(polygons_) {
setBoundingBox();
@ -46,7 +45,7 @@ kMinIntersections(min_intersections_), kMaxDepth(max_depth_), polygons(polygons_
root = makeCell(min_x, min_y, max_x, max_y, order);
}
const FinalCell * GridRoot::find(Float64 x, Float64 y) const
const Cell * GridRoot::find(Float64 x, Float64 y) const
{
if (x < min_x || x >= max_x)
return nullptr;
@ -55,7 +54,7 @@ const FinalCell * GridRoot::find(Float64 x, Float64 y) const
return root->find((x - min_x) / (max_x - min_x), (y - min_y) / (max_y - min_y));
}
std::unique_ptr<ICell> GridRoot::makeCell(Float64 current_min_x, Float64 current_min_y, Float64 current_max_x, Float64 current_max_y, std::vector<size_t> possible_ids, size_t depth)
std::unique_ptr<Cell> GridRoot::makeCell(Float64 current_min_x, Float64 current_min_y, Float64 current_max_x, Float64 current_max_y, std::vector<size_t> possible_ids, size_t depth)
{
auto current_box = Box(Point(current_min_x, current_min_y), Point(current_max_x, current_max_y));
possible_ids.erase(std::remove_if(possible_ids.begin(), possible_ids.end(), [&](const auto id)
@ -63,10 +62,10 @@ std::unique_ptr<ICell> GridRoot::makeCell(Float64 current_min_x, Float64 current
return !bg::intersects(current_box, polygons[id]);
}), possible_ids.end());
if (possible_ids.size() <= kMinIntersections || depth++ == kMaxDepth)
return std::make_unique<FinalCell>(possible_ids, polygons, current_box);
return std::make_unique<Cell>(possible_ids, polygons, current_box);
auto x_shift = (current_max_x - current_min_x) / kSplit;
auto y_shift = (current_max_y - current_min_y) / kSplit;
std::vector<std::unique_ptr<ICell>> children;
std::vector<std::unique_ptr<Cell>> children;
children.resize(kSplit * kSplit);
std::vector<ThreadFromGlobalPool> threads;
for (size_t i = 0; i < kSplit; current_min_x += x_shift, ++i)
@ -85,7 +84,7 @@ std::unique_ptr<ICell> GridRoot::makeCell(Float64 current_min_x, Float64 current
}
for (auto & thread : threads)
thread.join();
return std::make_unique<DividedCell>(std::move(children));
return std::make_unique<Cell>(std::move(children));
}
void GridRoot::setBoundingBox()

View File

@ -19,34 +19,21 @@ using Point = IPolygonDictionary::Point;
using Polygon = IPolygonDictionary::Polygon;
using Box = bg::model::box<IPolygonDictionary::Point>;
class FinalCell;
class ICell
class Cell
{
public:
virtual ~ICell() = default;
[[nodiscard]] virtual const FinalCell * find(Float64 x, Float64 y) const = 0;
};
class DividedCell : public ICell
{
public:
explicit DividedCell(std::vector<std::unique_ptr<ICell>> children_);
[[nodiscard]] const FinalCell * find(Float64 x, Float64 y) const override;
private:
std::vector<std::unique_ptr<ICell>> children;
};
class FinalCell : public ICell
{
public:
explicit FinalCell(std::vector<size_t> polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_);
explicit Cell(std::vector<size_t> polygon_ids_, const std::vector<Polygon> & polygons_, const Box & box_);
std::vector<size_t> polygon_ids;
std::vector<uint8_t> is_covered_by;
explicit Cell(std::vector<std::unique_ptr<Cell>> children_);
[[nodiscard]] const Cell * find(Float64 x, Float64 y) const override;
private:
[[nodiscard]] const FinalCell * find(Float64 x, Float64 y) const override;
std::vector<std::unique_ptr<Cell>> children;
bool is_final = false;
};
/** A recursively built grid containing information about polygons intersecting each cell.
@ -56,21 +43,21 @@ private:
* intersects a small enough number of polygons or the maximum allowed depth is exceeded.
* Both of these parameters are set in the constructor.
*/
class GridRoot : public ICell
class GridRoot
{
public:
GridRoot(size_t min_intersections_, size_t max_depth_, const std::vector<Polygon> & polygons_);
/** Retrieves the cell containing a given point.
* A null pointer is returned when the point falls outside the grid.
*/
[[nodiscard]] const FinalCell * find(Float64 x, Float64 y) const override;
[[nodiscard]] const Cell * find(Float64 x, Float64 y) const override;
/** When a cell is split every side is split into kSplit pieces producing kSplit * kSplit equal smaller cells. */
static constexpr size_t kSplit = 4;
static constexpr size_t kMultiProcessingDepth = 2;
private:
std::unique_ptr<ICell> root = nullptr;
std::unique_ptr<Cell> root = nullptr;
Float64 min_x = 0, min_y = 0;
Float64 max_x = 0, max_y = 0;
const size_t kMinIntersections;
@ -78,7 +65,7 @@ private:
const std::vector<Polygon> & polygons;
std::unique_ptr<ICell> makeCell(Float64 min_x, Float64 min_y, Float64 max_x, Float64 max_y, std::vector<size_t> intersecting_ids, size_t depth = 0);
std::unique_ptr<Cell> makeCell(Float64 min_x, Float64 min_y, Float64 max_x, Float64 max_y, std::vector<size_t> intersecting_ids, size_t depth = 0);
void setBoundingBox();
};