Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding one to one on v5.26.0 #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Unreleased
# 5.26.0
- Changes from 5.25.0
- API:
- FIXED: Allow for special characters in the profile/method as part of the HTTP URL. [#6090](https://github.com/Project-OSRM/osrm-backend/pull/6090)
10 changes: 8 additions & 2 deletions include/engine/api/table_api.hpp
Original file line number Diff line number Diff line change
@@ -70,7 +70,10 @@ class TableAPI final : public BaseAPI
const std::vector<TableCellRef> &fallback_speed_cells,
flatbuffers::FlatBufferBuilder &fb_result) const
{
auto number_of_sources = parameters.sources.size();
auto number_of_sources =
parameters.source_destination_mapping == api::TableParameters::MappingType::OneToOne
? 1
: parameters.sources.size();
auto number_of_destinations = parameters.destinations.size();

auto data_timestamp = facade.GetTimestamp();
@@ -172,7 +175,10 @@ class TableAPI final : public BaseAPI
const std::vector<TableCellRef> &fallback_speed_cells,
util::json::Object &response) const
{
auto number_of_sources = parameters.sources.size();
auto number_of_sources =
parameters.source_destination_mapping == api::TableParameters::MappingType::OneToOne
? 1
: parameters.sources.size();
auto number_of_destinations = parameters.destinations.size();

// symmetric case
26 changes: 26 additions & 0 deletions include/engine/api/table_parameters.hpp
Original file line number Diff line number Diff line change
@@ -81,6 +81,15 @@ struct TableParameters : public BaseParameters

double scale_factor = 1;

enum class MappingType
{
None,
OneToOne,
ManyToMany
};

MappingType source_destination_mapping = MappingType::None;

TableParameters() = default;
template <typename... Args>
TableParameters(std::vector<std::size_t> sources_,
@@ -117,6 +126,23 @@ struct TableParameters : public BaseParameters
{
}

template <typename... Args>
TableParameters(std::vector<std::size_t> sources_,
std::vector<std::size_t> destinations_,
const AnnotationsType annotations_,
double fallback_speed_,
FallbackCoordinateType fallback_coordinate_type_,
double scale_factor_,
MappingType source_destination_mapping_,
Args &&...args_)
: BaseParameters{std::forward<Args>(args_)...}, sources{std::move(sources_)},
destinations{std::move(destinations_)}, fallback_speed{fallback_speed_},
fallback_coordinate_type{fallback_coordinate_type_}, annotations{annotations_},
scale_factor{scale_factor_}, source_destination_mapping{source_destination_mapping_}

{
}

bool IsValid() const
{
if (!BaseParameters::IsValid())
19 changes: 15 additions & 4 deletions include/server/api/table_parameter_grammar.hpp
Original file line number Diff line number Diff line change
@@ -81,10 +81,19 @@ struct TableParametersGrammar : public BaseParametersGrammar<Iterator, Signature

annotations_list = annotations[qi::_val |= qi::_1] % ',';

base_rule = BaseGrammar::base_rule(qi::_r1) |
(qi::lit("annotations=") >
annotations_list[ph::bind(&engine::api::TableParameters::annotations,
qi::_r1) = qi::_1]);
using MappingType = engine::api::TableParameters::MappingType;
source_destination_mapping_type.add("one-to-one", MappingType::OneToOne)(
"many-to-many", MappingType::ManyToMany);

base_rule =
BaseGrammar::base_rule(qi::_r1) |
(qi::lit("annotations=") >
annotations_list[ph::bind(&engine::api::TableParameters::annotations, qi::_r1) =
qi::_1]) |
(qi::lit("source_destination_mapping=") >
source_destination_mapping_type
[ph::bind(&engine::api::TableParameters::source_destination_mapping, qi::_r1) =
qi::_1]);
}

protected:
@@ -99,8 +108,10 @@ struct TableParametersGrammar : public BaseParametersGrammar<Iterator, Signature
qi::rule<Iterator, Signature> destinations_rule;
qi::rule<Iterator, Signature> fallback_speed_rule;
qi::rule<Iterator, Signature> scale_factor_rule;
qi::rule<Iterator, Signature> source_destination_mapping_rule;
qi::rule<Iterator, std::size_t()> size_t_;
qi::symbols<char, engine::api::TableParameters::AnnotationsType> annotations;
qi::symbols<char, engine::api::TableParameters::MappingType> source_destination_mapping_type;
qi::rule<Iterator, engine::api::TableParameters::AnnotationsType()> annotations_list;
qi::symbols<char, engine::api::TableParameters::FallbackCoordinateType>
fallback_coordinate_type;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "osrm",
"version": "5.26.0-unreleased",
"version": "5.26.0",
"private": false,
"description": "The Open Source Routing Machine is a high performance routing engine written in C++14 designed to run on OpenStreetMap data.",
"dependencies": {
65 changes: 61 additions & 4 deletions src/engine/plugins/table.cpp
Original file line number Diff line number Diff line change
@@ -84,8 +84,56 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
bool request_distance = params.annotations & api::TableParameters::AnnotationsType::Distance;
bool request_duration = params.annotations & api::TableParameters::AnnotationsType::Duration;

auto result_tables_pair = algorithms.ManyToManySearch(
snapped_phantoms, params.sources, params.destinations, request_distance);
auto request_mapping_src_to_dst =
params.source_destination_mapping == api::TableParameters::MappingType::OneToOne
? api::TableParameters::MappingType::OneToOne
: api::TableParameters::MappingType::ManyToMany;

if ((params.source_destination_mapping == api::TableParameters::MappingType::OneToOne &&
params.sources.size() <= 0) ||
params.destinations.size() <= 0)
{
return Error("SourceOrDestinationIndicesEmpty",
"source and destination indices should not be empty for one to one mapping",
result);
}
if (params.source_destination_mapping == api::TableParameters::MappingType::OneToOne &&
params.sources.size() != params.destinations.size())
{
return Error("UnequalSourceAndDestinationIndices",
"source and destination indices should be equal in number",
result);
}

std::pair<std::vector<EdgeDuration>, std::vector<EdgeDistance>> result_tables_pair;
if (request_mapping_src_to_dst == api::TableParameters::MappingType::OneToOne)
{
const auto number_of_entries = num_sources;
std::vector<EdgeDuration> durations_table(number_of_entries, MAXIMAL_EDGE_DURATION);
std::vector<EdgeDistance> distances_table(request_distance ? number_of_entries : 0,
MAXIMAL_EDGE_DISTANCE);
for (std::size_t row = 0; row < num_sources; row++)
{
auto one_to_one_table_result = algorithms.ManyToManySearch(snapped_phantoms,
{params.sources[row]},
{params.destinations[row]},
request_distance);
if (row < number_of_entries)
{
durations_table[row] = one_to_one_table_result.first[0];
if (request_distance)
{
distances_table[row] = one_to_one_table_result.second[0];
}
}
}
result_tables_pair = std::make_pair(durations_table, distances_table);
}
else
{
result_tables_pair = algorithms.ManyToManySearch(
snapped_phantoms, params.sources, params.destinations, request_distance);
}

if ((request_duration && result_tables_pair.first.empty()) ||
(request_distance && result_tables_pair.second.empty()))
@@ -100,9 +148,18 @@ Status TablePlugin::HandleRequest(const RoutingAlgorithmsInterface &algorithms,
{
for (std::size_t row = 0; row < num_sources; row++)
{
for (std::size_t column = 0; column < num_destinations; column++)
auto start_index =
request_mapping_src_to_dst == api::TableParameters::MappingType::OneToOne ? row : 0;
auto end_index =
request_mapping_src_to_dst == api::TableParameters::MappingType::OneToOne
? row + 1
: num_destinations;
for (std::size_t column = start_index; column < end_index; column++)
{
const auto &table_index = row * num_destinations + column;
const auto &table_index =
request_mapping_src_to_dst == api::TableParameters::MappingType::OneToOne
? row
: row * num_destinations + column;
BOOST_ASSERT(table_index < result_tables_pair.first.size());
if (params.fallback_speed != INVALID_FALLBACK_SPEED && params.fallback_speed > 0 &&
result_tables_pair.first[table_index] == MAXIMAL_EDGE_DURATION)