circ_iterator.h
00001 #ifndef VVELIB_UTIL_CIRC_ITERATOR_H
00002 #define VVELIB_UTIL_CIRC_ITERATOR_H
00003
00004 #include <config.h>
00005 #include <iterator>
00006
00007 namespace util
00008 {
00014 template <typename ForwardIterator>
00015 class CircIterator
00016 {
00017 public:
00018 typedef ForwardIterator base_type;
00019 typedef std::forward_iterator_tag iterator_category;
00020 typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
00021 typedef typename std::iterator_traits<ForwardIterator>::difference_type difference_type;
00022 typedef typename std::iterator_traits<ForwardIterator>::pointer pointer;
00023 typedef typename std::iterator_traits<ForwardIterator>::reference reference;
00024
00025 CircIterator() {}
00026
00030 CircIterator(const ForwardIterator& f, const ForwardIterator& l, const ForwardIterator& c)
00031 : first(f)
00032 , last(l)
00033 , init(c)
00034 , cur(c)
00035 {
00036 }
00037
00041 CircIterator(const ForwardIterator& f, const ForwardIterator& l)
00042 : first(f)
00043 , last(l)
00044 , init(l)
00045 , cur(l)
00046 {
00047 }
00048
00052 CircIterator(const CircIterator& copy, const ForwardIterator& new_cur)
00053 : first(copy.first)
00054 , last(copy.last)
00055 , init(copy.init)
00056 , cur(new_cur)
00057 {}
00058
00059 CircIterator(const CircIterator& copy)
00060 : first(copy.first)
00061 , last(copy.last)
00062 , init(copy.init)
00063 , cur(copy.cur)
00064 {}
00065
00066 #ifdef USE_CXX0X
00067 CircIterator(CircIterator&& copy) = default;
00068 #endif
00069
00070 CircIterator& operator++()
00071 {
00072 ++cur;
00073 if(cur == last)
00074 cur = first;
00075 if(cur == init)
00076 cur = last;
00077 return *this;
00078 }
00079
00080 CircIterator operator++(int)
00081 {
00082 CircIterator temp(*this);
00083 this->operator++();
00084 return temp;
00085 }
00086
00087 reference operator*() { return *cur; }
00088 pointer operator->() { return cur.operator->(); }
00089
00090 bool operator==(const ForwardIterator& other) const
00091 {
00092 return cur == other;
00093 }
00094
00095 bool operator==(const CircIterator& other) const
00096 {
00097 return cur == other.cur;
00098 }
00099
00100 bool operator!=(const ForwardIterator& other) const
00101 {
00102 return cur != other;
00103 }
00104
00105 bool operator!=(const CircIterator& other) const
00106 {
00107 return cur != other.cur;
00108 }
00109
00114 bool isInitIterator(const base_type& cmp) const
00115 {
00116 return cmp == init;
00117 }
00118
00122 CircIterator end() const
00123 {
00124 return CircIterator(first, last);
00125 }
00126
00127 base_type base() const { return cur; }
00128
00129 protected:
00130 ForwardIterator first, last, init, cur;
00131 };
00132 }
00133
00134 #endif // VVELIB_UTIL_CIRC_ITERATOR_H
00135