ring.h
Go to the documentation of this file.
00001 #ifndef RING_H
00002 #define RING_H
00003 // Making a "ring" data structure from the STL
00004 #include <list>
00005 using namespace std;
00006 
00007 template<class T>
00008 class ring {
00009   list<T> lst;
00010 public:
00011 
00012   ring &operator=(const ring *r) {
00013      lst = r->lst;
00014   }
00015   // Declaration necessary so the following 
00016   // 'friend' statement sees this 'iterator' 
00017   // instead of std::iterator:
00018   class iterator;
00019   friend class iterator;
00020   class iterator : 
00021      public std::iterator<std::bidirectional_iterator_tag, T, ptrdiff_t> {
00022 
00023    public:
00024     typename list<T>::iterator it;
00025     list<T>* r;
00026 
00027     
00028     // "typename" necessary to resolve nesting:
00029 
00030     iterator(): r(NULL) {}
00031     iterator(list<T>& lst, const typename list<T>::iterator& i)
00032         : r(&lst), it(i) {}
00033 
00034     iterator &operator=(const iterator& x) {
00035        it = x.it;
00036        r = x.r;
00037        return *this;
00038     }
00039 
00040     bool operator==(const iterator& x) const {
00041       return it == x.it;
00042     }
00043 
00044     bool operator!=(const iterator& x) const {
00045       return !(*this == x);
00046     }
00047 
00048     typename list<T>::reference operator*() const {
00049       return *it;
00050     }
00051 
00052     iterator& operator++() {
00053       ++it;
00054       if(it == r->end())
00055         it = r->begin();
00056       return *this;
00057     }
00058 
00059     iterator operator++(int) {
00060       iterator tmp = *this;
00061       ++*this;
00062       return tmp;
00063     }
00064 
00065     iterator& operator--() {
00066       if(it == r->begin())
00067         it = r->end();
00068       --it;
00069       return *this;
00070     }
00071 
00072     iterator operator--(int) {
00073       iterator tmp = *this;
00074       --*this; 
00075       return tmp;
00076     }
00077 
00078     iterator operator+(int i) {
00079       iterator tmp = *this;
00080       while(i--) ++tmp;
00081       return tmp;
00082     }
00083 
00084     iterator operator-(int i) {
00085       iterator tmp = *this;
00086       while(i--) --tmp;
00087       return tmp;
00088     }
00089 
00090     iterator insert(const T& x){
00091       return iterator(*r, r->insert(it, x));
00092     }
00093     
00094     iterator erase() {
00095       iterator tmp = iterator(*r, r->erase(it));
00096       
00097       if (tmp.it == r->end()) tmp.it = r->begin();
00098 
00099       return tmp;
00100     }
00101   };
00102   
00103   iterator insert(iterator &i, const T& x) {
00104     typename list<T>::iterator it;
00105     it = lst.insert(i.it, x);
00106     return iterator(lst, it);
00107   }
00108 
00109   iterator push_back(const T& x) {
00110     typename list<T>::iterator it;
00111     it = lst.insert(lst.end(), x);
00112     return iterator(lst, it);
00113     
00114   }
00115  /* void push_back(const T& x) {
00116     lst.push_back(x);
00117   }*/
00118   
00119   iterator begin() {
00120     return iterator(lst, lst.begin());
00121   }
00122   
00123   int size() { return lst.size(); }
00124   void clear() { lst.clear(); }
00125   void reverse() { lst.reverse(); }
00126   void erase(iterator &i) { lst.erase(i.it); }
00127 };
00128 
00129 #endif


shape_reconstruction
Author(s): Roberto Martín-Martín
autogenerated on Sat Jun 8 2019 18:35:09