|
Boost : |
From: nbecker_at_[hidden]
Date: 2001-11-02 10:20:35
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));
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk