iterator.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-2012.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. //! \file
  12. #ifndef BOOST_MOVE_ITERATOR_HPP
  13. #define BOOST_MOVE_ITERATOR_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. #include <boost/move/detail/config_begin.hpp>
  22. #include <boost/move/detail/workaround.hpp> //forceinline
  23. #include <boost/move/detail/iterator_traits.hpp>
  24. #include <boost/move/utility_core.hpp>
  25. namespace boost {
  26. //////////////////////////////////////////////////////////////////////////////
  27. //
  28. // move_iterator
  29. //
  30. //////////////////////////////////////////////////////////////////////////////
  31. //! Class template move_iterator is an iterator adaptor with the same behavior
  32. //! as the underlying iterator except that its dereference operator implicitly
  33. //! converts the value returned by the underlying iterator's dereference operator
  34. //! to an rvalue reference. Some generic algorithms can be called with move
  35. //! iterators to replace copying with moving.
  36. template <class It>
  37. class move_iterator
  38. {
  39. public:
  40. typedef It iterator_type;
  41. typedef typename boost::movelib::iterator_traits<iterator_type>::value_type value_type;
  42. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
  43. typedef value_type && reference;
  44. #else
  45. typedef typename ::boost::move_detail::if_
  46. < ::boost::has_move_emulation_enabled<value_type>
  47. , ::boost::rv<value_type>&
  48. , value_type & >::type reference;
  49. #endif
  50. typedef It pointer;
  51. typedef typename boost::movelib::iterator_traits<iterator_type>::difference_type difference_type;
  52. typedef typename boost::movelib::iterator_traits<iterator_type>::iterator_category iterator_category;
  53. BOOST_MOVE_FORCEINLINE move_iterator()
  54. : m_it()
  55. {}
  56. BOOST_MOVE_FORCEINLINE explicit move_iterator(const It &i)
  57. : m_it(i)
  58. {}
  59. template <class U>
  60. BOOST_MOVE_FORCEINLINE move_iterator(const move_iterator<U>& u)
  61. : m_it(u.m_it)
  62. {}
  63. BOOST_MOVE_FORCEINLINE reference operator*() const
  64. {
  65. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
  66. return *m_it;
  67. #else
  68. return ::boost::move(*m_it);
  69. #endif
  70. }
  71. BOOST_MOVE_FORCEINLINE pointer operator->() const
  72. { return m_it; }
  73. BOOST_MOVE_FORCEINLINE move_iterator& operator++()
  74. { ++m_it; return *this; }
  75. BOOST_MOVE_FORCEINLINE move_iterator<iterator_type> operator++(int)
  76. { move_iterator<iterator_type> tmp(*this); ++(*this); return tmp; }
  77. BOOST_MOVE_FORCEINLINE move_iterator& operator--()
  78. { --m_it; return *this; }
  79. BOOST_MOVE_FORCEINLINE move_iterator<iterator_type> operator--(int)
  80. { move_iterator<iterator_type> tmp(*this); --(*this); return tmp; }
  81. move_iterator<iterator_type> operator+ (difference_type n) const
  82. { return move_iterator<iterator_type>(m_it + n); }
  83. BOOST_MOVE_FORCEINLINE move_iterator& operator+=(difference_type n)
  84. { m_it += n; return *this; }
  85. BOOST_MOVE_FORCEINLINE move_iterator<iterator_type> operator- (difference_type n) const
  86. { return move_iterator<iterator_type>(m_it - n); }
  87. BOOST_MOVE_FORCEINLINE move_iterator& operator-=(difference_type n)
  88. { m_it -= n; return *this; }
  89. BOOST_MOVE_FORCEINLINE reference operator[](difference_type n) const
  90. {
  91. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_MOVE_OLD_RVALUE_REF_BINDING_RULES)
  92. return m_it[n];
  93. #else
  94. return ::boost::move(m_it[n]);
  95. #endif
  96. }
  97. BOOST_MOVE_FORCEINLINE friend bool operator==(const move_iterator& x, const move_iterator& y)
  98. { return x.m_it == y.m_it; }
  99. BOOST_MOVE_FORCEINLINE friend bool operator!=(const move_iterator& x, const move_iterator& y)
  100. { return x.m_it != y.m_it; }
  101. BOOST_MOVE_FORCEINLINE friend bool operator< (const move_iterator& x, const move_iterator& y)
  102. { return x.m_it < y.m_it; }
  103. BOOST_MOVE_FORCEINLINE friend bool operator<=(const move_iterator& x, const move_iterator& y)
  104. { return x.m_it <= y.m_it; }
  105. BOOST_MOVE_FORCEINLINE friend bool operator> (const move_iterator& x, const move_iterator& y)
  106. { return x.m_it > y.m_it; }
  107. BOOST_MOVE_FORCEINLINE friend bool operator>=(const move_iterator& x, const move_iterator& y)
  108. { return x.m_it >= y.m_it; }
  109. BOOST_MOVE_FORCEINLINE friend difference_type operator-(const move_iterator& x, const move_iterator& y)
  110. { return x.m_it - y.m_it; }
  111. BOOST_MOVE_FORCEINLINE friend move_iterator operator+(difference_type n, const move_iterator& x)
  112. { return move_iterator(x.m_it + n); }
  113. private:
  114. It m_it;
  115. };
  116. //is_move_iterator
  117. namespace move_detail {
  118. template <class I>
  119. struct is_move_iterator
  120. {
  121. static const bool value = false;
  122. };
  123. template <class I>
  124. struct is_move_iterator< ::boost::move_iterator<I> >
  125. {
  126. static const bool value = true;
  127. };
  128. } //namespace move_detail {
  129. //////////////////////////////////////////////////////////////////////////////
  130. //
  131. // move_iterator
  132. //
  133. //////////////////////////////////////////////////////////////////////////////
  134. //!
  135. //! <b>Returns</b>: move_iterator<It>(i).
  136. template<class It>
  137. inline move_iterator<It> make_move_iterator(const It &it)
  138. { return move_iterator<It>(it); }
  139. //////////////////////////////////////////////////////////////////////////////
  140. //
  141. // back_move_insert_iterator
  142. //
  143. //////////////////////////////////////////////////////////////////////////////
  144. //! A move insert iterator that move constructs elements at the
  145. //! back of a container
  146. template <typename C> // C models Container
  147. class back_move_insert_iterator
  148. {
  149. C* container_m;
  150. public:
  151. typedef C container_type;
  152. typedef typename C::value_type value_type;
  153. typedef typename C::reference reference;
  154. typedef typename C::pointer pointer;
  155. typedef typename C::difference_type difference_type;
  156. typedef std::output_iterator_tag iterator_category;
  157. explicit back_move_insert_iterator(C& x) : container_m(&x) { }
  158. back_move_insert_iterator& operator=(reference x)
  159. { container_m->push_back(boost::move(x)); return *this; }
  160. back_move_insert_iterator& operator=(BOOST_RV_REF(value_type) x)
  161. { reference rx = x; return this->operator=(rx); }
  162. back_move_insert_iterator& operator*() { return *this; }
  163. back_move_insert_iterator& operator++() { return *this; }
  164. back_move_insert_iterator& operator++(int) { return *this; }
  165. };
  166. //!
  167. //! <b>Returns</b>: back_move_insert_iterator<C>(x).
  168. template <typename C> // C models Container
  169. inline back_move_insert_iterator<C> back_move_inserter(C& x)
  170. {
  171. return back_move_insert_iterator<C>(x);
  172. }
  173. //////////////////////////////////////////////////////////////////////////////
  174. //
  175. // front_move_insert_iterator
  176. //
  177. //////////////////////////////////////////////////////////////////////////////
  178. //! A move insert iterator that move constructs elements int the
  179. //! front of a container
  180. template <typename C> // C models Container
  181. class front_move_insert_iterator
  182. {
  183. C* container_m;
  184. public:
  185. typedef C container_type;
  186. typedef typename C::value_type value_type;
  187. typedef typename C::reference reference;
  188. typedef typename C::pointer pointer;
  189. typedef typename C::difference_type difference_type;
  190. typedef std::output_iterator_tag iterator_category;
  191. explicit front_move_insert_iterator(C& x) : container_m(&x) { }
  192. front_move_insert_iterator& operator=(reference x)
  193. { container_m->push_front(boost::move(x)); return *this; }
  194. front_move_insert_iterator& operator=(BOOST_RV_REF(value_type) x)
  195. { reference rx = x; return this->operator=(rx); }
  196. front_move_insert_iterator& operator*() { return *this; }
  197. front_move_insert_iterator& operator++() { return *this; }
  198. front_move_insert_iterator& operator++(int) { return *this; }
  199. };
  200. //!
  201. //! <b>Returns</b>: front_move_insert_iterator<C>(x).
  202. template <typename C> // C models Container
  203. inline front_move_insert_iterator<C> front_move_inserter(C& x)
  204. {
  205. return front_move_insert_iterator<C>(x);
  206. }
  207. //////////////////////////////////////////////////////////////////////////////
  208. //
  209. // insert_move_iterator
  210. //
  211. //////////////////////////////////////////////////////////////////////////////
  212. template <typename C> // C models Container
  213. class move_insert_iterator
  214. {
  215. C* container_m;
  216. typename C::iterator pos_;
  217. public:
  218. typedef C container_type;
  219. typedef typename C::value_type value_type;
  220. typedef typename C::reference reference;
  221. typedef typename C::pointer pointer;
  222. typedef typename C::difference_type difference_type;
  223. typedef std::output_iterator_tag iterator_category;
  224. explicit move_insert_iterator(C& x, typename C::iterator pos)
  225. : container_m(&x), pos_(pos)
  226. {}
  227. move_insert_iterator& operator=(reference x)
  228. {
  229. pos_ = container_m->insert(pos_, ::boost::move(x));
  230. ++pos_;
  231. return *this;
  232. }
  233. move_insert_iterator& operator=(BOOST_RV_REF(value_type) x)
  234. { reference rx = x; return this->operator=(rx); }
  235. move_insert_iterator& operator*() { return *this; }
  236. move_insert_iterator& operator++() { return *this; }
  237. move_insert_iterator& operator++(int) { return *this; }
  238. };
  239. //!
  240. //! <b>Returns</b>: move_insert_iterator<C>(x, it).
  241. template <typename C> // C models Container
  242. inline move_insert_iterator<C> move_inserter(C& x, typename C::iterator it)
  243. {
  244. return move_insert_iterator<C>(x, it);
  245. }
  246. } //namespace boost {
  247. #include <boost/move/detail/config_end.hpp>
  248. #endif //#ifndef BOOST_MOVE_ITERATOR_HPP