[658] | 1 | /***************************************************************************
|
---|
| 2 | * blitz/range.h Declaration of the Range class
|
---|
| 3 | *
|
---|
| 4 | * $Id: range.h,v 1.1.1.1 1999-11-26 16:37:04 ansari Exp $
|
---|
| 5 | *
|
---|
| 6 | * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
|
---|
| 7 | *
|
---|
| 8 | * This program is free software; you can redistribute it and/or
|
---|
| 9 | * modify it under the terms of the GNU General Public License
|
---|
| 10 | * as published by the Free Software Foundation; either version 2
|
---|
| 11 | * of the License, or (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * This program is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * Suggestions: blitz-suggest@cybervision.com
|
---|
| 19 | * Bugs: blitz-bugs@cybervision.com
|
---|
| 20 | *
|
---|
| 21 | * For more information, please see the Blitz++ Home Page:
|
---|
| 22 | * http://seurat.uwaterloo.ca/blitz/
|
---|
| 23 | *
|
---|
| 24 | ***************************************************************************
|
---|
| 25 | * $Log: not supported by cvs2svn $
|
---|
| 26 | * Revision 1.1.1.1 1999/04/09 17:58:59 ansari
|
---|
| 27 | * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
|
---|
| 28 | *
|
---|
| 29 | * Revision 1.6 1998/03/14 00:04:47 tveldhui
|
---|
| 30 | * 0.2-alpha-05
|
---|
| 31 | *
|
---|
| 32 | * Revision 1.5 1997/07/16 14:51:20 tveldhui
|
---|
| 33 | * Update: Alpha release 0.2 (Arrays)
|
---|
| 34 | *
|
---|
| 35 | * Revision 1.4 1997/01/24 14:42:00 tveldhui
|
---|
| 36 | * Periodic RCS update
|
---|
| 37 | *
|
---|
| 38 | * Revision 1.3 1997/01/23 03:28:28 tveldhui
|
---|
| 39 | * Periodic RCS update
|
---|
| 40 | *
|
---|
| 41 | * Revision 1.2 1997/01/13 22:19:58 tveldhui
|
---|
| 42 | * Periodic RCS update
|
---|
| 43 | *
|
---|
| 44 | * Revision 1.1 1996/11/11 17:29:13 tveldhui
|
---|
| 45 | * Initial revision
|
---|
| 46 | *
|
---|
| 47 | *
|
---|
| 48 | */
|
---|
| 49 |
|
---|
| 50 | #ifndef BZ_RANGE_H
|
---|
| 51 | #define BZ_RANGE_H
|
---|
| 52 |
|
---|
| 53 | #ifndef BZ_BLITZ_H
|
---|
| 54 | #include <blitz/blitz.h>
|
---|
| 55 | #endif
|
---|
| 56 |
|
---|
| 57 | #ifndef BZ_VECEXPRWRAP_H
|
---|
| 58 | #include <blitz/vecexprwrap.h> // _bz_VecExpr wrapper
|
---|
| 59 | #endif
|
---|
| 60 |
|
---|
| 61 | #include <limits.h> // for INT_MIN
|
---|
| 62 |
|
---|
| 63 | BZ_NAMESPACE(blitz)
|
---|
| 64 |
|
---|
| 65 | // Examples:
|
---|
| 66 | // Vector<double> x(7);
|
---|
| 67 | // Range::all() [0,1,2,3,4,5,6]
|
---|
| 68 | // Range(3,5) [3,4,5]
|
---|
| 69 | // Range(3,Range::toEnd) [3,4,5,6]
|
---|
| 70 | // Range(Range::fromStart,3) [0,1,2,3]
|
---|
| 71 | // Range(1,5,2); [1,3,5]
|
---|
| 72 |
|
---|
| 73 | enum { fromStart = INT_MIN, toEnd = INT_MIN };
|
---|
| 74 |
|
---|
| 75 | // Class Range
|
---|
| 76 | class Range {
|
---|
| 77 |
|
---|
| 78 | public:
|
---|
| 79 | // This declaration not yet supported by all compilers
|
---|
| 80 | // const int fromStart = INT_MIN;
|
---|
| 81 | // const int toEnd = INT_MIN;
|
---|
| 82 |
|
---|
| 83 | typedef int T_numtype;
|
---|
| 84 |
|
---|
| 85 | enum { fromStart = INT_MIN, toEnd = INT_MIN };
|
---|
| 86 |
|
---|
| 87 | Range()
|
---|
| 88 | {
|
---|
| 89 | first_ = fromStart;
|
---|
| 90 | last_ = toEnd;
|
---|
| 91 | stride_ = 1;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | // Range(Range r): allow default copy constructor to be used
|
---|
| 95 | #ifdef BZ_MANUAL_VECEXPR_COPY_CONSTRUCTOR
|
---|
| 96 | Range(const Range& r)
|
---|
| 97 | {
|
---|
| 98 | first_ = r.first_;
|
---|
| 99 | last_ = r.last_;
|
---|
| 100 | stride_ = r.stride_;
|
---|
| 101 | }
|
---|
| 102 | #endif
|
---|
| 103 |
|
---|
| 104 | _bz_explicit Range(int slicePosition)
|
---|
| 105 | {
|
---|
| 106 | first_ = slicePosition;
|
---|
| 107 | last_ = slicePosition;
|
---|
| 108 | stride_ = 1;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | Range(int first, int last, int stride=1)
|
---|
| 112 | : first_(first), last_(last), stride_(stride)
|
---|
| 113 | {
|
---|
| 114 | BZPRECHECK((first == fromStart) || (last == toEnd) ||
|
---|
| 115 | (first < last) && (stride > 0) ||
|
---|
| 116 | (first > last) && (stride < 0) ||
|
---|
| 117 | (first == last), (*this) << " is an invalid range.");
|
---|
| 118 | BZPRECHECK((last-first) % stride == 0,
|
---|
| 119 | (*this) << ": the stride must evenly divide the range");
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | int first(int lowRange = 0) const
|
---|
| 123 | {
|
---|
| 124 | if (first_ == fromStart)
|
---|
| 125 | return lowRange;
|
---|
| 126 | return first_;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | int last(int highRange = 0) const
|
---|
| 130 | {
|
---|
| 131 | if (last_ == toEnd)
|
---|
| 132 | return highRange;
|
---|
| 133 | return last_;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | unsigned length(int recommendedLength = 0) const
|
---|
| 137 | {
|
---|
| 138 | BZPRECONDITION(first_ != fromStart);
|
---|
| 139 | BZPRECONDITION(last_ != toEnd);
|
---|
| 140 | BZPRECONDITION((last_ - first_) % stride_ == 0);
|
---|
| 141 | return (last_ - first_) / stride_ + 1;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | int stride() const
|
---|
| 145 | { return stride_; }
|
---|
| 146 |
|
---|
| 147 | _bz_bool isAscendingContiguous() const
|
---|
| 148 | {
|
---|
| 149 | return (first_ < last_) && (stride_ == 1);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | void setRange(int first, int last, int stride=1)
|
---|
| 153 | {
|
---|
| 154 | BZPRECONDITION((first < last) && (stride > 0) ||
|
---|
| 155 | (first > last) && (stride < 0) ||
|
---|
| 156 | (first == last));
|
---|
| 157 | BZPRECONDITION((last-first) % stride == 0);
|
---|
| 158 | first_ = first;
|
---|
| 159 | last_ = last;
|
---|
| 160 | stride_ = stride;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | static Range all()
|
---|
| 164 | { return Range(fromStart,toEnd,1); }
|
---|
| 165 |
|
---|
| 166 | bool isUnitStride() const
|
---|
| 167 | { return stride_ == 1; }
|
---|
| 168 |
|
---|
| 169 | // Operators
|
---|
| 170 | Range operator-(int shift) const
|
---|
| 171 | {
|
---|
| 172 | BZPRECONDITION(first_ != fromStart);
|
---|
| 173 | BZPRECONDITION(last_ != toEnd);
|
---|
| 174 | return Range(first_ - shift, last_ - shift, stride_);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | Range operator+(int shift) const
|
---|
| 178 | {
|
---|
| 179 | BZPRECONDITION(first_ != fromStart);
|
---|
| 180 | BZPRECONDITION(last_ != toEnd);
|
---|
| 181 | return Range(first_ + shift, last_ + shift, stride_);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | int operator[](unsigned i) const
|
---|
| 185 | {
|
---|
| 186 | return first_ + i * stride_;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | int operator()(unsigned i) const
|
---|
| 190 | {
|
---|
| 191 | return first_ + i * stride_;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | friend inline ostream& operator<<(ostream& os, const Range& range)
|
---|
| 195 | {
|
---|
| 196 | os << "Range(" << range.first() << "," << range.last() << ","
|
---|
| 197 | << range.stride() << ")";
|
---|
| 198 |
|
---|
| 199 | return os;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /////////////////////////////////////////////
|
---|
| 203 | // Library-internal member functions
|
---|
| 204 | // These are undocumented and may change or
|
---|
| 205 | // disappear in future releases.
|
---|
| 206 | /////////////////////////////////////////////
|
---|
| 207 |
|
---|
| 208 | enum { _bz_staticLengthCount = 0,
|
---|
| 209 | _bz_dynamicLengthCount = 0,
|
---|
| 210 | _bz_staticLength = 0 };
|
---|
| 211 |
|
---|
| 212 | _bz_bool _bz_hasFastAccess() const
|
---|
| 213 | { return stride_ == 1; }
|
---|
| 214 |
|
---|
| 215 | T_numtype _bz_fastAccess(unsigned i) const
|
---|
| 216 | { return first_ + i; }
|
---|
| 217 |
|
---|
| 218 | unsigned _bz_suggestLength() const
|
---|
| 219 | {
|
---|
| 220 | return length();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | _bz_VecExpr<Range> _bz_asVecExpr() const
|
---|
| 224 | { return _bz_VecExpr<Range>(*this); }
|
---|
| 225 |
|
---|
| 226 | private:
|
---|
| 227 | int first_, last_, stride_;
|
---|
| 228 | };
|
---|
| 229 |
|
---|
| 230 | BZ_NAMESPACE_END
|
---|
| 231 |
|
---|
| 232 | #endif // BZ_RANGE_H
|
---|