Created
December 15, 2013 04:26
-
-
Save brennie/7968865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class T> class Range | |
{ | |
public: | |
Range(T initial, T final, T step = T(1)) : _initial(initial), _final(final), _step(step) {} | |
class Iterator; | |
Iterator begin() const | |
{ | |
return Iterator(_initial, _step); | |
} | |
Iterator end() const | |
{ | |
return Iterator(_final, _step); | |
} | |
private: | |
const T _initial; | |
const T _final; | |
const T _step; | |
}; | |
template<class T> | |
class Range<T>::Iterator | |
{ | |
friend class Range<T>; | |
public: | |
T operator*() const { return _current; } | |
Iterator operator++() | |
{ | |
_current += _step; | |
return *this; | |
} | |
bool operator!=(Range<T>::Iterator& other) | |
{ | |
if (_current < _current + _step) | |
return _current < other._current; | |
else | |
return _current > other._current; | |
} | |
private: | |
Iterator(T start, T step) : _current(start), _step(step) {} | |
T _current; | |
const T _step; | |
}; | |
template<class T> | |
Range<T> range(T start, T stop, T step = T(1)) | |
{ | |
return Range<T>(start, stop, step); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment