Advertisement
Iwanicki

vector_iterator v3

May 14th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1. #ifndef VECTORITERATOR_H
  2. #define VECTORITERATOR_H
  3.  
  4. #include <iterator>
  5.  
  6. namespace mytemplate
  7. {
  8.     template <class, class>
  9.     class vector_iterator;
  10.  
  11.     template < class Type, class vector_class >
  12.     class vector_const_iterator : public std::iterator<std::random_access_iterator_tag, Type>
  13.     {
  14.     private:
  15.         Type* ptr;
  16.         vector_const_iterator( Type* rhs ) : ptr( rhs ) {}
  17.        
  18.     public:
  19.         friend vector_class;
  20.         friend vector_iterator<Type, vector_class>;
  21.  
  22.         vector_const_iterator() : ptr( nullptr ) {}
  23.         vector_const_iterator( const vector_const_iterator& rhs ) : ptr( rhs.ptr ) {}
  24.        
  25.         // Operatory : różne
  26.         inline vector_const_iterator& operator=(const vector_const_iterator& rhs) { ptr = rhs.ptr; return *this; }
  27.         inline vector_const_iterator& operator+=(ptrdiff_t rhs) { ptr += rhs; return *this; }
  28.         inline vector_const_iterator& operator-=(ptrdiff_t rhs) { ptr -= rhs; return *this; }
  29.         inline const Type& operator*() const { return *ptr; }
  30.         inline const Type* operator->() const { return &(*ptr); }
  31.         inline const Type& operator[](ptrdiff_t rhs) const { return ptr[rhs]; }
  32.        
  33.         // Operatory : arytmetyczne
  34.         inline vector_const_iterator& operator++() { ++ptr; return *this; }
  35.         inline vector_const_iterator& operator--() { --ptr; return *this; }
  36.         inline vector_const_iterator operator++(int) { vector_const_iterator tmp(*this); ++ptr; return tmp; }
  37.         inline vector_const_iterator operator--(int) { vector_const_iterator tmp(*this); --ptr; return tmp; }
  38.         inline vector_const_iterator operator+(ptrdiff_t rhs) const { return ptr + rhs; }
  39.         inline vector_const_iterator operator-(ptrdiff_t rhs) const { return ptr - rhs; }
  40.         inline int operator-(const vector_const_iterator& rhs) const { return ptr - rhs.ptr; }
  41.         friend inline vector_const_iterator operator+(ptrdiff_t lhs, vector_const_iterator& rhs) { return rhs + lhs; }
  42.  
  43.         // Operatory : porównania
  44.         inline bool operator==(const vector_const_iterator& rhs) const { return ptr == rhs.ptr; }
  45.         inline bool operator!=(const vector_const_iterator& rhs) const { return !(*this == rhs); }
  46.         inline bool operator<(const vector_const_iterator& rhs) const { return ptr < rhs.ptr; }
  47.         inline bool operator>(const vector_const_iterator& rhs) const { return ptr > rhs.ptr; }
  48.         inline bool operator<=(const vector_const_iterator& rhs) const { return ptr <= rhs.ptr; }
  49.         inline bool operator>=(const vector_const_iterator& rhs) const { return ptr >= rhs.ptr; }
  50.     };
  51.  
  52.     template < class Type, class vector_class >
  53.     class vector_iterator : public std::iterator<std::random_access_iterator_tag, Type>
  54.     {
  55.     private:
  56.         Type* ptr;
  57.         vector_iterator( Type* rhs ) : ptr( rhs ) {}
  58.        
  59.     public:
  60.         friend vector_class;
  61.         vector_iterator() : ptr( nullptr ) {}
  62.         vector_iterator( const vector_iterator& rhs ) : ptr( rhs.ptr ) {}
  63.        
  64.         // Operatory : różne
  65.         inline vector_iterator& operator=(const vector_iterator& rhs) { ptr = rhs.ptr; return *this; }
  66.         inline vector_iterator& operator+=(ptrdiff_t rhs) { ptr += rhs; return *this; }
  67.         inline vector_iterator& operator-=(ptrdiff_t rhs) { ptr -= rhs; return *this; }
  68.         inline Type& operator*() const { return *ptr; }
  69.         inline Type* operator->() const { return &(*ptr); }
  70.         inline Type& operator[](ptrdiff_t rhs) const { return ptr[rhs]; }
  71.  
  72.         typedef vector_const_iterator<Type, vector_class> vec_const_iterator;
  73.         inline operator vec_const_iterator() const { return vec_const_iterator( ptr ); }
  74.        
  75.         // Operatory : arytmetyczne
  76.         inline vector_iterator& operator++() { ++ptr; return *this; }
  77.         inline vector_iterator& operator--() { --ptr; return *this; }
  78.         inline vector_iterator operator++(int) { vector_iterator tmp(*this); ++ptr; return tmp; }
  79.         inline vector_iterator operator--(int) { vector_iterator tmp(*this); --ptr; return tmp; }
  80.         inline vector_iterator operator+(ptrdiff_t rhs) const { return ptr + rhs; }
  81.         inline vector_iterator operator-(ptrdiff_t rhs) const { return ptr - rhs; }
  82.         inline int operator-(const vec_const_iterator& rhs) const { return ptr - rhs.ptr; }
  83.         friend inline vector_iterator operator+(ptrdiff_t lhs, vector_iterator& rhs) { return rhs + lhs; }
  84.  
  85.         // Operatory : porównania
  86.         inline bool operator==(const vec_const_iterator& rhs) const { return ptr == rhs.ptr; }
  87.         inline bool operator!=(const vec_const_iterator& rhs) const { return !(*this == rhs); }
  88.         inline bool operator<(const vec_const_iterator& rhs) const { return ptr < rhs.ptr; }
  89.         inline bool operator>(const vec_const_iterator& rhs) const { return ptr > rhs.ptr; }
  90.         inline bool operator<=(const vec_const_iterator& rhs) const { return ptr <= rhs.ptr; }
  91.         inline bool operator>=(const vec_const_iterator& rhs) const { return ptr >= rhs.ptr; }
  92.     };
  93. }
  94.  
  95. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement