This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Patch for complex streaming
- To: libstdc++ at sourceware dot cygnus dot com
- Subject: Patch for complex streaming
- From: Petter Urkedal <petter at matfys dot lth dot se>
- Date: Sat, 12 Feb 2000 16:12:11 +0100 (MET)
At the moment the complex streaming operators does not make it
into the library, since they depend on template parameters.
Further, input is only a stub. The Attached files
src/complexio.cc
testsuite/26_numerics/complexio_input.cc
and the patch should fix this. Please note:
-- Since the stream operators have a different character than
the rest of the operators in `src/complex.cc' -- they depend on
more template parameters, and their specializations for float,
double and long double needs no special treatment -- I decided
to move them into a separate file `src/complexio.cc'. OK?
-- At present all instantiations are in one file. Do you think
we shall split them into a dozen of file like done for the string
stuff?
Requests? Otherwise,
2000-02-12 Petter Urkedal <petter@matfys.lth.se>
* std/complex.cc (operator<<, operator>>): Moved from here...
* src/complexio.cc: ...to new file.
(operator>>): Stub replaced by the real thing.
* src/Makefile.am (sources): Inserted complexio.cc.
* testsuite/26_numerics/complexio_input.cc: Check it.
Cheers,
-petter.
// The template and inlines for the -*- C++ -*- complex number classes.
// Copyright (C) 2000 Cygnus Solutions
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
// As a special exception, you may use this file as part of a free software
// library without restriction. Specifically, if other files instantiate
// templates or use macros or inline functions from this file, or you compile
// this file and link it with other files to produce an executable, this
// file does not by itself cause the resulting executable to be covered by
// the GNU General Public License. This exception does not however
// invalidate any other reasons why the executable file might be covered by
// the GNU General Public License.
#include <bits/std_complex.h>
#include <bits/std_istream.h>
#include <bits/std_ostream.h>
#include <bits/std_sstream.h>
namespace std
{
template<typename _Tp, typename _CharT, class _Traits>
basic_istream <_CharT, _Traits> &
operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
{
_Tp __re_x, __im_x;
_CharT __ch;
__is >> __ch;
if (__ch == '(') {
__is >> __re_x >> __ch;
if (__ch == ',') {
__is >> __im_x >> __ch;
if (__ch == ')') {
__x = complex<_Tp>(__re_x, __im_x);
return __is;
}
}
else if (__ch == ')') {
__x = complex<_Tp>(__re_x, _Tp(0));
return __is;
}
}
else {
__is.putback(__ch);
__is >> __re_x;
__x = complex<_Tp>(__re_x, _Tp(0));
return __is;
}
__is.setstate(ios_base::failbit);
return __is;
}
template<typename _Tp, typename _CharT, class _Traits>
basic_ostream<_CharT, _Traits>&
operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
{
basic_ostringstream<_CharT, _Traits> __s;
__s.flags(__os.flags());
#ifdef _G_HAVE_LOCALE
__s.imbue(__os.getloc());
#endif
__s.precision (__os.precision());
__s << '(' << __x.real() << "," << __x.imag() << ')' << ends;
return __os << __s.str();
}
template
basic_istream< char, char_traits<char> >&
operator>>(basic_istream< char, char_traits<char> >&,
complex<float>&);
template
basic_ostream< char, char_traits<char> >&
operator<<(basic_ostream< char, char_traits<char> >&,
const complex<float>&);
template
basic_istream< char, char_traits<char> >&
operator>>(basic_istream< char, char_traits<char> >&,
complex<double>&);
template
basic_ostream< char, char_traits<char> >&
operator<<(basic_ostream< char, char_traits<char> >&,
const complex<double>&);
template
basic_istream< char, char_traits<char> >&
operator>>(basic_istream< char, char_traits<char> >&,
complex<long double>&);
template
basic_ostream< char, char_traits<char> >&
operator<<(basic_ostream< char, char_traits<char> >&,
const complex<long double>&);
template
basic_istream< wchar_t, char_traits<wchar_t> >&
operator>>(basic_istream< wchar_t, char_traits<wchar_t> >&,
complex<float>&);
template
basic_ostream< wchar_t, char_traits<wchar_t> >&
operator<<(basic_ostream< wchar_t, char_traits<wchar_t> >&,
const complex<float>&);
template
basic_istream< wchar_t, char_traits<wchar_t> >&
operator>>(basic_istream< wchar_t, char_traits<wchar_t> >&,
complex<double>&);
template
basic_ostream< wchar_t, char_traits<wchar_t> >&
operator<<(basic_ostream< wchar_t, char_traits<wchar_t> >&,
const complex<double>&);
template
basic_istream< wchar_t, char_traits<wchar_t> >&
operator>>(basic_istream< wchar_t, char_traits<wchar_t> >&,
complex<long double>&);
template
basic_ostream< wchar_t, char_traits<wchar_t> >&
operator<<(basic_ostream< wchar_t, char_traits<wchar_t> >&,
const complex<long double>&);
}
// 2000-02-10
// Petter Urkedal <petter@matfys.lth.se>
// Copyright (C) 2000 Free Software Foundation
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING. If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
#include <iostream>
#include <string>
#include <sstream>
#include <complex>
template<typename R>
inline bool flteq(R x, R y)
{
if (x == R(0)) return y == R(0);
else return fabs(x-y) < 1e-6*fabs(x);
}
template<typename R>
void test_good(std::string str, R x, R y)
{
std::complex<R> z;
char ch;
std::istringstream iss(str);
iss >> z >> ch;
assert(iss.good());
assert(flteq(z.real(), x));
assert(flteq(z.imag(), y));
assert(ch == '#');
}
template<typename R>
void test_fail(std::string str)
{
std::complex<R> z;
std::istringstream iss(str);
iss >> z;
assert(iss.fail() && !iss.bad());
}
template<typename R>
int testall()
{
test_good<R>("(-1.1,3.7)#", -1.1, 3.7);
test_good<R>("( .7e6 , \n-3.1)#", .7e6, -3.1);
test_good<R>("(\t0,-1)#", 0.0, -1.0);
test_good<R>("(-3.14)#", -3.14, 0.0);
test_good<R>("-.1#", -.1, 0.0);
test_good<R>(" ( -2.7e3 )#", -2.7e3, 0.0);
test_good<R>(" -.1#", -.1, 0.0);
test_fail<R>("(a,1)");
test_fail<R>("(,1)");
test_fail<R>("(1,a)");
test_fail<R>("(1, )");
test_fail<R>("|1,1)");
test_fail<R>("(1|1)");
test_fail<R>("(1,1|");
}
int main()
{
testall<float>();
testall<double>();
testall<long double>();
return 0;
}
Index: src/Makefile.am
===================================================================
RCS file: /cvs/libstdc++/libstdc++/src/Makefile.am,v
retrieving revision 1.69
diff -c -r1.69 Makefile.am
*** Makefile.am 2000/02/11 11:27:32 1.69
--- Makefile.am 2000/02/12 13:50:12
***************
*** 124,130 ****
sources = \
limitsMEMBERS.cc \
! complex.cc complexf.cc complexl.cc \
stdexcept.cc ios.cc stdstreams.cc \
locale.cc localename.cc basic_file.cc \
locale-inst.cc stl-inst.cc misc-inst.cc valarray-inst.cc \
--- 124,130 ----
sources = \
limitsMEMBERS.cc \
! complex.cc complexf.cc complexl.cc complexio.cc \
stdexcept.cc ios.cc stdstreams.cc \
locale.cc localename.cc basic_file.cc \
locale-inst.cc stl-inst.cc misc-inst.cc valarray-inst.cc \
Index: src/complex.cc
===================================================================
RCS file: /cvs/libstdc++/libstdc++/src/complex.cc,v
retrieving revision 1.18
diff -c -r1.18 complex.cc
*** complex.cc 2000/02/09 18:06:34 1.18
--- complex.cc 2000/02/12 13:50:17
***************
*** 42,70 ****
namespace std
{
- template<typename _Tp, typename _CharT, class _Traits>
- basic_istream <_CharT, _Traits> &
- operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
- {
- // XXX TODO
- return __is;
- }
-
- template<typename _CharT, class _Traits>
- basic_ostream<_CharT, _Traits>&
- operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<FLT>& __x)
- {
- basic_ostringstream<_CharT, _Traits> __s;
- __s.flags(__os.flags());
- #ifdef _G_HAVE_LOCALE
- __s.imbue(__os.getloc());
- #endif
- __s.precision (__os.precision());
- __s << '(' << __x.real() << "," << __x.imag() << ')' << ends;
- return __os << __s.str();
- }
-
-
template<>
FLT
abs(const complex<FLT>& __x)
--- 42,47 ----