From 020fff0e68042383951d4e457c099b775f42faac Mon Sep 17 00:00:00 2001 From: Jeremy Siek Date: Mon, 3 Sep 2001 20:36:22 +0000 Subject: [PATCH] added closed plus [SVN r11005] --- include/boost/graph/relax.hpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/boost/graph/relax.hpp b/include/boost/graph/relax.hpp index e8c2d7fe..b2ac0c0f 100644 --- a/include/boost/graph/relax.hpp +++ b/include/boost/graph/relax.hpp @@ -41,11 +41,19 @@ namespace std { using ::abs; } namespace boost { - // warning: when weight of e is size_t and weight of e is - // close to max of size_t, du + wuv will overflow. To avoid - // this, be careful about the value of weight. If it is larger - // than the half of current max of the type, change the type of - // weight to have more bytes. + // The following version of the plus functor prevents + // problems due to overflow at positive infinity. + + template + struct closed_plus + { + T operator()(const T& a, const T& b) const { + T inf = std::numeric_limits::max(); + if (b > 0 && std::abs(inf - a) < b) + return inf; + return a + b; + } + }; template ::value_type W; D d_u = get(d, u), d_v = get(d, v); W w_e = get(w, e); - - // workaround overflow problem, don't relax if d_u is close to inf - if (w_e > 0 && std::abs(std::numeric_limits::max() - d_u) < w_e) - return false; if ( compare(combine(d_u, w_e), d_v) ) { put(d, v, combine(d_u, w_e)); @@ -80,7 +84,7 @@ namespace boost { const Graph& g, WeightMap w, PredecessorMap p, DistanceMap d) { typedef typename property_traits::value_type D; - typedef std::plus Combine; + typedef closed_plus Combine; typedef std::less Compare; return relax(e, g, w, p, d, Combine(), Compare()); }