iterator.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2014
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_DETAIL_ITERATOR_HPP
  13. #define BOOST_INTRUSIVE_DETAIL_ITERATOR_HPP
  14. #ifndef BOOST_CONFIG_HPP
  15. # include <boost/config.hpp>
  16. #endif
  17. #if defined(BOOST_HAS_PRAGMA_ONCE)
  18. # pragma once
  19. #endif
  20. #include <cstddef>
  21. #include <boost/intrusive/detail/std_fwd.hpp>
  22. #include <boost/intrusive/detail/workaround.hpp>
  23. #include <boost/move/detail/iterator_traits.hpp>
  24. #include <boost/move/detail/meta_utils_core.hpp>
  25. namespace boost {
  26. namespace intrusive {
  27. using boost::movelib::iterator_traits;
  28. ////////////////////
  29. // iterator
  30. ////////////////////
  31. template<class Category, class T, class Difference, class Pointer, class Reference>
  32. struct iterator
  33. {
  34. typedef Category iterator_category;
  35. typedef T value_type;
  36. typedef Difference difference_type;
  37. typedef Pointer pointer;
  38. typedef Reference reference;
  39. };
  40. ////////////////////////////////////////
  41. // iterator_[dis|en]able_if_tag
  42. ////////////////////////////////////////
  43. template<class I, class Tag, class R = void>
  44. struct iterator_enable_if_tag
  45. : ::boost::move_detail::enable_if_c
  46. < ::boost::move_detail::is_same
  47. < typename boost::intrusive::iterator_traits<I>::iterator_category
  48. , Tag
  49. >::value
  50. , R>
  51. {};
  52. template<class I, class Tag, class R = void>
  53. struct iterator_disable_if_tag
  54. : ::boost::move_detail::enable_if_c
  55. < !::boost::move_detail::is_same
  56. < typename boost::intrusive::iterator_traits<I>::iterator_category
  57. , Tag
  58. >::value
  59. , R>
  60. {};
  61. ////////////////////////////////////////
  62. // iterator_[dis|en]able_if_tag_difference_type
  63. ////////////////////////////////////////
  64. template<class I, class Tag>
  65. struct iterator_enable_if_tag_difference_type
  66. : iterator_enable_if_tag<I, Tag, typename boost::intrusive::iterator_traits<I>::difference_type>
  67. {};
  68. template<class I, class Tag>
  69. struct iterator_disable_if_tag_difference_type
  70. : iterator_disable_if_tag<I, Tag, typename boost::intrusive::iterator_traits<I>::difference_type>
  71. {};
  72. ////////////////////
  73. // advance
  74. ////////////////////
  75. template<class InputIt, class Distance>
  76. typename iterator_enable_if_tag<InputIt, std::input_iterator_tag>::type
  77. iterator_advance(InputIt& it, Distance n)
  78. {
  79. while(n--)
  80. ++it;
  81. }
  82. template<class InputIt, class Distance>
  83. typename iterator_enable_if_tag<InputIt, std::forward_iterator_tag>::type
  84. iterator_advance(InputIt& it, Distance n)
  85. {
  86. while(n--)
  87. ++it;
  88. }
  89. template<class InputIt, class Distance>
  90. typename iterator_enable_if_tag<InputIt, std::bidirectional_iterator_tag>::type
  91. iterator_advance(InputIt& it, Distance n)
  92. {
  93. for (; 0 < n; --n)
  94. ++it;
  95. for (; n < 0; ++n)
  96. --it;
  97. }
  98. template<class InputIt, class Distance>
  99. BOOST_INTRUSIVE_FORCEINLINE typename iterator_enable_if_tag<InputIt, std::random_access_iterator_tag>::type
  100. iterator_advance(InputIt& it, Distance n)
  101. {
  102. it += n;
  103. }
  104. ////////////////////
  105. // distance
  106. ////////////////////
  107. template<class InputIt> inline
  108. typename iterator_disable_if_tag_difference_type
  109. <InputIt, std::random_access_iterator_tag>::type
  110. iterator_distance(InputIt first, InputIt last)
  111. {
  112. typename iterator_traits<InputIt>::difference_type off = 0;
  113. while(first != last){
  114. ++off;
  115. ++first;
  116. }
  117. return off;
  118. }
  119. template<class InputIt>
  120. BOOST_INTRUSIVE_FORCEINLINE typename iterator_enable_if_tag_difference_type
  121. <InputIt, std::random_access_iterator_tag>::type
  122. iterator_distance(InputIt first, InputIt last)
  123. {
  124. typename iterator_traits<InputIt>::difference_type off = last - first;
  125. return off;
  126. }
  127. template<class I>
  128. BOOST_INTRUSIVE_FORCEINLINE typename iterator_traits<I>::pointer iterator_arrow_result(const I &i)
  129. { return i.operator->(); }
  130. template<class T>
  131. BOOST_INTRUSIVE_FORCEINLINE T * iterator_arrow_result(T *p)
  132. { return p; }
  133. } //namespace intrusive
  134. } //namespace boost
  135. #endif //BOOST_INTRUSIVE_DETAIL_ITERATOR_HPP