Make max-cell-sizes parameter a comma-separated list

This commit is contained in:
Michael Krasnyk 2017-03-28 16:56:22 +02:00 committed by Patrick Niklaus
parent ebd938a8fc
commit dac929f8df
3 changed files with 60 additions and 26 deletions

View File

@ -18,3 +18,9 @@ Feature: osrm-partition command line options: invalid options
And stderr should contain "option" And stderr should contain "option"
And stderr should contain "fly-me-to-the-moon" And stderr should contain "fly-me-to-the-moon"
And it should exit with an error And it should exit with an error
Scenario: osrm-partition - Check non-descending order
When I try to run "osrm-partition --max-cell-sizes 4,64,16 fly-me-to-the-moon.osrm"
Then stdout should be empty
And stderr should contain "must be sorted in non-descending order"
And it should exit with an error

View File

@ -3,7 +3,7 @@ Feature: Multi level routing
Background: Background:
Given the profile "testbot" Given the profile "testbot"
And the partition extra arguments "--small-component-size 1 --max-cell-sizes 4 16 64 --" And the partition extra arguments "--small-component-size 1 --max-cell-sizes 4,16,64"
Scenario: Testbot - Multi level routing check partition Scenario: Testbot - Multi level routing check partition
Given the node map Given the node map
@ -31,7 +31,7 @@ Feature: Multi level routing
| be | primary | | be | primary |
And the data has been extracted And the data has been extracted
When I run "osrm-partition --max-cell-sizes 4 16 --small-component-size 1 {processed_file}" When I run "osrm-partition --max-cell-sizes 4,16 --small-component-size 1 {processed_file}"
Then it should exit successfully Then it should exit successfully
And stdout should not contain "level 1 #cells 1 bit size 1" And stdout should not contain "level 1 #cells 1 bit size 1"
@ -111,7 +111,7 @@ Feature: Multi level routing
lk lk
""" """
And the partition extra arguments "--small-component-size 1 --max-cell-sizes 4 16 --" And the partition extra arguments "--small-component-size 1 --max-cell-sizes 4,16"
And the ways And the ways
| nodes | maxspeed | | nodes | maxspeed |
| abcda | 5 | | abcda | 5 |

View File

@ -12,7 +12,9 @@
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
#include <boost/range/adaptor/tokenized.hpp>
#include <boost/range/adaptor/transformed.hpp> #include <boost/range/adaptor/transformed.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <iostream> #include <iostream>
@ -25,6 +27,45 @@ enum class return_code : unsigned
exit exit
}; };
struct MaxCellSizesArgument
{
std::vector<size_t> value;
};
std::ostream &operator<<(std::ostream &os, const MaxCellSizesArgument &arg)
{
return os << boost::algorithm::join(
arg.value | boost::adaptors::transformed([](auto x) { return std::to_string(x); }),
",");
}
void validate(boost::any &v, const std::vector<std::string> &values, MaxCellSizesArgument *, int)
{
using namespace boost::program_options;
using namespace boost::adaptors;
// Make sure no previous assignment to 'v' was made.
validators::check_first_occurrence(v);
// Extract the first string from 'values'. If there is more than
// one string, it's an error, and exception will be thrown.
const std::string &s = validators::get_single_string(values);
std::vector<size_t> output;
boost::copy(s | tokenized(boost::regex(","), -1) | transformed([](const auto &x) {
try
{
return boost::lexical_cast<std::size_t>(x);
}
catch (const boost::bad_lexical_cast &)
{
throw validation_error(validation_error::invalid_option_value);
}
}),
std::back_inserter(output));
v = boost::any(MaxCellSizesArgument{output});
}
return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &config) return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &config)
{ {
// declare a group of options that will be allowed only on command line // declare a group of options that will be allowed only on command line
@ -60,14 +101,8 @@ return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &c
"Size threshold for small components.") "Size threshold for small components.")
// //
("max-cell-sizes", ("max-cell-sizes",
boost::program_options::value<std::vector<std::size_t>>(&config.max_cell_sizes) boost::program_options::value<MaxCellSizesArgument>()->default_value(
->multitoken() MaxCellSizesArgument{config.max_cell_sizes}),
->default_value(config.max_cell_sizes,
boost::algorithm::join(
config.max_cell_sizes |
boost::adaptors::transformed(
static_cast<std::string (*)(std::size_t)>(std::to_string)),
" ")),
"Maximum cell sizes starting from the level 1. The first cell size value is a bisection " "Maximum cell sizes starting from the level 1. The first cell size value is a bisection "
"termination citerion"); "termination citerion");
@ -128,23 +163,16 @@ return_code parseArguments(int argc, char *argv[], partition::PartitionConfig &c
return return_code::fail; return return_code::fail;
} }
if (config.max_cell_sizes.empty()) if (option_variables.count("max-cell-sizes"))
{ {
util::Log(logERROR) << "The maximum cell sizes array must be non-empty"; config.max_cell_sizes = option_variables["max-cell-sizes"].as<MaxCellSizesArgument>().value;
return return_code::fail;
}
if (!std::is_sorted(config.max_cell_sizes.begin(), config.max_cell_sizes.end())) if (!std::is_sorted(config.max_cell_sizes.begin(), config.max_cell_sizes.end()))
{ {
util::Log(logERROR) util::Log(logERROR)
<< "The maximum cell sizes array must be sorted in non-descending order."; << "The maximum cell sizes array must be sorted in non-descending order.";
return return_code::fail; return return_code::fail;
} }
if (config.max_cell_sizes.front() < 2)
{
util::Log(logERROR) << "Cells on the first level must have at least 2 nodes";
return return_code::fail;
} }
return return_code::ok; return return_code::ok;