diff options
Diffstat (limited to 'numericrange.h')
-rw-r--r-- | numericrange.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/numericrange.h b/numericrange.h new file mode 100644 index 0000000..d86e578 --- /dev/null +++ b/numericrange.h @@ -0,0 +1,79 @@ +#ifndef NUMERICRANGE_H +#define NUMERICRANGE_H + +#include <iterator> +#include <stdexcept> + +/* + * A range in [first, last) in steps of 1. + */ +template<typename T> +class NumericRange +{ +public: + class iterator + { + friend class NumericRange; + public: + typedef iterator self_type; + typedef T value_type; + typedef T& reference; + typedef T* pointer; + typedef std::bidirectional_iterator_tag iterator_category; + typedef int difference_type; + + iterator(const T &first): m_value(first) {} + + T operator *() const { return m_value; } + const iterator &operator ++() { + ++m_value; + return *this; + } + iterator operator++(int) { + iterator copy(*this); + ++m_value; + return copy; + } + const iterator &operator --() { + --m_value; + return *this; + } + iterator operator--(int) { + iterator copy(*this); + --m_value; + return copy; + } + + bool operator ==(const iterator &other) const { + return m_value == other.m_value; + } + bool operator !=(const iterator &other) const { + return m_value != other.m_value; + } + + private: + T m_value; + }; + + NumericRange(const T &first, const T &last) + : m_begin(first) + , m_end(last) + { + if (first > last) { + throw std::logic_error("first > last"); + } + } + + typedef const iterator const_iterator; + + iterator begin() const { return m_begin; } + iterator end() const { return m_end; } + + const_iterator cbegin() const { return m_begin; } + const_iterator cend() const { return m_end; } + +private: + iterator m_begin, m_end; +}; + +#endif // NUMERICRANGE_H |