1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/numeric/ublas/traits.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,537 @@
1.4 +//
1.5 +// Copyright (c) 2000-2002
1.6 +// Joerg Walter, Mathias Koch
1.7 +//
1.8 +// Permission to use, copy, modify, distribute and sell this software
1.9 +// and its documentation for any purpose is hereby granted without fee,
1.10 +// provided that the above copyright notice appear in all copies and
1.11 +// that both that copyright notice and this permission notice appear
1.12 +// in supporting documentation. The authors make no representations
1.13 +// about the suitability of this software for any purpose.
1.14 +// It is provided "as is" without express or implied warranty.
1.15 +//
1.16 +// The authors gratefully acknowledge the support of
1.17 +// GeNeSys mbH & Co. KG in producing this work.
1.18 +//
1.19 +
1.20 +#ifndef _BOOST_UBLAS_TRAITS_
1.21 +#define _BOOST_UBLAS_TRAITS_
1.22 +
1.23 +#include <iterator>
1.24 +#include <complex>
1.25 +#include <cmath>
1.26 +
1.27 +#include <boost/numeric/ublas/detail/config.hpp>
1.28 +#include <boost/numeric/ublas/detail/iterator.hpp>
1.29 +#include <boost/numeric/ublas/detail/returntype_deduction.hpp>
1.30 +
1.31 +
1.32 +namespace boost { namespace numeric { namespace ublas {
1.33 +
1.34 + // Use Joel de Guzman's return type deduction
1.35 + // uBLAS assumes a common return type for all binary arithmetic operators
1.36 + template<class X, class Y>
1.37 + struct promote_traits {
1.38 + typedef type_deduction_detail::base_result_of<X, Y> base_type;
1.39 + static typename base_type::x_type x;
1.40 + static typename base_type::y_type y;
1.41 + static const std::size_t size = sizeof (
1.42 + type_deduction_detail::test<
1.43 + typename base_type::x_type
1.44 + , typename base_type::y_type
1.45 + >(x + y) // Use x+y to stand of all the arithmetic actions
1.46 + );
1.47 +
1.48 + static const std::size_t index = (size / sizeof (char)) - 1;
1.49 + typedef typename mpl::at_c<
1.50 + typename base_type::types, index>::type id;
1.51 + typedef typename id::type promote_type;
1.52 + };
1.53 +
1.54 +
1.55 + // Type traits - generic numeric properties and functions
1.56 + template<class T>
1.57 + struct type_traits;
1.58 +
1.59 + // Define properties for a generic scalar type
1.60 + template<class T>
1.61 + struct scalar_traits {
1.62 + typedef scalar_traits<T> self_type;
1.63 + typedef T value_type;
1.64 + typedef const T &const_reference;
1.65 + typedef T &reference;
1.66 +
1.67 + typedef T real_type;
1.68 + typedef real_type precision_type; // we do not know what type has more precision then the real_type
1.69 +
1.70 + static const unsigned plus_complexity = 1;
1.71 + static const unsigned multiplies_complexity = 1;
1.72 +
1.73 + static
1.74 + BOOST_UBLAS_INLINE
1.75 + real_type real (const_reference t) {
1.76 + return t;
1.77 + }
1.78 + static
1.79 + BOOST_UBLAS_INLINE
1.80 + real_type imag (const_reference /*t*/) {
1.81 + return 0;
1.82 + }
1.83 + static
1.84 + BOOST_UBLAS_INLINE
1.85 + value_type conj (const_reference t) {
1.86 + return t;
1.87 + }
1.88 +
1.89 + static
1.90 + BOOST_UBLAS_INLINE
1.91 + real_type type_abs (const_reference t) {
1.92 + return std::abs (t); // must use explict std:: as bultin types are not in std namespace
1.93 + }
1.94 + static
1.95 + BOOST_UBLAS_INLINE
1.96 + value_type type_sqrt (const_reference t) {
1.97 + // force a type conversion back to value_type for intgral types
1.98 + return value_type (std::sqrt (t)); // must use explict std:: as bultin types are not in std namespace
1.99 + }
1.100 +
1.101 + static
1.102 + BOOST_UBLAS_INLINE
1.103 + real_type norm_1 (const_reference t) {
1.104 + return self_type::type_abs (t);
1.105 + }
1.106 + static
1.107 + BOOST_UBLAS_INLINE
1.108 + real_type norm_2 (const_reference t) {
1.109 + return self_type::type_abs (t);
1.110 + }
1.111 + static
1.112 + BOOST_UBLAS_INLINE
1.113 + real_type norm_inf (const_reference t) {
1.114 + return self_type::type_abs (t);
1.115 + }
1.116 +
1.117 + static
1.118 + BOOST_UBLAS_INLINE
1.119 + bool equals (const_reference t1, const_reference t2) {
1.120 + return self_type::norm_inf (t1 - t2) < BOOST_UBLAS_TYPE_CHECK_EPSILON *
1.121 + (std::max) ((std::max) (self_type::norm_inf (t1),
1.122 + self_type::norm_inf (t2)),
1.123 + BOOST_UBLAS_TYPE_CHECK_MIN);
1.124 + }
1.125 + };
1.126 +
1.127 + // Define default type traits, assume T is a scalar type
1.128 + template<class T>
1.129 + struct type_traits : scalar_traits <T> {
1.130 + typedef type_traits<T> self_type;
1.131 + typedef T value_type;
1.132 + typedef const T &const_reference;
1.133 + typedef T &reference;
1.134 +
1.135 + typedef T real_type;
1.136 + typedef real_type precision_type;
1.137 + static const unsigned multiplies_complexity = 1;
1.138 +
1.139 + };
1.140 +
1.141 + // Define real type traits
1.142 + template<>
1.143 + struct type_traits<float> : scalar_traits<float> {
1.144 + typedef type_traits<float> self_type;
1.145 + typedef float value_type;
1.146 + typedef const value_type &const_reference;
1.147 + typedef value_type &reference;
1.148 + typedef value_type real_type;
1.149 + typedef double precision_type;
1.150 + };
1.151 + template<>
1.152 + struct type_traits<double> : scalar_traits<double> {
1.153 + typedef type_traits<double> self_type;
1.154 + typedef double value_type;
1.155 + typedef const value_type &const_reference;
1.156 + typedef value_type &reference;
1.157 + typedef value_type real_type;
1.158 + typedef long double precision_type;
1.159 + };
1.160 + template<>
1.161 + struct type_traits<long double> : scalar_traits<long double> {
1.162 + typedef type_traits<long double> self_type;
1.163 + typedef long double value_type;
1.164 + typedef const value_type &const_reference;
1.165 + typedef value_type &reference;
1.166 + typedef value_type real_type;
1.167 + typedef value_type precision_type;
1.168 + };
1.169 +
1.170 + // Define properties for a generic complex type
1.171 + template<class T>
1.172 + struct complex_traits {
1.173 + typedef complex_traits<T> self_type;
1.174 + typedef T value_type;
1.175 + typedef const T &const_reference;
1.176 + typedef T &reference;
1.177 +
1.178 + typedef typename T::value_type real_type;
1.179 + typedef real_type precision_type; // we do not know what type has more precision then the real_type
1.180 +
1.181 + static const unsigned plus_complexity = 2;
1.182 + static const unsigned multiplies_complexity = 6;
1.183 +
1.184 + static
1.185 + BOOST_UBLAS_INLINE
1.186 + real_type real (const_reference t) {
1.187 + return std::real (t);
1.188 + }
1.189 + static
1.190 + BOOST_UBLAS_INLINE
1.191 + real_type imag (const_reference t) {
1.192 + return std::imag (t);
1.193 + }
1.194 + static
1.195 + BOOST_UBLAS_INLINE
1.196 + value_type conj (const_reference t) {
1.197 + return std::conj (t);
1.198 + }
1.199 +
1.200 + static
1.201 + BOOST_UBLAS_INLINE
1.202 + real_type type_abs (const_reference t) {
1.203 + return abs (t);
1.204 + }
1.205 + static
1.206 + BOOST_UBLAS_INLINE
1.207 + value_type type_sqrt (const_reference t) {
1.208 + return sqrt (t);
1.209 + }
1.210 +
1.211 + static
1.212 + BOOST_UBLAS_INLINE
1.213 + real_type norm_1 (const_reference t) {
1.214 + return type_traits<real_type>::type_abs (self_type::real (t)) +
1.215 + type_traits<real_type>::type_abs (self_type::imag (t));
1.216 + }
1.217 + static
1.218 + BOOST_UBLAS_INLINE
1.219 + real_type norm_2 (const_reference t) {
1.220 + return self_type::type_abs (t);
1.221 + }
1.222 + static
1.223 + BOOST_UBLAS_INLINE
1.224 + real_type norm_inf (const_reference t) {
1.225 + return (std::max) (type_traits<real_type>::type_abs (self_type::real (t)),
1.226 + type_traits<real_type>::type_abs (self_type::imag (t)));
1.227 + }
1.228 +
1.229 + static
1.230 + BOOST_UBLAS_INLINE
1.231 + bool equals (const_reference t1, const_reference t2) {
1.232 + return self_type::norm_inf (t1 - t2) < BOOST_UBLAS_TYPE_CHECK_EPSILON *
1.233 + (std::max) ((std::max) (self_type::norm_inf (t1),
1.234 + self_type::norm_inf (t2)),
1.235 + BOOST_UBLAS_TYPE_CHECK_MIN);
1.236 + }
1.237 + };
1.238 +
1.239 + // Define complex type traits
1.240 + template<>
1.241 + struct type_traits<std::complex<float> > : complex_traits<std::complex<float> >{
1.242 + typedef type_traits<std::complex<float> > self_type;
1.243 + typedef std::complex<float> value_type;
1.244 + typedef const value_type &const_reference;
1.245 + typedef value_type &reference;
1.246 + typedef float real_type;
1.247 + typedef std::complex<double> precision_type;
1.248 +
1.249 + };
1.250 + template<>
1.251 + struct type_traits<std::complex<double> > : complex_traits<std::complex<double> >{
1.252 + typedef type_traits<std::complex<double> > self_type;
1.253 + typedef std::complex<double> value_type;
1.254 + typedef const value_type &const_reference;
1.255 + typedef value_type &reference;
1.256 + typedef double real_type;
1.257 + typedef std::complex<long double> precision_type;
1.258 + };
1.259 + template<>
1.260 + struct type_traits<std::complex<long double> > : complex_traits<std::complex<long double> > {
1.261 + typedef type_traits<std::complex<long double> > self_type;
1.262 + typedef std::complex<long double> value_type;
1.263 + typedef const value_type &const_reference;
1.264 + typedef value_type &reference;
1.265 + typedef long double real_type;
1.266 + typedef value_type precision_type;
1.267 + };
1.268 +
1.269 +#ifdef BOOST_UBLAS_USE_INTERVAL
1.270 + // Define properties for a generic scalar interval type
1.271 + template<class T>
1.272 + struct scalar_interval_type_traits : scalar_type_traits<T> {
1.273 + typedef scalar_interval_type_traits<T> self_type;
1.274 + typedef boost::numeric::interval<float> value_type;
1.275 + typedef const value_type &const_reference;
1.276 + typedef value_type &reference;
1.277 + typedef value_type real_type;
1.278 + typedef real_type precision_type; // we do not know what type has more precision then the real_type
1.279 +
1.280 + static const unsigned plus_complexity = 1;
1.281 + static const unsigned multiplies_complexity = 1;
1.282 +
1.283 + static
1.284 + BOOST_UBLAS_INLINE
1.285 + real_type type_abs (const_reference t) {
1.286 + return abs (t);
1.287 + }
1.288 + static
1.289 + BOOST_UBLAS_INLINE
1.290 + value_type type_sqrt (const_reference t) {
1.291 + return sqrt (t);
1.292 + }
1.293 +
1.294 + static
1.295 + BOOST_UBLAS_INLINE
1.296 + real_type norm_1 (const_reference t) {
1.297 + return self_type::type_abs (t);
1.298 + }
1.299 + static
1.300 + BOOST_UBLAS_INLINE
1.301 + real_type norm_2 (const_reference t) {
1.302 + return self_type::type_abs (t);
1.303 + }
1.304 + static
1.305 + BOOST_UBLAS_INLINE
1.306 + real_type norm_inf (const_reference t) {
1.307 + return self_type::type_abs (t);
1.308 + }
1.309 +
1.310 + static
1.311 + BOOST_UBLAS_INLINE
1.312 + bool equals (const_reference t1, const_reference t2) {
1.313 + return self_type::norm_inf (t1 - t2) < BOOST_UBLAS_TYPE_CHECK_EPSILON *
1.314 + (std::max) ((std::max) (self_type::norm_inf (t1),
1.315 + self_type::norm_inf (t2)),
1.316 + BOOST_UBLAS_TYPE_CHECK_MIN);
1.317 + }
1.318 + };
1.319 +
1.320 + // Define scalar interval type traits
1.321 + template<>
1.322 + struct type_traits<boost::numeric::interval<float> > : scalar_interval_type_traits<boost::numeric::interval<float> > {
1.323 + typedef type_traits<boost::numeric::interval<float> > self_type;
1.324 + typedef boost::numeric::interval<float> value_type;
1.325 + typedef const value_type &const_reference;
1.326 + typedef value_type &reference;
1.327 + typedef value_type real_type;
1.328 + typedef boost::numeric::interval<double> precision_type;
1.329 +
1.330 + };
1.331 + template<>
1.332 + struct type_traits<boost::numeric::interval<double> > : scalar_interval_type_traits<boost::numeric::interval<double> > {
1.333 + typedef type_traits<boost::numeric::interval<double> > self_type;
1.334 + typedef boost::numeric::interval<double> value_type;
1.335 + typedef const value_type &const_reference;
1.336 + typedef value_type &reference;
1.337 + typedef value_type real_type;
1.338 + typedef boost::numeric::interval<long double> precision_type;
1.339 + };
1.340 + template<>
1.341 + struct type_traits<boost::numeric::interval<long double> > : scalar_interval_type_traits<boost::numeric::interval<long double> > {
1.342 + typedef type_traits<boost::numeric::interval<long double> > self_type;
1.343 + typedef boost::numeric::interval<long double> value_type;
1.344 + typedef const value_type &const_reference;
1.345 + typedef value_type &reference;
1.346 + typedef value_type real_type;
1.347 + typedef value_type precision_type;
1.348 + };
1.349 +
1.350 +#endif
1.351 +
1.352 +
1.353 + // Storage tags -- hierarchical definition of storage characteristics
1.354 +
1.355 + struct unknown_storage_tag {};
1.356 + struct sparse_proxy_tag: public unknown_storage_tag {};
1.357 + struct sparse_tag: public sparse_proxy_tag {};
1.358 + struct packed_proxy_tag: public sparse_proxy_tag {};
1.359 + struct packed_tag: public packed_proxy_tag {};
1.360 + struct dense_proxy_tag: public packed_proxy_tag {};
1.361 + struct dense_tag: public dense_proxy_tag {};
1.362 +
1.363 + template<class S1, class S2>
1.364 + struct storage_restrict_traits {
1.365 + typedef S1 storage_category;
1.366 + };
1.367 +
1.368 + template<>
1.369 + struct storage_restrict_traits<sparse_tag, dense_proxy_tag> {
1.370 + typedef sparse_proxy_tag storage_category;
1.371 + };
1.372 + template<>
1.373 + struct storage_restrict_traits<sparse_tag, packed_proxy_tag> {
1.374 + typedef sparse_proxy_tag storage_category;
1.375 + };
1.376 + template<>
1.377 + struct storage_restrict_traits<sparse_tag, sparse_proxy_tag> {
1.378 + typedef sparse_proxy_tag storage_category;
1.379 + };
1.380 +
1.381 + template<>
1.382 + struct storage_restrict_traits<packed_tag, dense_proxy_tag> {
1.383 + typedef packed_proxy_tag storage_category;
1.384 + };
1.385 + template<>
1.386 + struct storage_restrict_traits<packed_tag, packed_proxy_tag> {
1.387 + typedef packed_proxy_tag storage_category;
1.388 + };
1.389 + template<>
1.390 + struct storage_restrict_traits<packed_tag, sparse_proxy_tag> {
1.391 + typedef sparse_proxy_tag storage_category;
1.392 + };
1.393 +
1.394 + template<>
1.395 + struct storage_restrict_traits<packed_proxy_tag, sparse_proxy_tag> {
1.396 + typedef sparse_proxy_tag storage_category;
1.397 + };
1.398 +
1.399 + template<>
1.400 + struct storage_restrict_traits<dense_tag, dense_proxy_tag> {
1.401 + typedef dense_proxy_tag storage_category;
1.402 + };
1.403 + template<>
1.404 + struct storage_restrict_traits<dense_tag, packed_proxy_tag> {
1.405 + typedef packed_proxy_tag storage_category;
1.406 + };
1.407 + template<>
1.408 + struct storage_restrict_traits<dense_tag, sparse_proxy_tag> {
1.409 + typedef sparse_proxy_tag storage_category;
1.410 + };
1.411 +
1.412 + template<>
1.413 + struct storage_restrict_traits<dense_proxy_tag, packed_proxy_tag> {
1.414 + typedef packed_proxy_tag storage_category;
1.415 + };
1.416 + template<>
1.417 + struct storage_restrict_traits<dense_proxy_tag, sparse_proxy_tag> {
1.418 + typedef sparse_proxy_tag storage_category;
1.419 + };
1.420 +
1.421 +
1.422 + // Iterator tags -- hierarchical definition of storage characteristics
1.423 +
1.424 + struct sparse_bidirectional_iterator_tag : public std::bidirectional_iterator_tag {};
1.425 + struct packed_random_access_iterator_tag : public std::random_access_iterator_tag {};
1.426 + struct dense_random_access_iterator_tag : public packed_random_access_iterator_tag {};
1.427 +
1.428 + // Thanks to Kresimir Fresl for convincing Comeau with iterator_base_traits ;-)
1.429 + template<class IC>
1.430 + struct iterator_base_traits {};
1.431 +
1.432 + template<>
1.433 + struct iterator_base_traits<std::forward_iterator_tag> {
1.434 + template<class I, class T>
1.435 + struct iterator_base {
1.436 + typedef forward_iterator_base<std::forward_iterator_tag, I, T> type;
1.437 + };
1.438 + };
1.439 +
1.440 + template<>
1.441 + struct iterator_base_traits<std::bidirectional_iterator_tag> {
1.442 + template<class I, class T>
1.443 + struct iterator_base {
1.444 + typedef bidirectional_iterator_base<std::bidirectional_iterator_tag, I, T> type;
1.445 + };
1.446 + };
1.447 + template<>
1.448 + struct iterator_base_traits<sparse_bidirectional_iterator_tag> {
1.449 + template<class I, class T>
1.450 + struct iterator_base {
1.451 + typedef bidirectional_iterator_base<sparse_bidirectional_iterator_tag, I, T> type;
1.452 + };
1.453 + };
1.454 +
1.455 + template<>
1.456 + struct iterator_base_traits<std::random_access_iterator_tag> {
1.457 + template<class I, class T>
1.458 + struct iterator_base {
1.459 + typedef random_access_iterator_base<std::random_access_iterator_tag, I, T> type;
1.460 + };
1.461 + };
1.462 + template<>
1.463 + struct iterator_base_traits<packed_random_access_iterator_tag> {
1.464 + template<class I, class T>
1.465 + struct iterator_base {
1.466 + typedef random_access_iterator_base<packed_random_access_iterator_tag, I, T> type;
1.467 + };
1.468 + };
1.469 + template<>
1.470 + struct iterator_base_traits<dense_random_access_iterator_tag> {
1.471 + template<class I, class T>
1.472 + struct iterator_base {
1.473 + typedef random_access_iterator_base<dense_random_access_iterator_tag, I, T> type;
1.474 + };
1.475 + };
1.476 +
1.477 + template<class I1, class I2>
1.478 + struct iterator_restrict_traits {
1.479 + typedef I1 iterator_category;
1.480 + };
1.481 +
1.482 + template<>
1.483 + struct iterator_restrict_traits<packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag> {
1.484 + typedef sparse_bidirectional_iterator_tag iterator_category;
1.485 + };
1.486 + template<>
1.487 + struct iterator_restrict_traits<sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag> {
1.488 + typedef sparse_bidirectional_iterator_tag iterator_category;
1.489 + };
1.490 +
1.491 + template<>
1.492 + struct iterator_restrict_traits<dense_random_access_iterator_tag, sparse_bidirectional_iterator_tag> {
1.493 + typedef sparse_bidirectional_iterator_tag iterator_category;
1.494 + };
1.495 + template<>
1.496 + struct iterator_restrict_traits<sparse_bidirectional_iterator_tag, dense_random_access_iterator_tag> {
1.497 + typedef sparse_bidirectional_iterator_tag iterator_category;
1.498 + };
1.499 +
1.500 + template<>
1.501 + struct iterator_restrict_traits<dense_random_access_iterator_tag, packed_random_access_iterator_tag> {
1.502 + typedef packed_random_access_iterator_tag iterator_category;
1.503 + };
1.504 + template<>
1.505 + struct iterator_restrict_traits<packed_random_access_iterator_tag, dense_random_access_iterator_tag> {
1.506 + typedef packed_random_access_iterator_tag iterator_category;
1.507 + };
1.508 +
1.509 + template<class I>
1.510 + BOOST_UBLAS_INLINE
1.511 + void increment (I &it, const I &it_end, typename I::difference_type compare, packed_random_access_iterator_tag) {
1.512 + it += (std::min) (compare, it_end - it);
1.513 + }
1.514 + template<class I>
1.515 + BOOST_UBLAS_INLINE
1.516 + void increment (I &it, const I &/* it_end */, typename I::difference_type /* compare */, sparse_bidirectional_iterator_tag) {
1.517 + ++ it;
1.518 + }
1.519 + template<class I>
1.520 + BOOST_UBLAS_INLINE
1.521 + void increment (I &it, const I &it_end, typename I::difference_type compare) {
1.522 + increment (it, it_end, compare, typename I::iterator_category ());
1.523 + }
1.524 +
1.525 + template<class I>
1.526 + BOOST_UBLAS_INLINE
1.527 + void increment (I &it, const I &it_end) {
1.528 +#if BOOST_UBLAS_TYPE_CHECK
1.529 + I cit (it);
1.530 + while (cit != it_end) {
1.531 + BOOST_UBLAS_CHECK (*cit == typename I::value_type/*zero*/(), internal_logic ());
1.532 + ++ cit;
1.533 + }
1.534 +#endif
1.535 + it = it_end;
1.536 + }
1.537 +
1.538 +}}}
1.539 +
1.540 +#endif