tree_iterator.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  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_TREE_ITERATOR_HPP
  13. #define BOOST_INTRUSIVE_TREE_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 <boost/intrusive/detail/config_begin.hpp>
  21. #include <boost/intrusive/detail/workaround.hpp>
  22. #include <boost/intrusive/detail/std_fwd.hpp>
  23. #include <boost/intrusive/detail/iiterator.hpp>
  24. #include <boost/intrusive/detail/bstree_algorithms_base.hpp>
  25. namespace boost {
  26. namespace intrusive {
  27. /////////////////////////////////////////////////////////////////////////////
  28. // //
  29. // Implementation of the tree iterator //
  30. // //
  31. /////////////////////////////////////////////////////////////////////////////
  32. // tree_iterator provides some basic functions for a
  33. // node oriented bidirectional iterator:
  34. template<class ValueTraits, bool IsConst>
  35. class tree_iterator
  36. {
  37. private:
  38. typedef iiterator< ValueTraits, IsConst
  39. , std::bidirectional_iterator_tag> types_t;
  40. typedef typename types_t::value_traits value_traits;
  41. typedef typename types_t::node_traits node_traits;
  42. typedef typename types_t::node node;
  43. typedef typename types_t::node_ptr node_ptr;
  44. typedef typename types_t::const_value_traits_ptr const_value_traits_ptr;
  45. typedef bstree_algorithms_base<node_traits> node_algorithms;
  46. static const bool stateful_value_traits = types_t::stateful_value_traits;
  47. void unspecified_bool_type_func() const {}
  48. typedef void (tree_iterator::*unspecified_bool_type)() const;
  49. class nat;
  50. typedef typename
  51. detail::if_c< IsConst
  52. , tree_iterator<value_traits, false>
  53. , nat>::type nonconst_iterator;
  54. public:
  55. typedef typename types_t::iterator_type::difference_type difference_type;
  56. typedef typename types_t::iterator_type::value_type value_type;
  57. typedef typename types_t::iterator_type::pointer pointer;
  58. typedef typename types_t::iterator_type::reference reference;
  59. typedef typename types_t::iterator_type::iterator_category iterator_category;
  60. BOOST_INTRUSIVE_FORCEINLINE tree_iterator()
  61. {}
  62. BOOST_INTRUSIVE_FORCEINLINE explicit tree_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr)
  63. : members_(nodeptr, traits_ptr)
  64. {}
  65. BOOST_INTRUSIVE_FORCEINLINE tree_iterator(const tree_iterator &other)
  66. : members_(other.pointed_node(), other.get_value_traits())
  67. {}
  68. BOOST_INTRUSIVE_FORCEINLINE tree_iterator(const nonconst_iterator &other)
  69. : members_(other.pointed_node(), other.get_value_traits())
  70. {}
  71. BOOST_INTRUSIVE_FORCEINLINE tree_iterator &operator=(const tree_iterator &other)
  72. { members_.nodeptr_ = other.members_.nodeptr_; return *this; }
  73. BOOST_INTRUSIVE_FORCEINLINE tree_iterator &operator=(const node_ptr &nodeptr)
  74. { members_.nodeptr_ = nodeptr; return *this; }
  75. BOOST_INTRUSIVE_FORCEINLINE node_ptr pointed_node() const
  76. { return members_.nodeptr_; }
  77. public:
  78. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& operator++()
  79. {
  80. members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
  81. return *this;
  82. }
  83. tree_iterator operator++(int)
  84. {
  85. tree_iterator result (*this);
  86. members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
  87. return result;
  88. }
  89. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& operator--()
  90. {
  91. members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
  92. return *this;
  93. }
  94. tree_iterator operator--(int)
  95. {
  96. tree_iterator result (*this);
  97. members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
  98. return result;
  99. }
  100. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_left()
  101. {
  102. members_.nodeptr_ = node_traits::get_left(members_.nodeptr_);
  103. return *this;
  104. }
  105. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_right()
  106. {
  107. members_.nodeptr_ = node_traits::get_right(members_.nodeptr_);
  108. return *this;
  109. }
  110. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_parent()
  111. {
  112. members_.nodeptr_ = node_traits::get_parent(members_.nodeptr_);
  113. return *this;
  114. }
  115. BOOST_INTRUSIVE_FORCEINLINE operator unspecified_bool_type() const
  116. { return members_.nodeptr_ ? &tree_iterator::unspecified_bool_type_func : 0; }
  117. BOOST_INTRUSIVE_FORCEINLINE bool operator! () const
  118. { return !members_.nodeptr_; }
  119. BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const tree_iterator& l, const tree_iterator& r)
  120. { return l.pointed_node() == r.pointed_node(); }
  121. BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const tree_iterator& l, const tree_iterator& r)
  122. { return !(l == r); }
  123. BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
  124. { return *operator->(); }
  125. BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
  126. { return this->operator_arrow(detail::bool_<stateful_value_traits>()); }
  127. BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr get_value_traits() const
  128. { return members_.get_ptr(); }
  129. tree_iterator end_iterator_from_it() const
  130. {
  131. return tree_iterator(node_algorithms::get_header(this->pointed_node()), this->get_value_traits());
  132. }
  133. tree_iterator<value_traits, false> unconst() const
  134. { return tree_iterator<value_traits, false>(this->pointed_node(), this->get_value_traits()); }
  135. private:
  136. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::false_) const
  137. { return ValueTraits::to_value_ptr(members_.nodeptr_); }
  138. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::true_) const
  139. { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); }
  140. iiterator_members<node_ptr, const_value_traits_ptr, stateful_value_traits> members_;
  141. };
  142. } //namespace intrusive
  143. } //namespace boost
  144. #include <boost/intrusive/detail/config_end.hpp>
  145. #endif //BOOST_INTRUSIVE_TREE_ITERATOR_HPP