|
Boost : |
From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-11-02 19:03:50
At first blush I don't see an error, but it's likely I've overlooked
something. Have you tried to run this in a debugger? I suggest you try that
first, and if you still can't see anything, that you supply a stack
backtrace.
One thing that occurs to me is that you may need to implement the "equals"
policy member, at least in the CtoR case. I don't think equality can be
determined solely by the result of operator== on the Base iterators. I don't
think this is your problem, though.
You might also consider basing your adaptor on transform_iterator_adaptor,
which should save you lots of work.
===================================================
David Abrahams, C++ library designer for hire
resume: http://users.rcn.com/abrahams/resume.html
C++ Booster (http://www.boost.org)
email: david.abrahams_at_[hidden]
===================================================
----- Original Message -----
From: <nbecker_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Friday, November 02, 2001 10:20 AM
Subject: [boost] complex-real iterator adaptors help
> I am trying to use boost iterator adaptors to adapt between complex
> and scalar. When I try to compile using gcc-3.0.2 I get a segfault.
> Is that because my code is invalid?
>
> Here is my attempt:
>
> #ifndef RtoC_H
> #define RtoC_H
>
> #include <complex>
> #include <iterator>
> #include <boost/iterator_adaptors.hpp>
>
> using std::complex;
> using std::iterator_traits;
> using boost::default_iterator_policies;
> using boost::type;
> using boost::iterator_adaptor;
>
> struct RtoC_policies : public default_iterator_policies
> {
> public:
>
> template <class Reference, class BaseType>
> Reference dereference(type<Reference>, const BaseType& x) const
> { return complex<iterator_traits<BaseType>::value_type> (*x, *(x+1)); }
>
>
> template <class BaseType>
> void increment(BaseType& x)
> { x += 2; }
>
>
> template <class BaseType>
> void decrement(BaseType& x)
> { x -= 2; }
>
> template <class BaseType, class DifferenceType>
> void advance(BaseType& x, DifferenceType n)
> { x += 2*n; }
>
> template <class Difference, class BaseType1, class BaseType2>
> Difference distance(type<Difference>, const BaseType1& x, const
BaseType2& y) const
> { return (y - x)/2; }
> };
>
> template <class Iterator>
> struct RtoC_iterator_generator
> {
> typedef complex<iterator_traits<Iterator>::value_type> value_type;
> public:
> typedef iterator_adaptor<Iterator,
> RtoC_policies,
> value_type, value_type, value_type*, std::input_iterator_tag>
> type;
> };
>
> template <class Iterator>
> typename RtoC_iterator_generator<Iterator>::type
> make_RtoC_iterator(Iterator base)
> {
> typedef typename RtoC_iterator_generator<Iterator>::type result_t;
> return result_t(base);
> }
>
> struct CtoR_policies : public default_iterator_policies
> {
> int cnt;
> public:
>
> template <class Reference, class BaseType>
> Reference dereference(type<Reference>, const BaseType& x) const
> { return (cnt % 2 == 0) ? real (x) : imag (x); }
>
>
> template <class BaseType>
> void increment(BaseType& x) {
> cnt++;
> if (cnt == 2) {
> x++;
> cnt = 0;
> }
> }
>
>
> template <class BaseType>
> void decrement(BaseType& x) {
> if (cnt == 0) {
> x--;
> cnt = 1;
> }
> else
> cnt--;
> }
>
> template <class BaseType, class DifferenceType>
> void advance(BaseType& x, DifferenceType n) {
> x += n/2;
> cnt += n % 2;
> if (cnt == 2) {
> x++;
> cnt = 0;
> }
> }
>
> template <class Difference, class BaseType1, class BaseType2>
> Difference distance(type<Difference>, const BaseType1& x, const
BaseType2& y) const {
> return 2 * (y - x) + (y.cnt - x.cnt);
> }
>
> };
>
> template <class Iterator>
> struct CtoR_iterator_generator
> {
> typedef iterator_traits<Iterator>::value_type c_value_type;
> typedef c_value_type::value_type my_value_type;
> public:
> typedef iterator_adaptor<Iterator,
> CtoR_policies,
> my_value_type, my_value_type, my_value_type*, std::input_iterator_tag>
> type;
> };
>
> // template <class Iterator>
> // typename CtoR_iterator_generator<Iterator>::type
> // make_CtoR_iterator(Iterator base)
> // {
> // typedef typename CtoR_iterator_generator<Iterator>::type result_t;
> // return result_t(base);
> // }
>
> #endif
>
> -----------------
> #include "RtoC.H"
> #include <vector>
> #include <iostream>
> using std::vector;
> using std::ostream;
> using std::cout;
> using std::copy;
> typedef std::complex<double> Complex;
>
> int main () {
> vector<double> x (10);
> for (int i = 0; i < 10; i++)
> x[i] = i;
>
> copy (make_RtoC_iterator (x.begin()), make_RtoC_iterator (x.end()),
std::ostream_iterator<Complex> (cout, "\n"));
>
> vector<Complex> y;
> copy (make_RtoC_iterator (x.begin()), make_RtoC_iterator (x.end()),
std::back_inserter (y));
>
> }
>
> Info: http://www.boost.org Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk