diff --git a/include/engine/datafacade/internal_datafacade.hpp b/include/engine/datafacade/internal_datafacade.hpp index acdd79b78..51bc052e5 100644 --- a/include/engine/datafacade/internal_datafacade.hpp +++ b/include/engine/datafacade/internal_datafacade.hpp @@ -69,7 +69,7 @@ class InternalDataFacade final : public BaseDataFacade std::unique_ptr m_query_graph; std::string m_timestamp; - std::shared_ptr::vector> m_coordinate_list; + util::ShM::vector m_coordinate_list; util::ShM::vector m_via_node_list; util::ShM::vector m_name_ID_list; util::ShM::vector m_turn_instruction_list; @@ -139,12 +139,12 @@ class InternalDataFacade final : public BaseDataFacade extractor::QueryNode current_node; unsigned number_of_coordinates = 0; nodes_input_stream.read((char *)&number_of_coordinates, sizeof(unsigned)); - m_coordinate_list = std::make_shared>(number_of_coordinates); + m_coordinate_list.resize(number_of_coordinates); for (unsigned i = 0; i < number_of_coordinates; ++i) { nodes_input_stream.read((char *)¤t_node, sizeof(extractor::QueryNode)); - m_coordinate_list->at(i) = util::Coordinate(current_node.lon, current_node.lat); - BOOST_ASSERT(m_coordinate_list->at(i).IsValid()); + m_coordinate_list[i] = util::Coordinate(current_node.lon, current_node.lat); + BOOST_ASSERT(m_coordinate_list[i].IsValid()); } boost::filesystem::ifstream edges_input_stream(edges_file, std::ios::binary); @@ -253,7 +253,7 @@ class InternalDataFacade final : public BaseDataFacade void LoadRTree() { - BOOST_ASSERT_MSG(!m_coordinate_list->empty(), "coordinates must be loaded before r-tree"); + BOOST_ASSERT_MSG(!m_coordinate_list.empty(), "coordinates must be loaded before r-tree"); m_static_rtree.reset(new InternalRTree(ram_index_path, file_index_path, m_coordinate_list)); m_geospatial_query.reset( @@ -364,7 +364,7 @@ class InternalDataFacade final : public BaseDataFacade // node and edge information access util::Coordinate GetCoordinateOfNode(const unsigned id) const override final { - return m_coordinate_list->at(id); + return m_coordinate_list[id]; } extractor::guidance::TurnInstruction diff --git a/include/engine/datafacade/shared_datafacade.hpp b/include/engine/datafacade/shared_datafacade.hpp index 1fc94829b..05c6a74e9 100644 --- a/include/engine/datafacade/shared_datafacade.hpp +++ b/include/engine/datafacade/shared_datafacade.hpp @@ -72,7 +72,7 @@ class SharedDataFacade final : public BaseDataFacade std::string m_timestamp; extractor::ProfileProperties *m_profile_properties; - std::shared_ptr::vector> m_coordinate_list; + util::ShM::vector m_coordinate_list; util::ShM::vector m_via_node_list; util::ShM::vector m_name_ID_list; util::ShM::vector m_turn_instruction_list; @@ -119,7 +119,7 @@ class SharedDataFacade final : public BaseDataFacade void LoadRTree() { - BOOST_ASSERT_MSG(!m_coordinate_list->empty(), "coordinates must be loaded before r-tree"); + BOOST_ASSERT_MSG(!m_coordinate_list.empty(), "coordinates must be loaded before r-tree"); auto tree_ptr = data_layout->GetBlockPtr( shared_memory, storage::SharedDataLayout::R_SEARCH_TREE); @@ -149,8 +149,7 @@ class SharedDataFacade final : public BaseDataFacade { auto coordinate_list_ptr = data_layout->GetBlockPtr( shared_memory, storage::SharedDataLayout::COORDINATE_LIST); - m_coordinate_list = util::make_unique::vector>( - coordinate_list_ptr, + m_coordinate_list.reset(coordinate_list_ptr, data_layout->num_entries[storage::SharedDataLayout::COORDINATE_LIST]); auto travel_mode_list_ptr = data_layout->GetBlockPtr( @@ -353,13 +352,10 @@ class SharedDataFacade final : public BaseDataFacade LoadRTree(); util::SimpleLogger().Write() << "number of geometries: " - << m_coordinate_list->size(); - for (unsigned i = 0; i < m_coordinate_list->size(); ++i) + << m_coordinate_list.size(); + for (unsigned i = 0; i < m_coordinate_list.size(); ++i) { - if (!GetCoordinateOfNode(i).IsValid()) - { - util::SimpleLogger().Write() << "coordinate " << i << " not valid"; - } + BOOST_ASSERT(GetCoordinateOfNode(i).IsValid()); } } util::SimpleLogger().Write(logDEBUG) << "Releasing exclusive lock"; @@ -412,7 +408,7 @@ class SharedDataFacade final : public BaseDataFacade // node and edge information access util::Coordinate GetCoordinateOfNode(const NodeID id) const override final { - return m_coordinate_list->at(id); + return m_coordinate_list[id]; } virtual void GetUncompressedGeometry(const EdgeID id, diff --git a/include/engine/geospatial_query.hpp b/include/engine/geospatial_query.hpp index 16f0680c6..5d4f8698c 100644 --- a/include/engine/geospatial_query.hpp +++ b/include/engine/geospatial_query.hpp @@ -22,7 +22,7 @@ namespace engine // Implements complex queries on top of an RTree and builds PhantomNodes from it. // -// Only holds a weak reference on the RTree! +// Only holds a weak reference on the RTree and coordinates! template class GeospatialQuery { using EdgeData = typename RTreeT::EdgeData; @@ -31,9 +31,9 @@ template class GeospatialQuery public: GeospatialQuery(RTreeT &rtree_, - std::shared_ptr coordinates_, + const CoordinateList &coordinates_, DataFacadeT &datafacade_) - : rtree(rtree_), coordinates(std::move(coordinates_)), datafacade(datafacade_) + : rtree(rtree_), coordinates(coordinates_), datafacade(datafacade_) { } @@ -360,7 +360,7 @@ template class GeospatialQuery double ratio; const auto current_perpendicular_distance = util::coordinate_calculation::perpendicularDistance( - coordinates->at(data.u), coordinates->at(data.v), input_coordinate, + coordinates[data.u], coordinates[data.v], input_coordinate, point_on_segment, ratio); // Find the node-based-edge that this belongs to, and directly @@ -442,7 +442,7 @@ template class GeospatialQuery !segment.data.reverse_segment_id.enabled); const double forward_edge_bearing = util::coordinate_calculation::bearing( - coordinates->at(segment.data.u), coordinates->at(segment.data.v)); + coordinates[segment.data.u], coordinates[segment.data.v]); const double backward_edge_bearing = (forward_edge_bearing + 180) > 360 ? (forward_edge_bearing - 180) @@ -460,7 +460,7 @@ template class GeospatialQuery } RTreeT &rtree; - const std::shared_ptr coordinates; + const CoordinateList &coordinates; DataFacadeT &datafacade; }; } diff --git a/include/util/static_rtree.hpp b/include/util/static_rtree.hpp index a551e1925..534f6e4f0 100644 --- a/include/util/static_rtree.hpp +++ b/include/util/static_rtree.hpp @@ -116,7 +116,7 @@ class StaticRTree }; typename ShM::vector m_search_tree; - std::shared_ptr m_coordinate_list; + const CoordinateListT& m_coordinate_list; boost::iostreams::mapped_file_source m_leaves_region; // read-only view of leaves @@ -132,6 +132,7 @@ class StaticRTree const std::string &tree_node_filename, const std::string &leaf_node_filename, const std::vector &coordinate_list) + : m_coordinate_list(coordinate_list) { const uint64_t element_count = input_data_vector.size(); std::vector input_wrapper_vector(element_count); @@ -140,7 +141,7 @@ class StaticRTree tbb::parallel_for( tbb::blocked_range(0, element_count), [&input_data_vector, &input_wrapper_vector, - &coordinate_list](const tbb::blocked_range &range) + this](const tbb::blocked_range &range) { for (uint64_t element_counter = range.begin(), end = range.end(); element_counter != end; ++element_counter) @@ -151,11 +152,11 @@ class StaticRTree EdgeDataT const ¤t_element = input_data_vector[element_counter]; // Get Hilbert-Value for centroid in mercartor projection - BOOST_ASSERT(current_element.u < coordinate_list.size()); - BOOST_ASSERT(current_element.v < coordinate_list.size()); + BOOST_ASSERT(current_element.u < m_coordinate_list.size()); + BOOST_ASSERT(current_element.v < m_coordinate_list.size()); Coordinate current_centroid = coordinate_calculation::centroid( - coordinate_list[current_element.u], coordinate_list[current_element.v]); + m_coordinate_list[current_element.u], m_coordinate_list[current_element.v]); current_centroid.lat = FixedLatitude(COORDINATE_PRECISION * web_mercator::latToY(toFloating(current_centroid.lat))); @@ -194,7 +195,7 @@ class StaticRTree // generate tree node that resemble the objects in leaf and store it for next level InitializeMBRectangle(current_node.minimum_bounding_rectangle, current_leaf.objects, - current_leaf.object_count, coordinate_list); + current_leaf.object_count, m_coordinate_list); current_node.child_is_on_disk = true; current_node.children[0] = tree_nodes_in_level.size(); tree_nodes_in_level.emplace_back(current_node); @@ -275,11 +276,10 @@ class StaticRTree explicit StaticRTree(const boost::filesystem::path &node_file, const boost::filesystem::path &leaf_file, - const std::shared_ptr coordinate_list) + const CoordinateListT& coordinate_list) + : m_coordinate_list(coordinate_list) { // open tree node file and load into RAM. - m_coordinate_list = coordinate_list; - if (!boost::filesystem::exists(node_file)) { throw exception("ram index file does not exist"); @@ -305,9 +305,9 @@ class StaticRTree explicit StaticRTree(TreeNode *tree_node_ptr, const uint64_t number_of_nodes, const boost::filesystem::path &leaf_file, - std::shared_ptr coordinate_list) + const CoordinateListT& coordinate_list) : m_search_tree(tree_node_ptr, number_of_nodes) - , m_coordinate_list(std::move(coordinate_list)) + , m_coordinate_list(coordinate_list) { MapLeafNodesFile(leaf_file); } @@ -355,14 +355,14 @@ class StaticRTree // we don't need to project the coordinates here, // because we use the unprojected rectangle to test against - const Rectangle bbox{std::min((*m_coordinate_list)[current_edge.u].lon, - (*m_coordinate_list)[current_edge.v].lon), - std::max((*m_coordinate_list)[current_edge.u].lon, - (*m_coordinate_list)[current_edge.v].lon), - std::min((*m_coordinate_list)[current_edge.u].lat, - (*m_coordinate_list)[current_edge.v].lat), - std::max((*m_coordinate_list)[current_edge.u].lat, - (*m_coordinate_list)[current_edge.v].lat)}; + const Rectangle bbox{std::min(m_coordinate_list[current_edge.u].lon, + m_coordinate_list[current_edge.v].lon), + std::max(m_coordinate_list[current_edge.u].lon, + m_coordinate_list[current_edge.v].lon), + std::min(m_coordinate_list[current_edge.u].lat, + m_coordinate_list[current_edge.v].lat), + std::max(m_coordinate_list[current_edge.u].lat, + m_coordinate_list[current_edge.v].lat)}; // use the _unprojected_ input rectangle here if (bbox.Intersects(search_rectangle)) @@ -481,8 +481,8 @@ class StaticRTree for (const auto i : irange(0u, current_leaf_node.object_count)) { const auto ¤t_edge = current_leaf_node.objects[i]; - const auto projected_u = web_mercator::fromWGS84((*m_coordinate_list)[current_edge.u]); - const auto projected_v = web_mercator::fromWGS84((*m_coordinate_list)[current_edge.v]); + const auto projected_u = web_mercator::fromWGS84(m_coordinate_list[current_edge.u]); + const auto projected_v = web_mercator::fromWGS84(m_coordinate_list[current_edge.v]); FloatCoordinate projected_nearest; std::tie(std::ignore, projected_nearest) = diff --git a/src/extractor/extractor.cpp b/src/extractor/extractor.cpp index de2415079..c744c38f4 100644 --- a/src/extractor/extractor.cpp +++ b/src/extractor/extractor.cpp @@ -563,9 +563,9 @@ void Extractor::BuildRTree(std::vector node_based_edge_list, node_based_edge_list.resize(new_size); TIMER_START(construction); - util::StaticRTree rtree(node_based_edge_list, config.rtree_nodes_output_path, - config.rtree_leafs_output_path, - internal_to_external_node_map); + util::StaticRTree> rtree( + node_based_edge_list, config.rtree_nodes_output_path, config.rtree_leafs_output_path, + internal_to_external_node_map); TIMER_STOP(construction); util::SimpleLogger().Write() << "finished r-tree construction in " << TIMER_SEC(construction) diff --git a/unit_tests/util/static_rtree.cpp b/unit_tests/util/static_rtree.cpp index 78b3264f0..a20d0dbbc 100644 --- a/unit_tests/util/static_rtree.cpp +++ b/unit_tests/util/static_rtree.cpp @@ -52,7 +52,7 @@ static const int32_t WORLD_MAX_LON = 180 * COORDINATE_PRECISION; template class LinearSearchNN { public: - LinearSearchNN(const std::shared_ptr> &coords, + LinearSearchNN(const std::vector &coords, const std::vector &edges) : coords(coords), edges(edges) { @@ -67,9 +67,9 @@ template class LinearSearchNN const DataT &rhs) { using web_mercator::fromWGS84; const auto lhs_result = coordinate_calculation::projectPointOnSegment( - fromWGS84(coords->at(lhs.u)), fromWGS84(coords->at(lhs.v)), projected_input); + fromWGS84(coords[lhs.u]), fromWGS84(coords[lhs.v]), projected_input); const auto rhs_result = coordinate_calculation::projectPointOnSegment( - fromWGS84(coords->at(rhs.u)), fromWGS84(coords->at(rhs.v)), projected_input); + fromWGS84(coords[rhs.u]), fromWGS84(coords[rhs.v]), projected_input); const auto lhs_squared_dist = coordinate_calculation::squaredEuclideanDistance( lhs_result.second, projected_input); const auto rhs_squared_dist = coordinate_calculation::squaredEuclideanDistance( @@ -85,7 +85,7 @@ template class LinearSearchNN } private: - const std::shared_ptr> &coords; + const std::vector &coords; const std::vector &edges; }; @@ -105,7 +105,7 @@ template struct RandomGraphFixture } }; - RandomGraphFixture() : coords(std::make_shared>()) + RandomGraphFixture() { std::mt19937 g(RANDOM_SEED); @@ -116,10 +116,10 @@ template struct RandomGraphFixture { int lon = lon_udist(g); int lat = lat_udist(g); - coords->emplace_back(Coordinate(FixedLongitude(lon), FixedLatitude(lat))); + coords.emplace_back(Coordinate(FixedLongitude(lon), FixedLatitude(lat))); } - std::uniform_int_distribution<> edge_udist(0, coords->size() - 1); + std::uniform_int_distribution<> edge_udist(0, coords.size() - 1); std::unordered_set, TupleHash> used_edges; @@ -138,7 +138,7 @@ template struct RandomGraphFixture } } - std::shared_ptr> coords; + std::vector coords; std::vector edges; }; @@ -146,12 +146,11 @@ struct GraphFixture { GraphFixture(const std::vector> &input_coords, const std::vector> &input_edges) - : coords(std::make_shared>()) { for (unsigned i = 0; i < input_coords.size(); i++) { - coords->emplace_back(input_coords[i].first, input_coords[i].second); + coords.emplace_back(input_coords[i].first, input_coords[i].second); } for (const auto &pair : input_edges) @@ -169,7 +168,7 @@ struct GraphFixture } } - std::shared_ptr> coords; + std::vector coords; std::vector edges; }; @@ -189,13 +188,13 @@ typedef RandomGraphFixture<10, 30> TestRandomGraphFixture_10_30; template void simple_verify_rtree(RTreeT &rtree, - const std::shared_ptr> &coords, + const std::vector &coords, const std::vector &edges) { for (const auto &e : edges) { - const Coordinate &pu = coords->at(e.u); - const Coordinate &pv = coords->at(e.v); + const Coordinate &pu = coords[e.u]; + const Coordinate &pv = coords[e.v]; auto result_u = rtree.Nearest(pu, 1); auto result_v = rtree.Nearest(pv, 1); BOOST_CHECK(result_u.size() == 1 && result_v.size() == 1); @@ -248,7 +247,7 @@ void build_rtree(const std::string &prefix, nodes_path = prefix + ".ramIndex"; leaves_path = prefix + ".fileIndex"; - RTreeT r(fixture->edges, nodes_path, leaves_path, *fixture->coords); + RTreeT r(fixture->edges, nodes_path, leaves_path, fixture->coords); } template @@ -261,7 +260,7 @@ void construction_test(const std::string &prefix, FixtureT *fixture) LinearSearchNN lsnn(fixture->coords, fixture->edges); simple_verify_rtree(rtree, fixture->coords, fixture->edges); - sampling_verify_rtree(rtree, lsnn, *fixture->coords, 100); + sampling_verify_rtree(rtree, lsnn, fixture->coords, 100); } BOOST_FIXTURE_TEST_CASE(construct_tiny, TestRandomGraphFixture_10_30)