1 /////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2007-2013
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)
9 // See http://www.boost.org/libs/intrusive for documentation.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_TREE_ITERATOR_HPP
14 #define BOOST_INTRUSIVE_TREE_ITERATOR_HPP
16 #ifndef BOOST_CONFIG_HPP
17 # include <boost/config.hpp>
20 #if defined(BOOST_HAS_PRAGMA_ONCE)
24 #include <boost/intrusive/detail/config_begin.hpp>
25 #include <boost/intrusive/detail/workaround.hpp>
26 #include <boost/intrusive/detail/std_fwd.hpp>
27 #include <boost/intrusive/detail/iiterator.hpp>
28 #include <boost/intrusive/detail/bstree_algorithms_base.hpp>
33 /////////////////////////////////////////////////////////////////////////////
35 // Implementation of the tree iterator //
37 /////////////////////////////////////////////////////////////////////////////
39 // tree_iterator provides some basic functions for a
40 // node oriented bidirectional iterator:
41 template<class ValueTraits, bool IsConst>
45 typedef iiterator< ValueTraits, IsConst
46 , std::bidirectional_iterator_tag> types_t;
47 typedef typename types_t::value_traits value_traits;
48 typedef typename types_t::node_traits node_traits;
49 typedef typename types_t::node node;
50 typedef typename types_t::node_ptr node_ptr;
51 typedef typename types_t::const_value_traits_ptr const_value_traits_ptr;
52 typedef bstree_algorithms_base<node_traits> node_algorithms;
54 static const bool stateful_value_traits = types_t::stateful_value_traits;
56 void unspecified_bool_type_func() const {}
57 typedef void (tree_iterator::*unspecified_bool_type)() const;
60 typedef typename types_t::iterator_type::difference_type difference_type;
61 typedef typename types_t::iterator_type::value_type value_type;
62 typedef typename types_t::iterator_type::pointer pointer;
63 typedef typename types_t::iterator_type::reference reference;
64 typedef typename types_t::iterator_type::iterator_category iterator_category;
66 BOOST_INTRUSIVE_FORCEINLINE tree_iterator()
69 BOOST_INTRUSIVE_FORCEINLINE explicit tree_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr)
70 : members_(nodeptr, traits_ptr)
73 BOOST_INTRUSIVE_FORCEINLINE tree_iterator(tree_iterator<value_traits, false> const& other)
74 : members_(other.pointed_node(), other.get_value_traits())
77 BOOST_INTRUSIVE_FORCEINLINE const node_ptr &pointed_node() const
78 { return members_.nodeptr_; }
80 BOOST_INTRUSIVE_FORCEINLINE tree_iterator &operator=(const node_ptr &nodeptr)
81 { members_.nodeptr_ = nodeptr; return static_cast<tree_iterator&>(*this); }
84 tree_iterator& operator++()
86 members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
87 return static_cast<tree_iterator&> (*this);
90 tree_iterator operator++(int)
92 tree_iterator result (*this);
93 members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
97 tree_iterator& operator--()
99 members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
100 return static_cast<tree_iterator&> (*this);
103 tree_iterator operator--(int)
105 tree_iterator result (*this);
106 members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
110 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_left()
112 members_.nodeptr_ = node_traits::get_left(members_.nodeptr_);
113 return static_cast<tree_iterator&> (*this);
116 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_right()
118 members_.nodeptr_ = node_traits::get_right(members_.nodeptr_);
119 return static_cast<tree_iterator&> (*this);
122 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_parent()
124 members_.nodeptr_ = node_traits::get_parent(members_.nodeptr_);
125 return static_cast<tree_iterator&> (*this);
128 BOOST_INTRUSIVE_FORCEINLINE operator unspecified_bool_type() const
129 { return members_.nodeptr_ ? &tree_iterator::unspecified_bool_type_func : 0; }
131 BOOST_INTRUSIVE_FORCEINLINE bool operator! () const
132 { return !members_.nodeptr_; }
134 BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const tree_iterator& l, const tree_iterator& r)
135 { return l.pointed_node() == r.pointed_node(); }
137 BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const tree_iterator& l, const tree_iterator& r)
138 { return !(l == r); }
140 BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
141 { return *operator->(); }
143 BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
144 { return this->operator_arrow(detail::bool_<stateful_value_traits>()); }
146 BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr get_value_traits() const
147 { return members_.get_ptr(); }
149 tree_iterator end_iterator_from_it() const
151 return tree_iterator(node_algorithms::get_header(this->pointed_node()), this->get_value_traits());
154 tree_iterator<value_traits, false> unconst() const
155 { return tree_iterator<value_traits, false>(this->pointed_node(), this->get_value_traits()); }
158 BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::false_) const
159 { return ValueTraits::to_value_ptr(members_.nodeptr_); }
161 BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::true_) const
162 { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); }
164 iiterator_members<node_ptr, const_value_traits_ptr, stateful_value_traits> members_;
167 } //namespace intrusive
170 #include <boost/intrusive/detail/config_end.hpp>
172 #endif //BOOST_INTRUSIVE_TREE_ITERATOR_HPP