Skip to content

Instantly share code, notes, and snippets.

@brennie
Created December 15, 2013 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennie/7968865 to your computer and use it in GitHub Desktop.
Save brennie/7968865 to your computer and use it in GitHub Desktop.
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