os/ossrv/ossrv_pub/boost_apis/boost/numeric/ublas/io.hpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/numeric/ublas/io.hpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,252 @@
     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_IO_
    1.21 +#define _BOOST_UBLAS_IO_
    1.22 +
    1.23 +// Only forward definition required to define stream operations
    1.24 +#include <iosfwd>
    1.25 +#include <boost/numeric/ublas/matrix_expression.hpp>
    1.26 +
    1.27 +
    1.28 +namespace boost { namespace numeric { namespace ublas {
    1.29 +
    1.30 +    template<class E, class T, class VE>
    1.31 +    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
    1.32 +    std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
    1.33 +                                           const vector_expression<VE> &v) {
    1.34 +        typedef typename VE::size_type size_type;
    1.35 +        size_type size = v ().size ();
    1.36 +        std::basic_ostringstream<E, T, std::allocator<E> > s;
    1.37 +        s.flags (os.flags ());
    1.38 +        s.imbue (os.getloc ());
    1.39 +        s.precision (os.precision ());
    1.40 +        s << '[' << size << "](";
    1.41 +        if (size > 0)
    1.42 +            s << v () (0);
    1.43 +        for (size_type i = 1; i < size; ++ i)
    1.44 +            s << ',' << v () (i);
    1.45 +        s << ')';
    1.46 +        return os << s.str ().c_str ();
    1.47 +    }
    1.48 +
    1.49 +    template<class E, class T, class VT, class VA>
    1.50 +    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
    1.51 +    std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
    1.52 +                                           vector<VT, VA> &v) {
    1.53 +        typedef typename vector<VT, VA>::size_type size_type;
    1.54 +        E ch;
    1.55 +        size_type size;
    1.56 +        if (is >> ch && ch != '[') {
    1.57 +            is.putback (ch);
    1.58 +            is.setstate (std::ios_base::failbit);
    1.59 +        } else if (is >> size >> ch && ch != ']') {
    1.60 +            is.putback (ch);
    1.61 +            is.setstate (std::ios_base::failbit);
    1.62 +        } else if (! is.fail ()) {
    1.63 +            vector<VT, VA> s (size);
    1.64 +            if (is >> ch && ch != '(') {
    1.65 +                is.putback (ch);
    1.66 +                is.setstate (std::ios_base::failbit);
    1.67 +            } else if (! is.fail ()) {
    1.68 +                for (size_type i = 0; i < size; i ++) {
    1.69 +                    if (is >> s (i) >> ch && ch != ',') {
    1.70 +                        is.putback (ch);
    1.71 +                        if (i < size - 1)
    1.72 +                            is.setstate (std::ios_base::failbit);
    1.73 +                        break;
    1.74 +                    }
    1.75 +                }
    1.76 +                if (is >> ch && ch != ')') {
    1.77 +                    is.putback (ch);
    1.78 +                    is.setstate (std::ios_base::failbit);
    1.79 +                }
    1.80 +            }
    1.81 +            if (! is.fail ())
    1.82 +                v.swap (s);
    1.83 +        }
    1.84 +        return is;
    1.85 +    }
    1.86 +
    1.87 +    template<class E, class T, class ME>
    1.88 +    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
    1.89 +    std::basic_ostream<E, T> &operator << (std::basic_ostream<E, T> &os,
    1.90 +                                           const matrix_expression<ME> &m) {
    1.91 +        typedef typename ME::size_type size_type;
    1.92 +        size_type size1 = m ().size1 ();
    1.93 +        size_type size2 = m ().size2 ();
    1.94 +        std::basic_ostringstream<E, T, std::allocator<E> > s;
    1.95 +        s.flags (os.flags ());
    1.96 +        s.imbue (os.getloc ());
    1.97 +        s.precision (os.precision ());
    1.98 +        s << '[' << size1 << ',' << size2 << "](";
    1.99 +        if (size1 > 0) {
   1.100 +            s << '(' ;
   1.101 +            if (size2 > 0)
   1.102 +                s << m () (0, 0);
   1.103 +            for (size_type j = 1; j < size2; ++ j)
   1.104 +                s << ',' << m () (0, j);
   1.105 +            s << ')';
   1.106 +        }
   1.107 +        for (size_type i = 1; i < size1; ++ i) {
   1.108 +            s << ",(" ;
   1.109 +            if (size2 > 0)
   1.110 +                s << m () (i, 0);
   1.111 +            for (size_type j = 1; j < size2; ++ j)
   1.112 +                s << ',' << m () (i, j);
   1.113 +            s << ')';
   1.114 +        }
   1.115 +        s << ')';
   1.116 +        return os << s.str ().c_str ();
   1.117 +    }
   1.118 +
   1.119 +    template<class E, class T, class MT, class MF, class MA>
   1.120 +    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
   1.121 +    std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
   1.122 +                                           matrix<MT, MF, MA> &m) {
   1.123 +        typedef typename matrix<MT, MF, MA>::size_type size_type;
   1.124 +        E ch;
   1.125 +        size_type size1, size2;
   1.126 +        if (is >> ch && ch != '[') {
   1.127 +            is.putback (ch);
   1.128 +            is.setstate (std::ios_base::failbit);
   1.129 +        } else if (is >> size1 >> ch && ch != ',') {
   1.130 +            is.putback (ch);
   1.131 +            is.setstate (std::ios_base::failbit);
   1.132 +        } else if (is >> size2 >> ch && ch != ']') {
   1.133 +            is.putback (ch);
   1.134 +            is.setstate (std::ios_base::failbit);
   1.135 +        } else if (! is.fail ()) {
   1.136 +            matrix<MT, MF, MA> s (size1, size2);
   1.137 +            if (is >> ch && ch != '(') {
   1.138 +                is.putback (ch);
   1.139 +                is.setstate (std::ios_base::failbit);
   1.140 +            } else if (! is.fail ()) {
   1.141 +                for (size_type i = 0; i < size1; i ++) {
   1.142 +                    if (is >> ch && ch != '(') {
   1.143 +                        is.putback (ch);
   1.144 +                        is.setstate (std::ios_base::failbit);
   1.145 +                        break;
   1.146 +                    }
   1.147 +                    for (size_type j = 0; j < size2; j ++) {
   1.148 +                        if (is >> s (i, j) >> ch && ch != ',') {
   1.149 +                            is.putback (ch);
   1.150 +                            if (j < size2 - 1) {
   1.151 +                                is.setstate (std::ios_base::failbit);
   1.152 +                                break;
   1.153 +                            }
   1.154 +                        }
   1.155 +                    }
   1.156 +                    if (is >> ch && ch != ')') {
   1.157 +                        is.putback (ch);
   1.158 +                        is.setstate (std::ios_base::failbit);
   1.159 +                        break;
   1.160 +                    }
   1.161 +                    if (is >> ch && ch != ',') {
   1.162 +                       is.putback (ch);
   1.163 +                       if (i < size1 - 1) {
   1.164 +                            is.setstate (std::ios_base::failbit);
   1.165 +                            break;
   1.166 +                       }
   1.167 +                    }
   1.168 +                }
   1.169 +                if (is >> ch && ch != ')') {
   1.170 +                    is.putback (ch);
   1.171 +                    is.setstate (std::ios_base::failbit);
   1.172 +                }
   1.173 +            }
   1.174 +            if (! is.fail ())
   1.175 +                m.swap (s);
   1.176 +        }
   1.177 +        return is;
   1.178 +    }
   1.179 +
   1.180 +    // Special input operator for symmetrix_matrix
   1.181 +    template<class E, class T, class MT, class MF1, class MF2, class MA>
   1.182 +    // BOOST_UBLAS_INLINE This function seems to be big. So we do not let the compiler inline it.
   1.183 +    std::basic_istream<E, T> &operator >> (std::basic_istream<E, T> &is,
   1.184 +                                           symmetric_matrix<MT, MF1, MF2, MA> &m) {
   1.185 +        typedef typename symmetric_matrix<MT, MF1, MF2, MA>::size_type size_type;
   1.186 +        E ch;
   1.187 +        size_type size1, size2;
   1.188 +        MT value;
   1.189 +        if (is >> ch && ch != '[') {
   1.190 +            is.putback (ch);
   1.191 +            is.setstate (std::ios_base::failbit);
   1.192 +        } else if (is >> size1 >> ch && ch != ',') {
   1.193 +            is.putback (ch);
   1.194 +            is.setstate (std::ios_base::failbit);
   1.195 +        } else if (is >> size2 >> ch && (size2 != size1 || ch != ']')) { // symmetric matrix must be square
   1.196 +            is.putback (ch);
   1.197 +            is.setstate (std::ios_base::failbit);
   1.198 +        } else if (! is.fail ()) {
   1.199 +            symmetric_matrix<MT, MF1, MF2, MA> s (size1, size2);
   1.200 +            if (is >> ch && ch != '(') {
   1.201 +                is.putback (ch);
   1.202 +                is.setstate (std::ios_base::failbit);
   1.203 +             } else if (! is.fail ()) {
   1.204 +                for (size_type i = 0; i < size1; i ++) {
   1.205 +                    if (is >> ch && ch != '(') {
   1.206 +                        is.putback (ch);
   1.207 +                        is.setstate (std::ios_base::failbit);
   1.208 +                        break;
   1.209 +                    }
   1.210 +                    for (size_type j = 0; j < size2; j ++) {
   1.211 +                        if (is >> value >> ch && ch != ',') {
   1.212 +                            is.putback (ch);
   1.213 +                            if (j < size2 - 1) {
   1.214 +                                is.setstate (std::ios_base::failbit);
   1.215 +                                break;
   1.216 +                            }
   1.217 +                        }
   1.218 +                        if (i <= j) { 
   1.219 +                             // this is the first time we read this element - set the value
   1.220 +                            s(i,j) = value;
   1.221 +                        }
   1.222 +                        else if ( s(i,j) != value ) {
   1.223 +                            // matrix is not symmetric
   1.224 +                            is.setstate (std::ios_base::failbit);
   1.225 +                            break;
   1.226 +                        }
   1.227 +                     }
   1.228 +                     if (is >> ch && ch != ')') {
   1.229 +                         is.putback (ch);
   1.230 +                         is.setstate (std::ios_base::failbit);
   1.231 +                         break;
   1.232 +                     }
   1.233 +                     if (is >> ch && ch != ',') {
   1.234 +                        is.putback (ch);
   1.235 +                        if (i < size1 - 1) {
   1.236 +                             is.setstate (std::ios_base::failbit);
   1.237 +                             break;
   1.238 +                        }
   1.239 +                     }
   1.240 +                }
   1.241 +                if (is >> ch && ch != ')') {
   1.242 +                    is.putback (ch);
   1.243 +                    is.setstate (std::ios_base::failbit);
   1.244 +                }
   1.245 +            }
   1.246 +            if (! is.fail ())
   1.247 +                m.swap (s);
   1.248 +        }
   1.249 +        return is;
   1.250 +    }
   1.251 + 
   1.252 +
   1.253 +}}}
   1.254 +
   1.255 +#endif