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