tree_iterator.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. public:
  50. typedef typename types_t::iterator_type::difference_type difference_type;
  51. typedef typename types_t::iterator_type::value_type value_type;
  52. typedef typename types_t::iterator_type::pointer pointer;
  53. typedef typename types_t::iterator_type::reference reference;
  54. typedef typename types_t::iterator_type::iterator_category iterator_category;
  55. BOOST_INTRUSIVE_FORCEINLINE tree_iterator()
  56. {}
  57. BOOST_INTRUSIVE_FORCEINLINE explicit tree_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr)
  58. : members_(nodeptr, traits_ptr)
  59. {}
  60. BOOST_INTRUSIVE_FORCEINLINE tree_iterator(tree_iterator<value_traits, false> const& other)
  61. : members_(other.pointed_node(), other.get_value_traits())
  62. {}
  63. BOOST_INTRUSIVE_FORCEINLINE const node_ptr &pointed_node() const
  64. { return members_.nodeptr_; }
  65. BOOST_INTRUSIVE_FORCEINLINE tree_iterator &operator=(const node_ptr &nodeptr)
  66. { members_.nodeptr_ = nodeptr; return static_cast<tree_iterator&>(*this); }
  67. public:
  68. tree_iterator& operator++()
  69. {
  70. members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
  71. return static_cast<tree_iterator&> (*this);
  72. }
  73. tree_iterator operator++(int)
  74. {
  75. tree_iterator result (*this);
  76. members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
  77. return result;
  78. }
  79. tree_iterator& operator--()
  80. {
  81. members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
  82. return static_cast<tree_iterator&> (*this);
  83. }
  84. tree_iterator operator--(int)
  85. {
  86. tree_iterator result (*this);
  87. members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
  88. return result;
  89. }
  90. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_left()
  91. {
  92. members_.nodeptr_ = node_traits::get_left(members_.nodeptr_);
  93. return static_cast<tree_iterator&> (*this);
  94. }
  95. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_right()
  96. {
  97. members_.nodeptr_ = node_traits::get_right(members_.nodeptr_);
  98. return static_cast<tree_iterator&> (*this);
  99. }
  100. BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_parent()
  101. {
  102. members_.nodeptr_ = node_traits::get_parent(members_.nodeptr_);
  103. return static_cast<tree_iterator&> (*this);
  104. }
  105. BOOST_INTRUSIVE_FORCEINLINE operator unspecified_bool_type() const
  106. { return members_.nodeptr_ ? &tree_iterator::unspecified_bool_type_func : 0; }
  107. BOOST_INTRUSIVE_FORCEINLINE bool operator! () const
  108. { return !members_.nodeptr_; }
  109. BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const tree_iterator& l, const tree_iterator& r)
  110. { return l.pointed_node() == r.pointed_node(); }
  111. BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const tree_iterator& l, const tree_iterator& r)
  112. { return !(l == r); }
  113. BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
  114. { return *operator->(); }
  115. BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
  116. { return this->operator_arrow(detail::bool_<stateful_value_traits>()); }
  117. BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr get_value_traits() const
  118. { return members_.get_ptr(); }
  119. tree_iterator end_iterator_from_it() const
  120. {
  121. return tree_iterator(node_algorithms::get_header(this->pointed_node()), this->get_value_traits());
  122. }
  123. tree_iterator<value_traits, false> unconst() const
  124. { return tree_iterator<value_traits, false>(this->pointed_node(), this->get_value_traits()); }
  125. private:
  126. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::false_) const
  127. { return ValueTraits::to_value_ptr(members_.nodeptr_); }
  128. BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::true_) const
  129. { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); }
  130. iiterator_members<node_ptr, const_value_traits_ptr, stateful_value_traits> members_;
  131. };
  132. } //namespace intrusive
  133. } //namespace boost
  134. #include <boost/intrusive/detail/config_end.hpp>
  135. #endif //BOOST_INTRUSIVE_TREE_ITERATOR_HPP