|
Boost : |
From: nbecker_at_[hidden]
Date: 2001-11-19 08:44:23
Use iterators and adapt between complex and real with an iterator
adaptor.
Here's what I use:
note: I don't think this can easily be based on boost/iterator_adaptors, because we
require an adaptor that has state.
//! Convert complex to interleaved scalars
template<class T, class ComplexIterator>
class CtoR {
ComplexIterator I;
int cnt;
public:
typedef T value_type;
typedef CtoR self;
typedef ptrdiff_t difference_type;
CtoR (ComplexIterator C) :
I (C),
cnt (0)
{}
value_type operator*() const {
return (cnt % 2 == 0) ? real (*I) : imag (*I);
}
self& operator++ () {
cnt++;
if (cnt == 2) {
I++;
cnt = 0;
}
return *this;
}
self operator++ (int) {
self tmp = *this;
++*this;
return tmp;
}
bool operator==(const self& x) const { return I == x.I && cnt == x.cnt; }
bool operator!=(const self& x) const { return !(*this == x); }
difference_type operator- (const self& x) const {
return 2 * (I - x.I) + (cnt - x.cnt);
}
self operator+(difference_type n) const {
self tmp = *this;
return tmp += n;
}
self& operator+=(difference_type n) {
I += n/2;
cnt += n % 2;
if (cnt == 2) {
I++;
cnt = 0;
}
return *this;
}
};
//! convert interleaved scalars to complex
template<class T, class ScalarIterator>
class RtoC {
ScalarIterator I;
public:
typedef complex<T> value_type;
typedef RtoC self;
typedef ptrdiff_t difference_type;
RtoC (ScalarIterator S) :
I (S)
{}
value_type operator*() const {
return complex<T> (*I, *(I+1));
}
self& operator++ () {
I += 2;
return *this;
}
self operator++ (int) {
self tmp = *this;
++*this;
return tmp;
}
bool operator==(const self& x) const { return I == x.I; }
value_type operator[] (difference_type n) const { return *(*this + n); }
self operator+(difference_type n) const {
self tmp = *this;
return tmp += n;
}
self& operator+=(difference_type n) {
I += 2 * n;
return *this;
}
};
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk