1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/numeric/interval/utility.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,337 @@
1.4 +/* Boost interval/utility.hpp template implementation file
1.5 + *
1.6 + * Copyright 2000 Jens Maurer
1.7 + * Copyright 2002-2003 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
1.8 + *
1.9 + * Distributed under the Boost Software License, Version 1.0.
1.10 + * (See accompanying file LICENSE_1_0.txt or
1.11 + * copy at http://www.boost.org/LICENSE_1_0.txt)
1.12 + */
1.13 +
1.14 +#ifndef BOOST_NUMERIC_INTERVAL_UTILITY_HPP
1.15 +#define BOOST_NUMERIC_INTERVAL_UTILITY_HPP
1.16 +
1.17 +#include <boost/config.hpp>
1.18 +#include <boost/numeric/interval/detail/interval_prototype.hpp>
1.19 +#include <boost/numeric/interval/detail/test_input.hpp>
1.20 +#include <boost/numeric/interval/detail/bugs.hpp>
1.21 +#include <algorithm>
1.22 +#include <utility>
1.23 +
1.24 +/*
1.25 + * Implementation of simple functions
1.26 + */
1.27 +
1.28 +namespace boost {
1.29 +namespace numeric {
1.30 +
1.31 +/*
1.32 + * Utility Functions
1.33 + */
1.34 +
1.35 +template<class T, class Policies> inline
1.36 +const T& lower(const interval<T, Policies>& x)
1.37 +{
1.38 + return x.lower();
1.39 +}
1.40 +
1.41 +template<class T, class Policies> inline
1.42 +const T& upper(const interval<T, Policies>& x)
1.43 +{
1.44 + return x.upper();
1.45 +}
1.46 +
1.47 +template<class T, class Policies> inline
1.48 +T checked_lower(const interval<T, Policies>& x)
1.49 +{
1.50 + if (empty(x)) {
1.51 + typedef typename Policies::checking checking;
1.52 + return checking::nan();
1.53 + }
1.54 + return x.lower();
1.55 +}
1.56 +
1.57 +template<class T, class Policies> inline
1.58 +T checked_upper(const interval<T, Policies>& x)
1.59 +{
1.60 + if (empty(x)) {
1.61 + typedef typename Policies::checking checking;
1.62 + return checking::nan();
1.63 + }
1.64 + return x.upper();
1.65 +}
1.66 +
1.67 +template<class T, class Policies> inline
1.68 +T width(const interval<T, Policies>& x)
1.69 +{
1.70 + if (interval_lib::detail::test_input(x)) return static_cast<T>(0);
1.71 + typename Policies::rounding rnd;
1.72 + return rnd.sub_up(x.upper(), x.lower());
1.73 +}
1.74 +
1.75 +template<class T, class Policies> inline
1.76 +T median(const interval<T, Policies>& x)
1.77 +{
1.78 + if (interval_lib::detail::test_input(x)) {
1.79 + typedef typename Policies::checking checking;
1.80 + return checking::nan();
1.81 + }
1.82 + typename Policies::rounding rnd;
1.83 + return rnd.median(x.lower(), x.upper());
1.84 +}
1.85 +
1.86 +template<class T, class Policies> inline
1.87 +interval<T, Policies> widen(const interval<T, Policies>& x, const T& v)
1.88 +{
1.89 + if (interval_lib::detail::test_input(x))
1.90 + return interval<T, Policies>::empty();
1.91 + typename Policies::rounding rnd;
1.92 + return interval<T, Policies>(rnd.sub_down(x.lower(), v),
1.93 + rnd.add_up (x.upper(), v), true);
1.94 +}
1.95 +
1.96 +/*
1.97 + * Set-like operations
1.98 + */
1.99 +
1.100 +template<class T, class Policies> inline
1.101 +bool empty(const interval<T, Policies>& x)
1.102 +{
1.103 + return interval_lib::detail::test_input(x);
1.104 +}
1.105 +
1.106 +template<class T, class Policies> inline
1.107 +bool zero_in(const interval<T, Policies>& x)
1.108 +{
1.109 + if (interval_lib::detail::test_input(x)) return false;
1.110 + return (!interval_lib::user::is_pos(x.lower())) &&
1.111 + (!interval_lib::user::is_neg(x.upper()));
1.112 +}
1.113 +
1.114 +template<class T, class Policies> inline
1.115 +bool in_zero(const interval<T, Policies>& x) // DEPRECATED
1.116 +{
1.117 + return zero_in<T, Policies>(x);
1.118 +}
1.119 +
1.120 +template<class T, class Policies> inline
1.121 +bool in(const T& x, const interval<T, Policies>& y)
1.122 +{
1.123 + if (interval_lib::detail::test_input(x, y)) return false;
1.124 + return y.lower() <= x && x <= y.upper();
1.125 +}
1.126 +
1.127 +template<class T, class Policies> inline
1.128 +bool subset(const interval<T, Policies>& x,
1.129 + const interval<T, Policies>& y)
1.130 +{
1.131 + if (empty(x)) return true;
1.132 + return !empty(y) && y.lower() <= x.lower() && x.upper() <= y.upper();
1.133 +}
1.134 +
1.135 +template<class T, class Policies1, class Policies2> inline
1.136 +bool proper_subset(const interval<T, Policies1>& x,
1.137 + const interval<T, Policies2>& y)
1.138 +{
1.139 + if (empty(y)) return false;
1.140 + if (empty(x)) return true;
1.141 + return y.lower() <= x.lower() && x.upper() <= y.upper() &&
1.142 + (y.lower() != x.lower() || x.upper() != y.upper());
1.143 +}
1.144 +
1.145 +template<class T, class Policies1, class Policies2> inline
1.146 +bool overlap(const interval<T, Policies1>& x,
1.147 + const interval<T, Policies2>& y)
1.148 +{
1.149 + if (interval_lib::detail::test_input(x, y)) return false;
1.150 + return x.lower() <= y.lower() && y.lower() <= x.upper() ||
1.151 + y.lower() <= x.lower() && x.lower() <= y.upper();
1.152 +}
1.153 +
1.154 +template<class T, class Policies> inline
1.155 +bool singleton(const interval<T, Policies>& x)
1.156 +{
1.157 + return !empty(x) && x.lower() == x.upper();
1.158 +}
1.159 +
1.160 +template<class T, class Policies1, class Policies2> inline
1.161 +bool equal(const interval<T, Policies1>& x, const interval<T, Policies2>& y)
1.162 +{
1.163 + if (empty(x)) return empty(y);
1.164 + return !empty(y) && x.lower() == y.lower() && x.upper() == y.upper();
1.165 +}
1.166 +
1.167 +template<class T, class Policies> inline
1.168 +interval<T, Policies> intersect(const interval<T, Policies>& x,
1.169 + const interval<T, Policies>& y)
1.170 +{
1.171 + BOOST_USING_STD_MIN();
1.172 + BOOST_USING_STD_MAX();
1.173 + if (interval_lib::detail::test_input(x, y))
1.174 + return interval<T, Policies>::empty();
1.175 + const T& l = max BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower());
1.176 + const T& u = min BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper());
1.177 + if (l <= u) return interval<T, Policies>(l, u, true);
1.178 + else return interval<T, Policies>::empty();
1.179 +}
1.180 +
1.181 +template<class T, class Policies> inline
1.182 +interval<T, Policies> hull(const interval<T, Policies>& x,
1.183 + const interval<T, Policies>& y)
1.184 +{
1.185 + BOOST_USING_STD_MIN();
1.186 + BOOST_USING_STD_MAX();
1.187 + bool bad_x = interval_lib::detail::test_input(x);
1.188 + bool bad_y = interval_lib::detail::test_input(y);
1.189 + if (bad_x)
1.190 + if (bad_y) return interval<T, Policies>::empty();
1.191 + else return y;
1.192 + else
1.193 + if (bad_y) return x;
1.194 + return interval<T, Policies>(min BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()),
1.195 + max BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()), true);
1.196 +}
1.197 +
1.198 +template<class T, class Policies> inline
1.199 +interval<T, Policies> hull(const interval<T, Policies>& x, const T& y)
1.200 +{
1.201 + BOOST_USING_STD_MIN();
1.202 + BOOST_USING_STD_MAX();
1.203 + bool bad_x = interval_lib::detail::test_input(x);
1.204 + bool bad_y = interval_lib::detail::test_input<T, Policies>(y);
1.205 + if (bad_y)
1.206 + if (bad_x) return interval<T, Policies>::empty();
1.207 + else return x;
1.208 + else
1.209 + if (bad_x) return interval<T, Policies>(y, y, true);
1.210 + return interval<T, Policies>(min BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y),
1.211 + max BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y), true);
1.212 +}
1.213 +
1.214 +template<class T, class Policies> inline
1.215 +interval<T, Policies> hull(const T& x, const interval<T, Policies>& y)
1.216 +{
1.217 + BOOST_USING_STD_MIN();
1.218 + BOOST_USING_STD_MAX();
1.219 + bool bad_x = interval_lib::detail::test_input<T, Policies>(x);
1.220 + bool bad_y = interval_lib::detail::test_input(y);
1.221 + if (bad_x)
1.222 + if (bad_y) return interval<T, Policies>::empty();
1.223 + else return y;
1.224 + else
1.225 + if (bad_y) return interval<T, Policies>(x, x, true);
1.226 + return interval<T, Policies>(min BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()),
1.227 + max BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true);
1.228 +}
1.229 +
1.230 +template<class T> inline
1.231 +interval<T> hull(const T& x, const T& y)
1.232 +{
1.233 + return interval<T>::hull(x, y);
1.234 +}
1.235 +
1.236 +template<class T, class Policies> inline
1.237 +std::pair<interval<T, Policies>, interval<T, Policies> >
1.238 +bisect(const interval<T, Policies>& x)
1.239 +{
1.240 + typedef interval<T, Policies> I;
1.241 + if (interval_lib::detail::test_input(x))
1.242 + return std::pair<I,I>(I::empty(), I::empty());
1.243 + const T m = median(x);
1.244 + return std::pair<I,I>(I(x.lower(), m, true), I(m, x.upper(), true));
1.245 +}
1.246 +
1.247 +/*
1.248 + * Elementary functions
1.249 + */
1.250 +
1.251 +template<class T, class Policies> inline
1.252 +T norm(const interval<T, Policies>& x)
1.253 +{
1.254 + typedef interval<T, Policies> I;
1.255 + if (interval_lib::detail::test_input(x)) {
1.256 + typedef typename Policies::checking checking;
1.257 + return checking::nan();
1.258 + }
1.259 + BOOST_USING_STD_MAX();
1.260 + return max BOOST_PREVENT_MACRO_SUBSTITUTION(static_cast<T>(-x.lower()), x.upper());
1.261 +}
1.262 +
1.263 +template<class T, class Policies> inline
1.264 +interval<T, Policies> abs(const interval<T, Policies>& x)
1.265 +{
1.266 + typedef interval<T, Policies> I;
1.267 + if (interval_lib::detail::test_input(x))
1.268 + return I::empty();
1.269 + if (!interval_lib::user::is_neg(x.lower())) return x;
1.270 + if (!interval_lib::user::is_pos(x.upper())) return -x;
1.271 + BOOST_USING_STD_MAX();
1.272 + return I(static_cast<T>(0), max BOOST_PREVENT_MACRO_SUBSTITUTION(static_cast<T>(-x.lower()), x.upper()), true);
1.273 +}
1.274 +
1.275 +template<class T, class Policies> inline
1.276 +interval<T, Policies> max BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x,
1.277 + const interval<T, Policies>& y)
1.278 +{
1.279 + typedef interval<T, Policies> I;
1.280 + if (interval_lib::detail::test_input(x, y))
1.281 + return I::empty();
1.282 + BOOST_USING_STD_MAX();
1.283 + return I(max BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()), max BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()), true);
1.284 +}
1.285 +
1.286 +template<class T, class Policies> inline
1.287 +interval<T, Policies> max BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x, const T& y)
1.288 +{
1.289 + typedef interval<T, Policies> I;
1.290 + if (interval_lib::detail::test_input(x, y))
1.291 + return I::empty();
1.292 + BOOST_USING_STD_MAX();
1.293 + return I(max BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y), max BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y), true);
1.294 +}
1.295 +
1.296 +template<class T, class Policies> inline
1.297 +interval<T, Policies> max BOOST_PREVENT_MACRO_SUBSTITUTION (const T& x, const interval<T, Policies>& y)
1.298 +{
1.299 + typedef interval<T, Policies> I;
1.300 + if (interval_lib::detail::test_input(x, y))
1.301 + return I::empty();
1.302 + BOOST_USING_STD_MAX();
1.303 + return I(max BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), max BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true);
1.304 +}
1.305 +
1.306 +template<class T, class Policies> inline
1.307 +interval<T, Policies> min BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x,
1.308 + const interval<T, Policies>& y)
1.309 +{
1.310 + typedef interval<T, Policies> I;
1.311 + if (interval_lib::detail::test_input(x, y))
1.312 + return I::empty();
1.313 + BOOST_USING_STD_MIN();
1.314 + return I(min BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y.lower()), min BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y.upper()), true);
1.315 +}
1.316 +
1.317 +template<class T, class Policies> inline
1.318 +interval<T, Policies> min BOOST_PREVENT_MACRO_SUBSTITUTION (const interval<T, Policies>& x, const T& y)
1.319 +{
1.320 + typedef interval<T, Policies> I;
1.321 + if (interval_lib::detail::test_input(x, y))
1.322 + return I::empty();
1.323 + BOOST_USING_STD_MIN();
1.324 + return I(min BOOST_PREVENT_MACRO_SUBSTITUTION(x.lower(), y), min BOOST_PREVENT_MACRO_SUBSTITUTION(x.upper(), y), true);
1.325 +}
1.326 +
1.327 +template<class T, class Policies> inline
1.328 +interval<T, Policies> min BOOST_PREVENT_MACRO_SUBSTITUTION (const T& x, const interval<T, Policies>& y)
1.329 +{
1.330 + typedef interval<T, Policies> I;
1.331 + if (interval_lib::detail::test_input(x, y))
1.332 + return I::empty();
1.333 + BOOST_USING_STD_MIN();
1.334 + return I(min BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.lower()), min BOOST_PREVENT_MACRO_SUBSTITUTION(x, y.upper()), true);
1.335 +}
1.336 +
1.337 +} // namespace numeric
1.338 +} // namespace boost
1.339 +
1.340 +#endif // BOOST_NUMERIC_INTERVAL_UTILITY_HPP