00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef BZ_RANGE_H
00028 #define BZ_RANGE_H
00029
00030 #ifndef BZ_BLITZ_H
00031 #include <blitz/blitz.h>
00032 #endif
00033
00034 #ifndef BZ_VECEXPRWRAP_H
00035 #include <blitz/vecexprwrap.h>
00036 #endif
00037
00038 #include <climits>
00039
00040 BZ_NAMESPACE(blitz)
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 const int fromStart = INT_MIN;
00055 const int toEnd = INT_MAX;
00056
00057
00058 class Range {
00059
00060 public:
00061 typedef int T_numtype;
00062 typedef unsigned int T_sizetype;
00063
00064 Range()
00065 {
00066 first_ = fromStart;
00067 last_ = toEnd;
00068 stride_ = 1;
00069 }
00070
00071
00072 #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
00073 Range(const Range& r)
00074 {
00075 first_ = r.first_;
00076 last_ = r.last_;
00077 stride_ = r.stride_;
00078 }
00079 #endif
00080
00081 explicit Range(T_numtype slicePosition)
00082 {
00083 first_ = slicePosition;
00084 last_ = slicePosition;
00085 stride_ = 1;
00086 }
00087
00088 Range(T_numtype first, T_numtype last, T_numtype stride=1)
00089 : first_(first), last_(last), stride_(stride)
00090 {
00091 BZPRECHECK((first == fromStart) || (last == toEnd) ||
00092 ((first < last) && (stride > 0)) ||
00093 ((first > last) && (stride < 0)) ||
00094 (first == last), (*this) << " is an invalid range.");
00095 BZPRECHECK((first == fromStart) || (last == toEnd) ||
00096 (last-first) % stride == 0,
00097 (*this) << ": the stride must evenly divide the range");
00098 }
00099
00100 T_numtype first(T_numtype lowRange = 0) const
00101 {
00102 if (first_ == fromStart)
00103 return lowRange;
00104 return first_;
00105 }
00106
00107 T_numtype last(T_numtype highRange = 0) const
00108 {
00109 if (last_ == toEnd)
00110 return highRange;
00111 return last_;
00112 }
00113
00114 T_sizetype length(int =0) const
00115 {
00116 BZPRECONDITION(first_ != fromStart);
00117 BZPRECONDITION(last_ != toEnd);
00118 BZPRECONDITION((last_ - first_) % stride_ == 0);
00119 return (last_ - first_) / stride_ + 1;
00120 }
00121
00122 T_numtype stride() const
00123 { return stride_; }
00124
00125 bool isAscendingContiguous() const
00126 {
00127 return (((first_ < last_) && (stride_ == 1)) || (first_ == last_));
00128 }
00129
00130 void setRange(T_numtype first, T_numtype last, T_numtype stride=1)
00131 {
00132 BZPRECONDITION(((first < last) && (stride > 0)) ||
00133 ((first > last) && (stride < 0)) ||
00134 (first == last));
00135 BZPRECONDITION((last-first) % stride == 0);
00136 first_ = first;
00137 last_ = last;
00138 stride_ = stride;
00139 }
00140
00141 static Range all()
00142 { return Range(fromStart,toEnd,1); }
00143
00144 bool isUnitStride() const
00145 { return stride_ == 1; }
00146
00147
00148 Range operator-(T_numtype shift) const
00149 {
00150 BZPRECONDITION(first_ != fromStart);
00151 BZPRECONDITION(last_ != toEnd);
00152 return Range(first_ - shift, last_ - shift, stride_);
00153 }
00154
00155 Range operator+(T_numtype shift) const
00156 {
00157 BZPRECONDITION(first_ != fromStart);
00158 BZPRECONDITION(last_ != toEnd);
00159 return Range(first_ + shift, last_ + shift, stride_);
00160 }
00161
00162 T_numtype operator[](T_sizetype i) const
00163 {
00164 return first_ + i * stride_;
00165 }
00166
00167 T_numtype operator()(T_sizetype i) const
00168 {
00169 return first_ + i * stride_;
00170 }
00171
00172 friend inline ostream& operator<<(ostream& os, const Range& range)
00173 {
00174 os << "Range(" << range.first() << "," << range.last() << ","
00175 << range.stride() << ")";
00176
00177 return os;
00178 }
00179
00181
00182
00183
00185
00186 static const int
00187 _bz_staticLengthCount = 0,
00188 _bz_dynamicLengthCount = 0,
00189 _bz_staticLength = 0;
00190
00191 bool _bz_hasFastAccess() const
00192 { return stride_ == 1; }
00193
00194 int _bz_fastAccess(unsigned int i) const
00195 { return first_ + i; }
00196
00197 unsigned int _bz_suggestLength() const
00198 {
00199 return length();
00200 }
00201
00202 _bz_VecExpr<Range> _bz_asVecExpr() const
00203 { return _bz_VecExpr<Range>(*this); }
00204
00205 private:
00206 T_numtype first_, last_, stride_;
00207 };
00208
00209 BZ_NAMESPACE_END
00210
00211 #endif // BZ_RANGE_H