Make edge metrics strongly typed (#6421)

This change takes the existing typedefs for weight, duration and
distance, and makes them proper types, using the existing Alias
functionality.

Primarily this is to prevent bugs where the metrics are switched,
but it also adds additional documentation. For example, it now
makes it clear (despite the naming of variables) that most of the
trip algorithm is running on the duration metric.

I've not made any changes to the casts performed between metrics
and numeric types, they now just more explicit.
This commit is contained in:
Michael Bell
2022-10-28 15:16:12 +01:00
committed by GitHub
parent 16685d0de9
commit 5d468f2897
69 changed files with 922 additions and 686 deletions
@@ -19,14 +19,14 @@ BOOST_AUTO_TEST_CASE(unchanged_collapse_route_result)
PhantomNode target;
source.forward_segment_id = {1, true};
target.forward_segment_id = {6, true};
PathData pathy{0, 2, 2, 3, 4, 5, 2, boost::none};
PathData kathy{0, 1, 1, 2, 3, 4, 1, boost::none};
PathData pathy{0, 2, {2}, {3}, {4}, {5}, 2, boost::none};
PathData kathy{0, 1, {1}, {2}, {3}, {4}, 1, boost::none};
InternalRouteResult one_leg_result;
one_leg_result.unpacked_path_segments = {{pathy, kathy}};
one_leg_result.leg_endpoints = {PhantomEndpoints{source, target}};
one_leg_result.source_traversed_in_reverse = {true};
one_leg_result.target_traversed_in_reverse = {true};
one_leg_result.shortest_path_weight = 50;
one_leg_result.shortest_path_weight = {50};
auto collapsed = CollapseInternalRouteResult(one_leg_result, {true, true});
BOOST_CHECK_EQUAL(one_leg_result.unpacked_path_segments[0].front().turn_via_node,
@@ -39,22 +39,26 @@ BOOST_AUTO_TEST_CASE(two_legs_to_one_leg)
{
// from_edge_based_node, turn_via_node, weight_until_turn, weight_of_turn,
// duration_until_turn, duration_of_turn, datasource_id, turn_edge
PathData pathy{0, 2, 2, 3, 4, 5, 2, boost::none};
PathData kathy{0, 1, 1, 2, 3, 4, 1, boost::none};
PathData cathy{0, 3, 1, 2, 3, 4, 1, boost::none};
PathData pathy{0, 2, {2}, {3}, {4}, {5}, 2, boost::none};
PathData kathy{0, 1, {1}, {2}, {3}, {4}, 1, boost::none};
PathData cathy{0, 3, {1}, {2}, {3}, {4}, 1, boost::none};
PhantomNode node_1;
PhantomNode node_2;
PhantomNode node_3;
node_1.forward_segment_id = {1, true};
node_2.forward_segment_id = {6, true};
node_3.forward_segment_id = {12, true};
node_1.forward_weight = node_2.forward_weight = node_3.forward_weight = {1};
node_1.forward_duration = node_2.forward_duration = node_3.forward_duration = {1};
node_1.forward_distance = node_2.forward_distance = node_3.forward_distance = {1};
InternalRouteResult two_leg_result;
two_leg_result.unpacked_path_segments = {{pathy, kathy}, {kathy, cathy}};
two_leg_result.leg_endpoints = {PhantomEndpoints{node_1, node_2},
PhantomEndpoints{node_2, node_3}};
two_leg_result.source_traversed_in_reverse = {true, false};
two_leg_result.target_traversed_in_reverse = {true, false};
two_leg_result.shortest_path_weight = 80;
two_leg_result.shortest_path_weight = {80};
auto collapsed = CollapseInternalRouteResult(two_leg_result, {true, false, true, true});
BOOST_CHECK_EQUAL(collapsed.unpacked_path_segments.size(), 1);
@@ -70,11 +74,11 @@ BOOST_AUTO_TEST_CASE(two_legs_to_one_leg)
BOOST_AUTO_TEST_CASE(three_legs_to_two_legs)
{
PathData pathy{0, 2, 2, 3, 4, 5, 2, boost::none};
PathData kathy{0, 1, 1, 2, 3, 4, 1, boost::none};
PathData qathy{0, 5, 1, 2, 3, 4, 1, boost::none};
PathData cathy{0, 3, 1, 2, 3, 4, 1, boost::none};
PathData mathy{0, 4, 8, 9, 13, 4, 2, boost::none};
PathData pathy{0, 2, {2}, {3}, {4}, {5}, 2, boost::none};
PathData kathy{0, 1, {1}, {2}, {3}, {4}, 1, boost::none};
PathData qathy{0, 5, {1}, {2}, {3}, {4}, 1, boost::none};
PathData cathy{0, 3, {1}, {2}, {3}, {4}, 1, boost::none};
PathData mathy{0, 4, {8}, {9}, {13}, {4}, 2, boost::none};
PhantomNode node_1;
PhantomNode node_2;
PhantomNode node_3;
@@ -83,6 +87,19 @@ BOOST_AUTO_TEST_CASE(three_legs_to_two_legs)
node_2.forward_segment_id = {6, true};
node_3.forward_segment_id = {12, true};
node_4.forward_segment_id = {18, true};
node_1.forward_weight = node_2.forward_weight = node_3.forward_weight =
node_4.forward_weight = {1};
node_1.forward_duration = node_2.forward_duration = node_3.forward_duration =
node_4.forward_duration = {1};
node_1.forward_distance = node_2.forward_distance = node_3.forward_distance =
node_4.forward_distance = {1};
node_1.reverse_weight = node_2.reverse_weight = node_3.reverse_weight =
node_4.reverse_weight = {1};
node_1.reverse_duration = node_2.reverse_duration = node_3.reverse_duration =
node_4.reverse_duration = {1};
node_1.reverse_distance = node_2.reverse_distance = node_3.reverse_distance =
node_4.reverse_distance = {1};
InternalRouteResult three_leg_result;
three_leg_result.unpacked_path_segments = {std::vector<PathData>{pathy, kathy},
std::vector<PathData>{kathy, qathy, cathy},
@@ -92,7 +109,7 @@ BOOST_AUTO_TEST_CASE(three_legs_to_two_legs)
PhantomEndpoints{node_3, node_4}};
three_leg_result.source_traversed_in_reverse = {true, false, true},
three_leg_result.target_traversed_in_reverse = {true, false, true},
three_leg_result.shortest_path_weight = 140;
three_leg_result.shortest_path_weight = {140};
auto collapsed = CollapseInternalRouteResult(three_leg_result, {true, true, false, true});
BOOST_CHECK_EQUAL(collapsed.unpacked_path_segments.size(), 2);
@@ -114,9 +131,9 @@ BOOST_AUTO_TEST_CASE(three_legs_to_two_legs)
BOOST_AUTO_TEST_CASE(two_legs_to_two_legs)
{
PathData pathy{0, 2, 2, 3, 4, 5, 2, boost::none};
PathData kathy{0, 1, 1, 2, 3, 4, 1, boost::none};
PathData cathy{0, 3, 1, 2, 3, 4, 1, boost::none};
PathData pathy{0, 2, {2}, {3}, {4}, {5}, 2, boost::none};
PathData kathy{0, 1, {1}, {2}, {3}, {4}, 1, boost::none};
PathData cathy{0, 3, {1}, {2}, {3}, {4}, 1, boost::none};
PhantomNode node_1;
PhantomNode node_2;
PhantomNode node_3;
@@ -129,7 +146,7 @@ BOOST_AUTO_TEST_CASE(two_legs_to_two_legs)
PhantomEndpoints{node_2, node_3}};
two_leg_result.source_traversed_in_reverse = {true, false};
two_leg_result.target_traversed_in_reverse = {true, false};
two_leg_result.shortest_path_weight = 80;
two_leg_result.shortest_path_weight = {80};
auto collapsed = CollapseInternalRouteResult(two_leg_result, {true, true, true});
BOOST_CHECK_EQUAL(collapsed.unpacked_path_segments.size(), 2);
+3 -6
View File
@@ -87,14 +87,11 @@ struct ExternalCellStorage
return boost::make_iterator_range((EdgeWeight *)0, (EdgeWeight *)0);
}
auto GetSourceNodes() const
{
return boost::make_iterator_range((EdgeWeight *)0, (EdgeWeight *)0);
}
auto GetSourceNodes() const { return boost::make_iterator_range((NodeID *)0, (NodeID *)0); }
auto GetDestinationNodes() const
{
return boost::make_iterator_range((EdgeWeight *)0, (EdgeWeight *)0);
return boost::make_iterator_range((NodeID *)0, (NodeID *)0);
}
};
@@ -265,7 +262,7 @@ class ContiguousInternalMemoryDataFacade<routing_algorithms::offline::Algorithm>
return {};
}
EdgeWeight GetNodeWeight(const NodeID /*node*/) const { return 0; }
EdgeWeight GetNodeWeight(const NodeID /*node*/) const { return {0}; }
bool IsForwardEdge(const NodeID /*edge*/) const { return true; }