_iterator.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996-1998
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_ITERATOR_H
  29. #define _STLP_INTERNAL_ITERATOR_H
  30. #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
  31. # include <stl/_iterator_base.h>
  32. #endif
  33. _STLP_BEGIN_NAMESPACE
  34. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION)
  35. // This is the new version of reverse_iterator, as defined in the
  36. // draft C++ standard. It relies on the iterator_traits template,
  37. // which in turn relies on partial specialization. The class
  38. // reverse_bidirectional_iterator is no longer part of the draft
  39. // standard, but it is retained for backward compatibility.
  40. template <class _Iterator>
  41. class reverse_iterator :
  42. public iterator<typename iterator_traits<_Iterator>::iterator_category,
  43. typename iterator_traits<_Iterator>::value_type,
  44. typename iterator_traits<_Iterator>::difference_type,
  45. typename iterator_traits<_Iterator>::pointer,
  46. typename iterator_traits<_Iterator>::reference> {
  47. protected:
  48. _Iterator current;
  49. typedef reverse_iterator<_Iterator> _Self;
  50. public:
  51. typedef typename iterator_traits<_Iterator>::difference_type difference_type;
  52. // pointer type required for arrow operator hidden behind _STLP_DEFINE_ARROW_OPERATOR:
  53. typedef typename iterator_traits<_Iterator>::pointer pointer;
  54. typedef typename iterator_traits<_Iterator>::reference reference;
  55. typedef _Iterator iterator_type;
  56. public:
  57. reverse_iterator() {}
  58. explicit reverse_iterator(iterator_type __x) : current(__x) {}
  59. reverse_iterator(const _Self& __x) : current(__x.current) {}
  60. _Self& operator = (const _Self& __x) { current = __x.base(); return *this; }
  61. # if defined (_STLP_MEMBER_TEMPLATES)
  62. template <class _Iter>
  63. reverse_iterator(const reverse_iterator<_Iter>& __x) : current(__x.base()) {}
  64. template <class _Iter>
  65. _Self& operator = (const reverse_iterator<_Iter>& __x) { current = __x.base(); return *this; }
  66. # endif /* _STLP_MEMBER_TEMPLATES */
  67. iterator_type base() const { return current; }
  68. reference operator*() const {
  69. _Iterator __tmp = current;
  70. return *--__tmp;
  71. }
  72. _STLP_DEFINE_ARROW_OPERATOR
  73. _Self& operator++() {
  74. --current;
  75. return *this;
  76. }
  77. _Self operator++(int) {
  78. _Self __tmp = *this;
  79. --current;
  80. return __tmp;
  81. }
  82. _Self& operator--() {
  83. ++current;
  84. return *this;
  85. }
  86. _Self operator--(int) {
  87. _Self __tmp = *this;
  88. ++current;
  89. return __tmp;
  90. }
  91. _Self operator+(difference_type __n) const { return _Self(current - __n); }
  92. _Self& operator+=(difference_type __n) {
  93. current -= __n;
  94. return *this;
  95. }
  96. _Self operator-(difference_type __n) const { return _Self(current + __n); }
  97. _Self& operator-=(difference_type __n) {
  98. current += __n;
  99. return *this;
  100. }
  101. reference operator[](difference_type __n) const { return *(*this + __n); }
  102. };
  103. template <class _Iterator>
  104. inline bool _STLP_CALL operator==(const reverse_iterator<_Iterator>& __x,
  105. const reverse_iterator<_Iterator>& __y)
  106. { return __x.base() == __y.base(); }
  107. template <class _Iterator>
  108. inline bool _STLP_CALL operator<(const reverse_iterator<_Iterator>& __x,
  109. const reverse_iterator<_Iterator>& __y)
  110. { return __y.base() < __x.base(); }
  111. # if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
  112. template <class _Iterator>
  113. inline bool _STLP_CALL operator!=(const reverse_iterator<_Iterator>& __x,
  114. const reverse_iterator<_Iterator>& __y)
  115. { return !(__x == __y); }
  116. template <class _Iterator>
  117. inline bool _STLP_CALL operator>(const reverse_iterator<_Iterator>& __x,
  118. const reverse_iterator<_Iterator>& __y)
  119. { return __y < __x; }
  120. template <class _Iterator>
  121. inline bool _STLP_CALL operator<=(const reverse_iterator<_Iterator>& __x,
  122. const reverse_iterator<_Iterator>& __y)
  123. { return !(__y < __x); }
  124. template <class _Iterator>
  125. inline bool _STLP_CALL operator>=(const reverse_iterator<_Iterator>& __x,
  126. const reverse_iterator<_Iterator>& __y)
  127. { return !(__x < __y); }
  128. # endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
  129. template <class _Iterator>
  130. # if defined (__SUNPRO_CC)
  131. inline ptrdiff_t _STLP_CALL
  132. # else
  133. inline typename reverse_iterator<_Iterator>::difference_type _STLP_CALL
  134. # endif
  135. operator-(const reverse_iterator<_Iterator>& __x,
  136. const reverse_iterator<_Iterator>& __y)
  137. { return __y.base() - __x.base(); }
  138. template <class _Iterator, class _DifferenceType>
  139. inline reverse_iterator<_Iterator> _STLP_CALL
  140. operator+(_DifferenceType n,const reverse_iterator<_Iterator>& x)
  141. { return x.operator+(n); }
  142. #endif
  143. template <class _Container>
  144. class back_insert_iterator
  145. : public iterator<output_iterator_tag, void, void, void, void> {
  146. typedef back_insert_iterator<_Container> _Self;
  147. protected:
  148. //c is a Standard name (24.4.2.1), do no make it STLport naming convention compliant.
  149. _Container *container;
  150. public:
  151. typedef _Container container_type;
  152. typedef output_iterator_tag iterator_category;
  153. explicit back_insert_iterator(_Container& __x) : container(&__x) {}
  154. _Self& operator=(const _Self& __other) {
  155. container = __other.container;
  156. return *this;
  157. }
  158. _Self& operator=(const typename _Container::value_type& __val) {
  159. container->push_back(__val);
  160. return *this;
  161. }
  162. _Self& operator*() { return *this; }
  163. _Self& operator++() { return *this; }
  164. _Self operator++(int) { return *this; }
  165. };
  166. template <class _Container>
  167. inline back_insert_iterator<_Container> _STLP_CALL back_inserter(_Container& __x)
  168. { return back_insert_iterator<_Container>(__x); }
  169. template <class _Container>
  170. class front_insert_iterator
  171. : public iterator<output_iterator_tag, void, void, void, void> {
  172. typedef front_insert_iterator<_Container> _Self;
  173. protected:
  174. //c is a Standard name (24.4.2.3), do no make it STLport naming convention compliant.
  175. _Container *container;
  176. public:
  177. typedef _Container container_type;
  178. typedef output_iterator_tag iterator_category;
  179. explicit front_insert_iterator(_Container& __x) : container(&__x) {}
  180. _Self& operator=(const _Self& __other) {
  181. container = __other.container;
  182. return *this;
  183. }
  184. _Self& operator=(const typename _Container::value_type& __val) {
  185. container->push_front(__val);
  186. return *this;
  187. }
  188. _Self& operator*() { return *this; }
  189. _Self& operator++() { return *this; }
  190. _Self operator++(int) { return *this; }
  191. };
  192. template <class _Container>
  193. inline front_insert_iterator<_Container> _STLP_CALL front_inserter(_Container& __x)
  194. { return front_insert_iterator<_Container>(__x); }
  195. template <class _Container>
  196. class insert_iterator
  197. : public iterator<output_iterator_tag, void, void, void, void> {
  198. typedef insert_iterator<_Container> _Self;
  199. protected:
  200. //container is a Standard name (24.4.2.5), do no make it STLport naming convention compliant.
  201. _Container *container;
  202. typename _Container::iterator _M_iter;
  203. public:
  204. typedef _Container container_type;
  205. typedef output_iterator_tag iterator_category;
  206. insert_iterator(_Container& __x, typename _Container::iterator __i)
  207. : container(&__x), _M_iter(__i) {}
  208. _Self& operator=(_Self const& __other) {
  209. container = __other.container;
  210. _M_iter = __other._M_iter;
  211. return *this;
  212. }
  213. _Self& operator=(const typename _Container::value_type& __val) {
  214. _M_iter = container->insert(_M_iter, __val);
  215. ++_M_iter;
  216. return *this;
  217. }
  218. _Self& operator*() { return *this; }
  219. _Self& operator++() { return *this; }
  220. _Self& operator++(int) { return *this; }
  221. };
  222. template <class _Container, class _Iterator>
  223. inline insert_iterator<_Container> _STLP_CALL
  224. inserter(_Container& __x, _Iterator __i) {
  225. typedef typename _Container::iterator __iter;
  226. return insert_iterator<_Container>(__x, __iter(__i));
  227. }
  228. _STLP_END_NAMESPACE
  229. #if ! defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) || defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
  230. # include <stl/_iterator_old.h>
  231. #endif
  232. #endif /* _STLP_INTERNAL_ITERATOR_H */
  233. // Local Variables:
  234. // mode:C++
  235. // End: