Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
11 kB · 336 lines
C++
at develop
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337/* Boost interval/utility.hpp template implementation file * * Copyright 2000 Jens Maurer * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or * copy at http://www.boost.org/LICENSE_1_0.txt) */
#ifndef GECODE_BOOST_NUMERIC_INTERVAL_UTILITY_HPP#define GECODE_BOOST_NUMERIC_INTERVAL_UTILITY_HPP
#include <gecode/third-party/boost/config.hpp>#include <gecode/third-party/boost/numeric/interval/detail/interval_prototype.hpp>#include <gecode/third-party/boost/numeric/interval/detail/test_input.hpp>#include <gecode/third-party/boost/numeric/interval/detail/bugs.hpp>#include <algorithm>#include <utility>
/* * Implementation of simple functions */
namespace gecode_boost {namespace numeric {
/* * Utility Functions */
template<class T, class Policies> inlineconst T& lower(const interval<T, Policies>& x){ return x.lower();}
template<class T, class Policies> inlineconst T& upper(const interval<T, Policies>& x){ return x.upper();}
template<class T, class Policies> inlineT checked_lower(const interval<T, Policies>& x){ if (empty(x)) { typedef typename Policies::checking checking; return checking::nan(); } return x.lower();}
template<class T, class Policies> inlineT checked_upper(const interval<T, Policies>& x){ if (empty(x)) { typedef typename Policies::checking checking; return checking::nan(); } return x.upper();}
template<class T, class Policies> inlineT width(const interval<T, Policies>& x){ if (interval_lib::detail::test_input(x)) return static_cast<T>(0); typename Policies::rounding rnd; return rnd.sub_up(x.upper(), x.lower());}
template<class T, class Policies> inlineT median(const interval<T, Policies>& x){ if (interval_lib::detail::test_input(x)) { typedef typename Policies::checking checking; return checking::nan(); } typename Policies::rounding rnd; return rnd.median(x.lower(), x.upper());}
template<class T, class Policies> inlineinterval<T, Policies> widen(const interval<T, Policies>& x, const T& v){ if (interval_lib::detail::test_input(x)) return interval<T, Policies>::empty(); typename Policies::rounding rnd; return interval<T, Policies>(rnd.sub_down(x.lower(), v), rnd.add_up (x.upper(), v), true);}
/* * Set-like operations */
template<class T, class Policies> inlinebool empty(const interval<T, Policies>& x){ return interval_lib::detail::test_input(x);}
template<class T, class Policies> inlinebool zero_in(const interval<T, Policies>& x){ if (interval_lib::detail::test_input(x)) return false; return (!interval_lib::user::is_pos(x.lower())) && (!interval_lib::user::is_neg(x.upper()));}
template<class T, class Policies> inlinebool in_zero(const interval<T, Policies>& x) // DEPRECATED{ return zero_in<T, Policies>(x);}
template<class T, class Policies> inlinebool in(const T& x, const interval<T, Policies>& y){ if (interval_lib::detail::test_input(x, y)) return false; return y.lower() <= x && x <= y.upper();}
template<class T, class Policies> inlinebool subset(const interval<T, Policies>& x, const interval<T, Policies>& y){ if (empty(x)) return true; return !empty(y) && y.lower() <= x.lower() && x.upper() <= y.upper();}
template<class T, class Policies1, class Policies2> inlinebool proper_subset(const interval<T, Policies1>& x, const interval<T, Policies2>& y){ if (empty(y)) return false; if (empty(x)) return true; return y.lower() <= x.lower() && x.upper() <= y.upper() && (y.lower() != x.lower() || x.upper() != y.upper());}
template<class T, class Policies1, class Policies2> inlinebool overlap(const interval<T, Policies1>& x, const interval<T, Policies2>& y){ if (interval_lib::detail::test_input(x, y)) return false; return (x.lower() <= y.lower() && y.lower() <= x.upper()) || (y.lower() <= x.lower() && x.lower() <= y.upper());}
template<class T, class Policies> inlinebool singleton(const interval<T, Policies>& x){ return !empty(x) && x.lower() == x.upper();}
template<class T, class Policies1, class Policies2> inlinebool equal(const interval<T, Policies1>& x, const interval<T, Policies2>& y){ if (empty(x)) return empty(y); return !empty(y) && x.lower() == y.lower() && x.upper() == y.upper();}
template<class T, class Policies> inlineinterval<T, Policies> intersect(const interval<T, Policies>& x, const interval<T, Policies>& y){ GECODE_BOOST_USING_STD_MIN(); GECODE_BOOST_USING_STD_MAX(); if (interval_lib::detail::test_input(x, y)) return interval<T, Policies>::empty(); const T& l = max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()); const T& u = min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()); if (l <= u) return interval<T, Policies>(l, u, true); else return interval<T, Policies>::empty();}
template<class T, class Policies> inlineinterval<T, Policies> hull(const interval<T, Policies>& x, const interval<T, Policies>& y){ GECODE_BOOST_USING_STD_MIN(); GECODE_BOOST_USING_STD_MAX(); bool bad_x = interval_lib::detail::test_input(x); bool bad_y = interval_lib::detail::test_input(y); if (bad_x) if (bad_y) return interval<T, Policies>::empty(); else return y; else if (bad_y) return x; return interval<T, Policies>(min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()), true);}
template<class T, class Policies> inlineinterval<T, Policies> hull(const interval<T, Policies>& x, const T& y){ GECODE_BOOST_USING_STD_MIN(); GECODE_BOOST_USING_STD_MAX(); bool bad_x = interval_lib::detail::test_input(x); bool bad_y = interval_lib::detail::test_input<T, Policies>(y); if (bad_y) if (bad_x) return interval<T, Policies>::empty(); else return x; else if (bad_x) return interval<T, Policies>(y, y, true); return interval<T, Policies>(min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y), true);}
template<class T, class Policies> inlineinterval<T, Policies> hull(const T& x, const interval<T, Policies>& y){ GECODE_BOOST_USING_STD_MIN(); GECODE_BOOST_USING_STD_MAX(); bool bad_x = interval_lib::detail::test_input<T, Policies>(x); bool bad_y = interval_lib::detail::test_input(y); if (bad_x) if (bad_y) return interval<T, Policies>::empty(); else return y; else if (bad_y) return interval<T, Policies>(x, x, true); return interval<T, Policies>(min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true);}
template<class T> inlineinterval<T> hull(const T& x, const T& y){ return interval<T>::hull(x, y);}
template<class T, class Policies> inlinestd::pair<interval<T, Policies>, interval<T, Policies> >bisect(const interval<T, Policies>& x){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x)) return std::pair<I,I>(I::empty(), I::empty()); const T m = median(x); return std::pair<I,I>(I(x.lower(), m, true), I(m, x.upper(), true));}
/* * Elementary functions */
template<class T, class Policies> inlineT norm(const interval<T, Policies>& x){ if (interval_lib::detail::test_input(x)) { typedef typename Policies::checking checking; return checking::nan(); } GECODE_BOOST_USING_STD_MAX(); return max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(static_cast<T>(-x.lower()), x.upper());}
template<class T, class Policies> inlineinterval<T, Policies> abs(const interval<T, Policies>& x){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x)) return I::empty(); if (!interval_lib::user::is_neg(x.lower())) return x; if (!interval_lib::user::is_pos(x.upper())) return -x; GECODE_BOOST_USING_STD_MAX(); return I(static_cast<T>(0), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(static_cast<T>(-x.lower()), x.upper()), true);}
template<class T, class Policies> inlineinterval<T, Policies> max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x, const interval<T, Policies>& y){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); GECODE_BOOST_USING_STD_MAX(); return I(max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()), true);}
template<class T, class Policies> inlineinterval<T, Policies> max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x, const T& y){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); GECODE_BOOST_USING_STD_MAX(); return I(max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y), true);}
template<class T, class Policies> inlineinterval<T, Policies> max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION (const T& x, const interval<T, Policies>& y){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); GECODE_BOOST_USING_STD_MAX(); return I(max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), max GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true);}
template<class T, class Policies> inlineinterval<T, Policies> min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x, const interval<T, Policies>& y){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); GECODE_BOOST_USING_STD_MIN(); return I(min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()), min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()), true);}
template<class T, class Policies> inlineinterval<T, Policies> min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x, const T& y){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); GECODE_BOOST_USING_STD_MIN(); return I(min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y), min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y), true);}
template<class T, class Policies> inlineinterval<T, Policies> min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION (const T& x, const interval<T, Policies>& y){ typedef interval<T, Policies> I; if (interval_lib::detail::test_input(x, y)) return I::empty(); GECODE_BOOST_USING_STD_MIN(); return I(min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), min GECODE_BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true);}
} // namespace numeric} // namespace gecode_boost
#endif // GECODE_BOOST_NUMERIC_INTERVAL_UTILITY_HPP