integral_range.inl
Go to the documentation of this file.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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 namespace lass
00044 {
00045 namespace stde
00046 {
00047
00048
00049
00050 template <typename I>
00051 const_integral_iterator<I>::const_integral_iterator(value_type value, value_type step):
00052 value_(value),
00053 step_(step)
00054 {
00055 }
00056
00057
00058
00059 template <typename I> inline
00060 typename const_integral_iterator<I>::pointer
00061 const_integral_iterator<I>::operator->() const
00062 {
00063 return &value_;
00064 }
00065
00066
00067
00068 template <typename I> inline
00069 typename const_integral_iterator<I>::reference
00070 const_integral_iterator<I>::operator*() const
00071 {
00072 return value_;
00073 }
00074
00075
00076
00077 template <typename I> inline
00078 typename const_integral_iterator<I>::value_type
00079 const_integral_iterator<I>::operator[](difference_type n) const
00080 {
00081 return value_ + n * step_;
00082 }
00083
00084
00085
00086 template <typename I> inline
00087 typename const_integral_iterator<I>::self_type&
00088 const_integral_iterator<I>::operator++()
00089 {
00090 value_ += step_;
00091 return *this;
00092 }
00093
00094
00095
00096 template <typename I> inline
00097 typename const_integral_iterator<I>::self_type
00098 const_integral_iterator<I>::operator++(int)
00099 {
00100 self_type temp(*this);
00101 ++*this;
00102 return temp;
00103 }
00104
00105
00106
00107 template <typename I> inline
00108 typename const_integral_iterator<I>::self_type&
00109 const_integral_iterator<I>::operator--()
00110 {
00111 value_ -= step_;
00112 return *this;
00113 }
00114
00115
00116
00117 template <typename I> inline
00118 typename const_integral_iterator<I>::self_type
00119 const_integral_iterator<I>::operator--(int)
00120 {
00121 self_type temp(*this);
00122 --*this;
00123 return temp;
00124 }
00125
00126
00127
00128 template <typename I> inline
00129 typename const_integral_iterator<I>::self_type&
00130 const_integral_iterator<I>::operator+=(difference_type n)
00131 {
00132 value_ += n * step_;
00133 return *this;
00134 }
00135
00136
00137
00138 template <typename I> inline
00139 typename const_integral_iterator<I>::self_type&
00140 const_integral_iterator<I>::operator-=(difference_type n)
00141 {
00142 value_ -= n * step_;
00143 return *this;
00144 }
00145
00146
00147
00148 template <typename I> inline
00149 typename const_integral_iterator<I>::self_type
00150 const_integral_iterator<I>::operator+(difference_type n) const
00151 {
00152 self_type temp(*this);
00153 temp += n;
00154 return temp;
00155 }
00156
00157
00158
00159 template <typename I> inline
00160 typename const_integral_iterator<I>::self_type
00161 const_integral_iterator<I>::operator-(difference_type n) const
00162 {
00163 self_type temp(*this);
00164 temp -= n;
00165 return temp;
00166 }
00167
00168
00169
00170 template <typename I> inline
00171 typename const_integral_iterator<I>::difference_type
00172 const_integral_iterator<I>::operator-(const self_type& other) const
00173 {
00174 LASS_ASSERT(step_ == other.step_);
00175 LASS_ASSERT((value_ - other.value_) % step_ == 0);
00176 return (value_ - other.value_) / step_;
00177 }
00178
00179
00180
00181 template <typename I> inline
00182 bool operator==(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
00183 {
00184 LASS_ASSERT(a.step_ == b.step_);
00185 return a.value_ == b.value_;
00186 }
00187
00188
00189
00190 template <typename I> inline
00191 bool operator!=(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
00192 {
00193 return !(a == b);
00194 }
00195
00196
00197
00198 template <typename I> inline
00199 bool operator<(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
00200 {
00201 LASS_ASSERT(a.step_ == b.step_);
00202 return a.value_ < b.value_;
00203 }
00204
00205
00206
00207 template <typename I> inline
00208 bool operator>(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
00209 {
00210 return b < a;
00211 }
00212
00213
00214
00215 template <typename I> inline
00216 bool operator<=(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
00217 {
00218 return !(b < a);
00219 }
00220
00221
00222
00223 template <typename I> inline
00224 bool operator>=(const const_integral_iterator<I>& a, const const_integral_iterator<I>& b)
00225 {
00226 return !(a < b);
00227 }
00228
00229
00230
00231
00232
00233 template <typename I> inline
00234 integral_range_t<I>::integral_range_t():
00235 first_(0),
00236 last_(0),
00237 step_(0)
00238 {
00239 }
00240
00241
00242
00243 template <typename I> inline
00244 integral_range_t<I>::integral_range_t(value_type last):
00245 first_(0),
00246 last_(last),
00247 step_(last >= 0 ? 1 : -1)
00248 {
00249 }
00250
00251
00252
00253 template <typename I> inline
00254 integral_range_t<I>::integral_range_t(value_type first, value_type last):
00255 first_(first),
00256 last_(last),
00257 step_(last >= first ? 1 : -1)
00258 {
00259 }
00260
00261
00262
00263 template <typename I> inline
00264 integral_range_t<I>::integral_range_t(value_type first, value_type last, value_type step):
00265 first_(first),
00266 last_(last),
00267 step_(step)
00268 {
00269 LASS_ASSERT(step != 0);
00270 value_type m = (last_ - first_) % step_;
00271 if (m != 0)
00272 {
00273 last_ += step_ - m;
00274 }
00275 LASS_ASSERT((last_ - first_) % step_ == 0);
00276 }
00277
00278
00279
00280 template <typename I> inline
00281 typename integral_range_t<I>::const_iterator
00282 integral_range_t<I>::begin() const
00283 {
00284 return const_iterator(first_, step_);
00285 }
00286
00287
00288
00289 template <typename I> inline
00290 typename integral_range_t<I>::const_iterator
00291 integral_range_t<I>::end() const
00292 {
00293 return const_iterator(last_, step_);
00294 }
00295
00296
00297
00298 template <typename I> inline
00299 typename integral_range_t<I>::const_reverse_iterator
00300 integral_range_t<I>::rbegin() const
00301 {
00302 return const_reverse_iterator(end());
00303 }
00304
00305
00306
00307 template <typename I> inline
00308 typename integral_range_t<I>::const_reverse_iterator
00309 integral_range_t<I>::rend() const
00310 {
00311 return const_reverse_iterator(begin());
00312 }
00313
00314
00315
00316 template <typename I> inline
00317 typename integral_range_t<I>::self_type&
00318 integral_range_t<I>::operator++()
00319 {
00320 LASS_ASSERT(first_ != last_);
00321 first_ += step_;
00322 }
00323
00324
00325
00326 template <typename I> inline
00327 typename integral_range_t<I>::self_type&
00328 integral_range_t<I>::operator--()
00329 {
00330 LASS_ASSERT(first_ != last_);
00331 first_ -= step_;
00332 }
00333
00334
00335
00336 template <typename I> inline
00337 typename integral_range_t<I>::self_type&
00338 integral_range_t<I>::operator+=(difference_type n)
00339 {
00340 LASS_ASSERT((step_ > 0) ? (first_ + n * step_ <= last_) : (first_ + n * step_ >= last_));
00341 first_ += n * step_;
00342 }
00343
00344
00345
00346 template <typename I> inline
00347 typename integral_range_t<I>::self_type&
00348 integral_range_t<I>::operator-=(difference_type n)
00349 {
00350 first_ -= n * step_;
00351 }
00352
00353
00354
00355 template <typename I> inline
00356 typename integral_range_t<I>::self_type
00357 integral_range_t<I>::operator+(difference_type n) const
00358 {
00359 self_type temp(*this);
00360 temp += n;
00361 return temp;
00362 }
00363
00364
00365
00366 template <typename I> inline
00367 typename integral_range_t<I>::self_type
00368 integral_range_t<I>::operator-(difference_type n) const
00369 {
00370 self_type temp(*this);
00371 temp -= n;
00372 return temp;
00373 }
00374
00375
00376
00377 template <typename I> inline
00378 const typename integral_range_t<I>::size_type
00379 integral_range_t<I>::size() const
00380 {
00381 LASS_ASSERT((last_ - first_) % step_ == 0);
00382 return static_cast<size_type>((last_ - first_) / step_);
00383 }
00384
00385
00386
00387 template <typename I> inline
00388 const bool integral_range_t<I>::empty() const
00389 {
00390 return first_ == last_;
00391 }
00392
00393
00394
00395 template <typename I> inline
00396 const bool integral_range_t<I>::operator!() const
00397 {
00398 return empty();
00399 }
00400
00401
00402
00403 template <typename I> inline
00404 integral_range_t<I>::operator const num::SafeBool() const
00405 {
00406 return empty() ? num::safeFalse : num::safeTrue;
00407 }
00408
00409
00410
00411 template <typename I>
00412 void integral_range_t<I>::swap(self_type& other)
00413 {
00414 std::swap(first_, other.first_);
00415 std::swap(last_, other.last_);
00416 std::swap(step_, other.step_);
00417 }
00418
00419
00420
00421 template <typename I> inline
00422 integral_range_t<I> integral_range(const I& last)
00423 {
00424 return integral_range_t<I>(last);
00425 }
00426
00427
00428
00429 template <typename I> inline
00430 integral_range_t<I> integral_range(const I& first, const I& last)
00431 {
00432 return integral_range_t<I>(first, last);
00433 }
00434
00435
00436
00437 template <typename I> inline
00438 integral_range_t<I> integral_range(const I& first, const I& last, const I& step)
00439 {
00440 return integral_range_t<I>(first, last, step);
00441 }
00442
00443
00444
00445 template <typename I> inline
00446 bool operator==(const integral_range_t<I>& a, const integral_range_t<I>& b)
00447 {
00448 LASS_ASSERT(a.step_ == b.step_ && a.last_ == b.last_);
00449 return a.first_ == b.first_;
00450 }
00451
00452
00453
00454 template <typename I> inline
00455 bool operator!=(const integral_range_t<I>& a, const integral_range_t<I>& b)
00456 {
00457 return !(a == b);
00458 }
00459
00460
00461
00462 template <typename I> inline
00463 bool operator<(const integral_range_t<I>& a, const integral_range_t<I>& b)
00464 {
00465 LASS_ASSERT(a.step_ == b.step_ && a.last_ == b.last_);
00466 return a.first_ < b.first_;
00467 }
00468
00469
00470
00471 template <typename I> inline
00472 bool operator>(const integral_range_t<I>& a, const integral_range_t<I>& b)
00473 {
00474 return b < a;
00475 }
00476
00477
00478
00479 template <typename I> inline
00480 bool operator<=(const integral_range_t<I>& a, const integral_range_t<I>& b)
00481 {
00482 return !(b < a);
00483 }
00484
00485
00486
00487 template <typename I> inline
00488 bool operator>=(const integral_range_t<I>& a, const integral_range_t<I>& b)
00489 {
00490 return !(a < b);
00491 }
00492
00493 }
00494
00495 }
00496
00497