From cc24f766d014020a74ef95c67c45a841bd77b1f6 Mon Sep 17 00:00:00 2001 From: jcoupey Date: Tue, 29 Oct 2024 09:37:15 +0100 Subject: [PATCH] Use optional for Input::_amount_size instead of manually set flag. --- src/structures/vroom/input/input.cpp | 27 +++++++++++++++------------ src/structures/vroom/input/input.h | 7 ++++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/structures/vroom/input/input.cpp b/src/structures/vroom/input/input.cpp index 1d7570a2b..0dcaa94c3 100644 --- a/src/structures/vroom/input/input.cpp +++ b/src/structures/vroom/input/input.cpp @@ -99,17 +99,18 @@ void Input::add_routing_wrapper(const std::string& profile) { } void Input::check_amount_size(const Amount& amount) { - if (!_amount_size_set) { + const auto size = amount.size(); + + if (!_amount_size.has_value()) { // Only setup once on first call. - _amount_size_set = true; - _amount_size = amount.size(); - _zero = Amount(_amount_size); + _amount_size = size; + _zero = Amount(size); } else { - if (amount.size() != _amount_size) { + if (size != _amount_size.value()) { throw InputException( std::format("Inconsistent delivery length: {} instead of {}.", - amount.size(), - _amount_size)); + size, + _amount_size.value())); } } } @@ -604,7 +605,9 @@ void Input::set_vehicles_costs() { } void Input::set_vehicles_max_tasks() { - if (_has_jobs && !_has_shipments && _amount_size > 0) { + const auto amount_size = get_amount_size(); + + if (_has_jobs && !_has_shipments && amount_size > 0) { // For job-only instances where capacity restrictions apply: // compute an upper bound of the number of jobs for each vehicle // based on pickups load and delivery loads. This requires sorting @@ -619,12 +622,12 @@ void Input::set_vehicles_max_tasks() { }; std::vector> - job_pickups_per_component(_amount_size, + job_pickups_per_component(amount_size, std::vector(jobs.size())); std::vector> - job_deliveries_per_component(_amount_size, + job_deliveries_per_component(amount_size, std::vector(jobs.size())); - for (std::size_t i = 0; i < _amount_size; ++i) { + for (std::size_t i = 0; i < amount_size; ++i) { for (Index j = 0; j < jobs.size(); ++j) { job_pickups_per_component[i][j] = JobAmount({j, jobs[j].pickup[i]}); job_deliveries_per_component[i][j] = @@ -641,7 +644,7 @@ void Input::set_vehicles_max_tasks() { for (Index v = 0; v < vehicles.size(); ++v) { std::size_t max_tasks = jobs.size(); - for (std::size_t i = 0; i < _amount_size; ++i) { + for (std::size_t i = 0; i < amount_size; ++i) { Capacity pickup_sum = 0; Capacity delivery_sum = 0; std::size_t doable_pickups = 0; diff --git a/src/structures/vroom/input/input.h b/src/structures/vroom/input/input.h index bb954f243..ae2519f64 100644 --- a/src/structures/vroom/input/input.h +++ b/src/structures/vroom/input/input.h @@ -12,6 +12,7 @@ All rights reserved (see LICENSE). #include #include +#include #include #include "routing/wrapper.h" @@ -77,8 +78,7 @@ class Input { bool _all_locations_have_coords{true}; std::vector> _jobs_vehicles_evals; - bool _amount_size_set{false}; - unsigned _amount_size; + std::optional _amount_size; Amount _zero; const io::Servers _servers; @@ -117,7 +117,8 @@ class Input { bool apply_TSPFix = false); unsigned get_amount_size() const { - return _amount_size; + assert(_amount_size.has_value()); + return _amount_size.value(); } void set_geometry(bool geometry);