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