From 980e4ee89a04fd1f460958407a0f6079b03e8bc3 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Wed, 2 Sep 2015 16:33:03 +0200 Subject: [PATCH] Don't mix signed and unsigned in comparisons as signed is converted first to unsigned. This is true: -1 > 1u because the integer literal `-1` is first converted to a large unsigned value and then compared to the unsigned `1`. This patch fixes several of those isses in the farthest insertion algorithm. `-Wsign-compare` catches those issues. References: - http://stackoverflow.com/a/5416498 - C++14 standard --- algorithms/trip_farthest_insertion.hpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/algorithms/trip_farthest_insertion.hpp b/algorithms/trip_farthest_insertion.hpp index 8f5c59ebb..5f41248a4 100644 --- a/algorithms/trip_farthest_insertion.hpp +++ b/algorithms/trip_farthest_insertion.hpp @@ -32,6 +32,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "../util/dist_table_wrapper.hpp" #include +#include #include #include @@ -170,10 +171,12 @@ std::vector FarthestInsertionTrip(const NodeIDIterator &start, ////////////////////////////////////////////////////////////////////////////////////////////////// const auto component_size = std::distance(start, end); + BOOST_ASSERT(component_size >= 0); + auto max_from = -1; auto max_to = -1; - if (component_size == number_of_locations) + if (static_cast(component_size) == number_of_locations) { // find the pair of location with the biggest distance and make the pair the initial start // trip @@ -199,12 +202,14 @@ std::vector FarthestInsertionTrip(const NodeIDIterator &start, } } } - BOOST_ASSERT_MSG(max_from < number_of_locations && max_from >= 0, "start node"); - BOOST_ASSERT_MSG(max_to < number_of_locations && max_to >= 0, "start node"); + BOOST_ASSERT(max_from >= 0); + BOOST_ASSERT(max_to >= 0); + BOOST_ASSERT_MSG(static_cast(max_from) < number_of_locations, "start node"); + BOOST_ASSERT_MSG(static_cast(max_to) < number_of_locations, "start node"); return FindRoute(number_of_locations, component_size, start, end, dist_table, max_from, max_to); } } // end namespace trip } // end namespace osrm -#endif // TRIP_FARTHEST_INSERTION_HPP \ No newline at end of file +#endif // TRIP_FARTHEST_INSERTION_HPP