#ifndef Ring_H #define Ring_H #include "cycle_iterator.hpp" #include #include // DEBUG #include // DEBUG #include // DEBUG template class CONT=std::vector, typename offset_t=int> class Ring { public: typedef typename boost::cycle_iterator::iterator, offset_t> iterator; typedef typename boost::cycle_iterator::const_iterator, offset_t> const_iterator; Ring() {} explicit Ring (size_t n) : v (n), it (boost::make_cycle_iterator (v.begin(), v.end())) {} Ring (const Ring& r) : v (r.v), it (boost::make_cycle_iterator (v.begin(), v.end(), r.it.realposition())) {} iterator begin() { return it; } iterator end() { return it + size(); } const iterator begin() const { return it; } const iterator end() const { return it + size(); } void shift (int n=1) { it += n; } void Shift (int n=1) { shift (n); } T& operator[] (int n) { return it[n]; } const T& operator[] (int n) const { return it[n]; } void resize (size_t n) { v.resize (n); it = boost::make_cycle_iterator (v.begin(), v.end(), it.realposition()); } void insert (iterator pos, const T& value) { size_t offset = pos.offset(); // offset from base of v v.insert (v.begin()+offset, value); it = boost::make_cycle_iterator (v.begin(), v.end(), it.realposition()); } template void insert (iterator pos, in_t in, in_t inend) { size_t offset = pos.offset(); // offset from base of v v.insert (v.begin()+offset, in, inend); it = boost::make_cycle_iterator (v.begin(), v.end(), it.realposition()); } void erase (iterator pos) { size_t offset = pos.offset(); v.erase (v.begin()+offset); it = boost::make_cycle_iterator (v.begin(), v.end(), it.realposition()); } size_t size() const { return v.size(); } CONT v; iterator it; void Debug() const { std::cout << "v: ["; std::copy (v.begin(), v.end(), std::ostream_iterator (std::cout, " ")); std::cout << "]size: " << v.size() << " it.size: " << it.size << " it.pos: " << it.position << " it.wrap: " << it.wrap << '\n'; } }; #endif