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;
61 , tree_iterator<value_traits, false>
62 , nat>::type nonconst_iterator;
65 typedef typename types_t::iterator_type::difference_type difference_type;
66 typedef typename types_t::iterator_type::value_type value_type;
67 typedef typename types_t::iterator_type::pointer pointer;
68 typedef typename types_t::iterator_type::reference reference;
69 typedef typename types_t::iterator_type::iterator_category iterator_category;
71 BOOST_INTRUSIVE_FORCEINLINE tree_iterator()
74 BOOST_INTRUSIVE_FORCEINLINE explicit tree_iterator(const node_ptr & nodeptr, const const_value_traits_ptr &traits_ptr)
75 : members_(nodeptr, traits_ptr)
78 BOOST_INTRUSIVE_FORCEINLINE tree_iterator(const tree_iterator &other)
79 : members_(other.pointed_node(), other.get_value_traits())
82 BOOST_INTRUSIVE_FORCEINLINE tree_iterator(const nonconst_iterator &other)
83 : members_(other.pointed_node(), other.get_value_traits())
86 BOOST_INTRUSIVE_FORCEINLINE tree_iterator &operator=(const tree_iterator &other)
87 { members_.nodeptr_ = other.members_.nodeptr_; return *this; }
89 BOOST_INTRUSIVE_FORCEINLINE tree_iterator &operator=(const node_ptr &nodeptr)
90 { members_.nodeptr_ = nodeptr; return *this; }
92 BOOST_INTRUSIVE_FORCEINLINE node_ptr pointed_node() const
93 { return members_.nodeptr_; }
96 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& operator++()
98 members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
102 tree_iterator operator++(int)
104 tree_iterator result (*this);
105 members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
109 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& operator--()
111 members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
115 tree_iterator operator--(int)
117 tree_iterator result (*this);
118 members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
122 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_left()
124 members_.nodeptr_ = node_traits::get_left(members_.nodeptr_);
128 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_right()
130 members_.nodeptr_ = node_traits::get_right(members_.nodeptr_);
134 BOOST_INTRUSIVE_FORCEINLINE tree_iterator& go_parent()
136 members_.nodeptr_ = node_traits::get_parent(members_.nodeptr_);
140 BOOST_INTRUSIVE_FORCEINLINE operator unspecified_bool_type() const
141 { return members_.nodeptr_ ? &tree_iterator::unspecified_bool_type_func : 0; }
143 BOOST_INTRUSIVE_FORCEINLINE bool operator! () const
144 { return !members_.nodeptr_; }
146 BOOST_INTRUSIVE_FORCEINLINE friend bool operator== (const tree_iterator& l, const tree_iterator& r)
147 { return l.pointed_node() == r.pointed_node(); }
149 BOOST_INTRUSIVE_FORCEINLINE friend bool operator!= (const tree_iterator& l, const tree_iterator& r)
150 { return !(l == r); }
152 BOOST_INTRUSIVE_FORCEINLINE reference operator*() const
153 { return *operator->(); }
155 BOOST_INTRUSIVE_FORCEINLINE pointer operator->() const
156 { return this->operator_arrow(detail::bool_<stateful_value_traits>()); }
158 BOOST_INTRUSIVE_FORCEINLINE const_value_traits_ptr get_value_traits() const
159 { return members_.get_ptr(); }
161 tree_iterator end_iterator_from_it() const
163 return tree_iterator(node_algorithms::get_header(this->pointed_node()), this->get_value_traits());
166 tree_iterator<value_traits, false> unconst() const
167 { return tree_iterator<value_traits, false>(this->pointed_node(), this->get_value_traits()); }
170 BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::false_) const
171 { return ValueTraits::to_value_ptr(members_.nodeptr_); }
173 BOOST_INTRUSIVE_FORCEINLINE pointer operator_arrow(detail::true_) const
174 { return this->get_value_traits()->to_value_ptr(members_.nodeptr_); }
176 iiterator_members<node_ptr, const_value_traits_ptr, stateful_value_traits> members_;
179 } //namespace intrusive
182 #include <boost/intrusive/detail/config_end.hpp>
184 #endif //BOOST_INTRUSIVE_TREE_ITERATOR_HPP