00001
00015 #ifndef _TENSORARRAYRECURSIVEITERATOR_H
00016 #define _TENSORARRAYRECURSIVEITERATOR_H
00017
00018 #include <TensorArray.H>
00019
00020 namespace xchen
00021 {
00022 template<typename T, int dims>
00023 class TensorArraySliceIterator
00024 {
00025 int d, s, e, i;
00026 TensorArray<T,dims>*ar;
00027
00028 typedef TensorArraySliceIterator<T,dims> MyType;
00029
00030
00031 public:
00032 TensorArraySliceIterator(TensorArray<T,dims>*array, int dir=0, int start=0, int end=0) :
00033 d(dir), s(start), e(end? end:array->GetSize(dir)), ar(array) { i = start; }
00034
00035 bool PastEnd() const { return i >= e; }
00036
00038 MyType& operator++() { i++; return *this; }
00039 MyType& operator+=(int inc) { i += inc; return *this; }
00040 MyType operator+(int inc) { MyType ret(*this); ret += inc; return ret; }
00041 MyType& operator--() { i--; return *this; }
00042 MyType& operator-=(int inc) { i -= inc; return *this; }
00043 MyType operator-(int inc) { MyType ret(*this); ret -= inc; return ret; }
00045
00047 MyType& operator=(MyType const& rhs ) { copy(rhs.Begin(), rhs.End(), Begin()); return *this; }
00048 MyType& operator*=(double scale) { transform(Begin(), End(), Begin(), scale_fun(scale)); return *this; }
00049 MyType& operator/=(double denom) { transform(Begin(), End(), Begin(), scale_fun(1/denom)); return *this; }
00050 MyType& operator+=(MyType const& rhs) { transform(rhs.Begin(), rhs.End(), Begin(), Begin(), plus<T>()); return *this; }
00051 MyType& operator-=(MyType const& rhs) { transform(rhs.Begin(), rhs.End(), Begin(), Begin(), minus<T>()); return *this; }
00053
00054 TensorArray1DIterator<T,dims> Begin() const { return ((TensorArray<T,dims>*)ar)->LinearIterate(d, i, 1).Begin(); }
00055 TensorArray1DIterator<T,dims> End() const { return ((TensorArray<T,dims>*)ar)->LinearIterate(d, i, 1).End(); }
00056
00057 private:
00058 struct scale_fun : public unary_function<const T&, double>
00059 {
00060 scale_fun(double s) : scale(s) { }
00061 double scale;
00062 T operator()(const T& x) { return x*scale; }
00063 };
00064 };
00065
00066
00067 }
00068
00069
00070 #endif