iterators.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #ifndef ITERATORS_H
  2. #define ITERATORS_H
  3. #include <iterator>
  4. template <class It>
  5. class output_iterator
  6. {
  7. It it_;
  8. template <class U> friend class output_iterator;
  9. public:
  10. typedef std::output_iterator_tag iterator_category;
  11. typedef typename std::iterator_traits<It>::value_type value_type;
  12. typedef typename std::iterator_traits<It>::difference_type difference_type;
  13. typedef It pointer;
  14. typedef typename std::iterator_traits<It>::reference reference;
  15. It base() const {return it_;}
  16. explicit output_iterator(It it) : it_(it) {}
  17. template <class U>
  18. output_iterator(const output_iterator<U>& u) :it_(u.it_) {}
  19. reference operator*() const {return *it_;}
  20. output_iterator& operator++() {++it_; return *this;}
  21. output_iterator operator++(int)
  22. {output_iterator tmp(*this); ++(*this); return tmp;}
  23. };
  24. template <class Iter>
  25. inline
  26. Iter
  27. base(output_iterator<Iter> i)
  28. {
  29. return i.base();
  30. }
  31. template <class It>
  32. class input_iterator
  33. {
  34. It it_;
  35. template <class U> friend class input_iterator;
  36. public:
  37. typedef std::input_iterator_tag iterator_category;
  38. typedef typename std::iterator_traits<It>::value_type value_type;
  39. typedef typename std::iterator_traits<It>::difference_type difference_type;
  40. typedef It pointer;
  41. typedef typename std::iterator_traits<It>::reference reference;
  42. It base() const {return it_;}
  43. explicit input_iterator(It it) : it_(it) {}
  44. template <class U>
  45. input_iterator(const input_iterator<U>& u) :it_(u.it_) {}
  46. reference operator*() const {return *it_;}
  47. pointer operator->() const {return it_;}
  48. input_iterator& operator++() {++it_; return *this;}
  49. input_iterator operator++(int)
  50. {input_iterator tmp(*this); ++(*this); return tmp;}
  51. };
  52. template <class T, class U>
  53. inline
  54. bool
  55. operator==(const input_iterator<T>& x, const input_iterator<U>& y)
  56. {
  57. return x.base() == y.base();
  58. }
  59. template <class T, class U>
  60. inline
  61. bool
  62. operator!=(const input_iterator<T>& x, const input_iterator<U>& y)
  63. {
  64. return !(x == y);
  65. }
  66. template <class Iter>
  67. inline
  68. Iter
  69. base(input_iterator<Iter> i)
  70. {
  71. return i.base();
  72. }
  73. template <class It>
  74. class forward_iterator
  75. {
  76. It it_;
  77. template <class U> friend class forward_iterator;
  78. public:
  79. typedef std::forward_iterator_tag iterator_category;
  80. typedef typename std::iterator_traits<It>::value_type value_type;
  81. typedef typename std::iterator_traits<It>::difference_type difference_type;
  82. typedef It pointer;
  83. typedef typename std::iterator_traits<It>::reference reference;
  84. It base() const {return it_;}
  85. forward_iterator() : it_() {}
  86. explicit forward_iterator(It it) : it_(it) {}
  87. template <class U>
  88. forward_iterator(const forward_iterator<U>& u) :it_(u.it_) {}
  89. reference operator*() const {return *it_;}
  90. pointer operator->() const {return it_;}
  91. forward_iterator& operator++() {++it_; return *this;}
  92. forward_iterator operator++(int)
  93. {forward_iterator tmp(*this); ++(*this); return tmp;}
  94. };
  95. template <class T, class U>
  96. inline
  97. bool
  98. operator==(const forward_iterator<T>& x, const forward_iterator<U>& y)
  99. {
  100. return x.base() == y.base();
  101. }
  102. template <class T, class U>
  103. inline
  104. bool
  105. operator!=(const forward_iterator<T>& x, const forward_iterator<U>& y)
  106. {
  107. return !(x == y);
  108. }
  109. template <class Iter>
  110. inline
  111. Iter
  112. base(forward_iterator<Iter> i)
  113. {
  114. return i.base();
  115. }
  116. template <class It>
  117. class bidirectional_iterator
  118. {
  119. It it_;
  120. template <class U> friend class bidirectional_iterator;
  121. public:
  122. typedef std::bidirectional_iterator_tag iterator_category;
  123. typedef typename std::iterator_traits<It>::value_type value_type;
  124. typedef typename std::iterator_traits<It>::difference_type difference_type;
  125. typedef It pointer;
  126. typedef typename std::iterator_traits<It>::reference reference;
  127. It base() const {return it_;}
  128. bidirectional_iterator() : it_() {}
  129. explicit bidirectional_iterator(It it) : it_(it) {}
  130. template <class U>
  131. bidirectional_iterator(const bidirectional_iterator<U>& u) :it_(u.it_) {}
  132. reference operator*() const {return *it_;}
  133. pointer operator->() const {return it_;}
  134. bidirectional_iterator& operator++() {++it_; return *this;}
  135. bidirectional_iterator operator++(int)
  136. {bidirectional_iterator tmp(*this); ++(*this); return tmp;}
  137. bidirectional_iterator& operator--() {--it_; return *this;}
  138. bidirectional_iterator operator--(int)
  139. {bidirectional_iterator tmp(*this); --(*this); return tmp;}
  140. };
  141. template <class T, class U>
  142. inline
  143. bool
  144. operator==(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
  145. {
  146. return x.base() == y.base();
  147. }
  148. template <class T, class U>
  149. inline
  150. bool
  151. operator!=(const bidirectional_iterator<T>& x, const bidirectional_iterator<U>& y)
  152. {
  153. return !(x == y);
  154. }
  155. template <class Iter>
  156. inline
  157. Iter
  158. base(bidirectional_iterator<Iter> i)
  159. {
  160. return i.base();
  161. }
  162. template <class It>
  163. class random_access_iterator
  164. {
  165. It it_;
  166. template <class U> friend class random_access_iterator;
  167. public:
  168. typedef std::random_access_iterator_tag iterator_category;
  169. typedef typename std::iterator_traits<It>::value_type value_type;
  170. typedef typename std::iterator_traits<It>::difference_type difference_type;
  171. typedef It pointer;
  172. typedef typename std::iterator_traits<It>::reference reference;
  173. It base() const {return it_;}
  174. random_access_iterator() : it_() {}
  175. explicit random_access_iterator(It it) : it_(it) {}
  176. template <class U>
  177. random_access_iterator(const random_access_iterator<U>& u) :it_(u.it_) {}
  178. reference operator*() const {return *it_;}
  179. pointer operator->() const {return it_;}
  180. random_access_iterator& operator++() {++it_; return *this;}
  181. random_access_iterator operator++(int)
  182. {random_access_iterator tmp(*this); ++(*this); return tmp;}
  183. random_access_iterator& operator--() {--it_; return *this;}
  184. random_access_iterator operator--(int)
  185. {random_access_iterator tmp(*this); --(*this); return tmp;}
  186. random_access_iterator& operator+=(difference_type n) {it_ += n; return *this;}
  187. random_access_iterator operator+(difference_type n) const
  188. {random_access_iterator tmp(*this); tmp += n; return tmp;}
  189. friend random_access_iterator operator+(difference_type n, random_access_iterator x)
  190. {x += n; return x;}
  191. random_access_iterator& operator-=(difference_type n) {return *this += -n;}
  192. random_access_iterator operator-(difference_type n) const
  193. {random_access_iterator tmp(*this); tmp -= n; return tmp;}
  194. reference operator[](difference_type n) const {return it_[n];}
  195. };
  196. template <class T, class U>
  197. inline
  198. bool
  199. operator==(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  200. {
  201. return x.base() == y.base();
  202. }
  203. template <class T, class U>
  204. inline
  205. bool
  206. operator!=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  207. {
  208. return !(x == y);
  209. }
  210. template <class T, class U>
  211. inline
  212. bool
  213. operator<(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  214. {
  215. return x.base() < y.base();
  216. }
  217. template <class T, class U>
  218. inline
  219. bool
  220. operator<=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  221. {
  222. return !(y < x);
  223. }
  224. template <class T, class U>
  225. inline
  226. bool
  227. operator>(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  228. {
  229. return y < x;
  230. }
  231. template <class T, class U>
  232. inline
  233. bool
  234. operator>=(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  235. {
  236. return !(x < y);
  237. }
  238. template <class T, class U>
  239. inline
  240. typename std::iterator_traits<T>::difference_type
  241. operator-(const random_access_iterator<T>& x, const random_access_iterator<U>& y)
  242. {
  243. return x.base() - y.base();
  244. }
  245. template <class Iter>
  246. inline
  247. Iter
  248. base(random_access_iterator<Iter> i)
  249. {
  250. return i.base();
  251. }
  252. template <class Iter>
  253. inline
  254. Iter
  255. base(Iter i)
  256. {
  257. return i;
  258. }
  259. #endif // ITERATORS_H