epoc32/include/stdapis/stlport/stl/_complex.c
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/stdapis/stlport/stl/_complex.c	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,169 @@
     1.4 +/*
     1.5 + * Copyright (c) 1999
     1.6 + * Silicon Graphics Computer Systems, Inc.
     1.7 + *
     1.8 + * Copyright (c) 1999 
     1.9 + * Boris Fomitchev
    1.10 + *
    1.11 + * This material is provided "as is", with absolutely no warranty expressed
    1.12 + * or implied. Any use is at your own risk.
    1.13 + *
    1.14 + * Permission to use or copy this software for any purpose is hereby granted 
    1.15 + * without fee, provided the above notices are retained on all copies.
    1.16 + * Permission to modify the code and to distribute modified code is granted,
    1.17 + * provided the above notices are retained, and a notice that the code was
    1.18 + * modified is included with the above copyright notice.
    1.19 + *
    1.20 + */ 
    1.21 +#ifndef _STLP_COMPLEX_C
    1.22 +#define _STLP_COMPLEX_C
    1.23 +
    1.24 +# ifndef _STLP_internal_complex_h
    1.25 +#  include <stl/_complex.h>
    1.26 +# endif
    1.27 +
    1.28 +#include <istream>
    1.29 +
    1.30 +#ifdef _STLP_USE_NEW_IOSTREAMS
    1.31 +# include <sstream>
    1.32 +#endif
    1.33 +
    1.34 +_STLP_BEGIN_NAMESPACE
    1.35 +
    1.36 +// Non-inline member functions.
    1.37 +
    1.38 +template <class _Tp>
    1.39 +void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
    1.40 +                        const _Tp& __z2_r, const _Tp& __z2_i,
    1.41 +                        _Tp& __res_r, _Tp& __res_i) {
    1.42 +  _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
    1.43 +  _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
    1.44 +
    1.45 +  if (__ar <= __ai) {
    1.46 +    _Tp __ratio = __z2_r / __z2_i;
    1.47 +    _Tp __denom = __z2_i * (1 + __ratio * __ratio);
    1.48 +    __res_r = (__z1_r * __ratio + __z1_i) / __denom;
    1.49 +    __res_i = (__z1_i * __ratio - __z1_r) / __denom;
    1.50 +  }
    1.51 +  else {
    1.52 +    _Tp __ratio = __z2_i / __z2_r;
    1.53 +    _Tp __denom = __z2_r * (1 + __ratio * __ratio);
    1.54 +    __res_r = (__z1_r + __z1_i * __ratio) / __denom;
    1.55 +    __res_i = (__z1_i - __z1_r * __ratio) / __denom;
    1.56 +  }
    1.57 +}
    1.58 +
    1.59 +template <class _Tp>
    1.60 +void complex<_Tp>::_div(const _Tp& __z1_r,
    1.61 +                        const _Tp& __z2_r, const _Tp& __z2_i,
    1.62 +                        _Tp& __res_r, _Tp& __res_i) {
    1.63 +  _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
    1.64 +  _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
    1.65 +
    1.66 +  if (__ar <= __ai) {
    1.67 +    _Tp __ratio = __z2_r / __z2_i;
    1.68 +    _Tp __denom = __z2_i * (1 + __ratio * __ratio);
    1.69 +    __res_r = (__z1_r * __ratio) / __denom;
    1.70 +    __res_i = - __z1_r / __denom;
    1.71 +  }
    1.72 +  else {
    1.73 +    _Tp __ratio = __z2_i / __z2_r;
    1.74 +    _Tp __denom = __z2_r * (1 + __ratio * __ratio);
    1.75 +    __res_r = __z1_r / __denom;
    1.76 +    __res_i = - (__z1_r * __ratio) / __denom;
    1.77 +  }
    1.78 +}
    1.79 +
    1.80 +// I/O.
    1.81 +
    1.82 +#ifdef _STLP_USE_NEW_IOSTREAMS
    1.83 +
    1.84 +// Complex output, in the form (re,im).  We use a two-step process 
    1.85 +// involving stringstream so that we get the padding right.  
    1.86 +template <class _Tp, class _CharT, class _Traits>
    1.87 +basic_ostream<_CharT, _Traits>& _STLP_CALL
    1.88 +operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z)
    1.89 +{
    1.90 +  basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
    1.91 +  __tmp.flags(__os.flags());
    1.92 +  __tmp.imbue(__os.getloc());
    1.93 +  __tmp.precision(__os.precision());
    1.94 +  __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
    1.95 +  return __os << __tmp.str();
    1.96 +}
    1.97 +
    1.98 +// Complex input from arbitrary streams.  Note that results in some
    1.99 +// locales may be confusing, since the decimal character varies with
   1.100 +// locale and the separator between real and imaginary parts does not.
   1.101 +
   1.102 +template <class _Tp, class _CharT, class _Traits>
   1.103 +basic_istream<_CharT, _Traits>& _STLP_CALL
   1.104 +operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z)
   1.105 +{
   1.106 +  _Tp  __re = 0;
   1.107 +  _Tp  __im = 0;
   1.108 +
   1.109 +  // typedef ctype<_CharT> _Ctype;
   1.110 +  //  locale __loc = __is.getloc();
   1.111 +  //const _Ctype&  __c_type  = use_facet<_Ctype>(__loc);
   1.112 +  const ctype<_CharT>& __c_type = *(const ctype<_CharT>*)__is._M_ctype_facet();
   1.113 +
   1.114 +  char   __punct[4] = "(,)";
   1.115 +  _CharT __wpunct[3];
   1.116 +  __c_type.widen(__punct, __punct + 3, __wpunct);
   1.117 +
   1.118 +  _CharT __c;
   1.119 +
   1.120 +  __is >> __c;
   1.121 +  if (_Traits::eq(__c, __wpunct[0])) {  // Left paren
   1.122 +    __is >> __re >> __c;
   1.123 +    if (_Traits::eq(__c, __wpunct[1]))  // Comma
   1.124 +      __is >> __im >> __c;
   1.125 +    if (!_Traits::eq(__c, __wpunct[2])) // Right paren
   1.126 +      __is.setstate(ios_base::failbit);
   1.127 +  }
   1.128 +  else {
   1.129 +    __is.putback(__c);
   1.130 +    __is >> __re;
   1.131 +  }
   1.132 +
   1.133 +  if (__is)
   1.134 +    __z = complex<_Tp>(__re, __im);
   1.135 +  return __is;
   1.136 +}
   1.137 +
   1.138 +
   1.139 +#else /* _STLP_USE_NEW_IOSTREAMS */
   1.140 +
   1.141 +template <class _Tp>
   1.142 +ostream& _STLP_CALL operator<<(ostream& s, const complex<_Tp>& __z)
   1.143 +{
   1.144 +  return s << "( " << __z._M_re <<", " << __z._M_im <<")";
   1.145 +}
   1.146 +
   1.147 +template <class _Tp>
   1.148 +istream& _STLP_CALL operator>>(istream& s, complex<_Tp>& a)
   1.149 +{
   1.150 +  _Tp re = 0, im = 0;
   1.151 +  char 	c = 0;
   1.152 +
   1.153 +  s >> c;
   1.154 +  if (c == '(') {
   1.155 +    s >> re >> c;
   1.156 +    if (c == ',') s >> im >> c;
   1.157 +    if (c != ')') s.clear(ios::badbit);
   1.158 +  }
   1.159 +  else {
   1.160 +    s.putback(c);
   1.161 +    s >> re;
   1.162 +  }
   1.163 +
   1.164 +  if (s) a = complex<_Tp>(re, im);
   1.165 +  return s;
   1.166 +}
   1.167 +
   1.168 +#endif /* _STLP_USE_NEW_IOSTREAMS */
   1.169 +
   1.170 +_STLP_END_NAMESPACE
   1.171 +
   1.172 +#endif /* _STLP_COMPLEX_C */