1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| template<class It>
class cyclic_iterator
{
public:
typedef cyclic_iterator self;
typedef It iter_type;
typedef typename iterator_traits<iter_type>::iterator_category iterator_category;
typedef typename iterator_traits<iter_type>::value_type value_type;
typedef typename iterator_traits<iter_type>::reference reference;
typedef const value_type &const_reference;
typedef typename iterator_traits<iter_type>::pointer pointer;
typedef const value_type *const_pointer;
typedef typename iterator_traits<iter_type>::difference_type difference_type;
protected:
iter_type it;
iter_type begin;
iter_type end;
public:
cyclic_iterator(iter_type b, iter_type e) : it(b), begin(b), end(e) {};
cyclic_iterator(iter_type b, iter_type e, iter_type &i) : it(i), begin(b), end(e) {};
iter_type &base() { return it; }
const iter_type &base() const { return it; }
reference operator* () { return *it; }
const_reference operator* () const { return *it; }
pointer operator->() { return &*it; }
const_pointer operator->() const { return &*it; }
self &operator++() { ++it; if (it==end) it=begin; return *this; }
self &operator--() { if (it==begin) it=end; --it; return *this; }
self operator++(int) { self t=*this; ++*this; return t; }
self operator--(int) { self t=*this; --*this; return t; }
//manque l'accès aléatoire
bool operator==(const self &r) const { return it==r.it; }
bool operator!=(const self &r) const { return it!=r.it; }
};
template<class T > inline cyclic_iterator<typename T:: iterator> cyclic_it( T &x ) { return cyclic_iterator<typename T:: iterator>(x.begin(),x.end()); }
template<class T > inline cyclic_iterator<typename T:: iterator> cyclic_it( T &x, typename T::iterator i) { return cyclic_iterator<typename T:: iterator>(x.begin(),x.end(),i); }
template<class It> inline cyclic_iterator<It > cyclic_it(It b, It e ) { return cyclic_iterator<It >(b,e); } |
Partager