fix doc example typo
[boost.git] / boost / intrusive / detail / tree_node.hpp
blob146f758b0315e3b65b9cfa033cb283e6af397658
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2007.
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.
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef BOOST_INTRUSIVE_TREE_NODE_HPP
14 #define BOOST_INTRUSIVE_TREE_NODE_HPP
16 #include <boost/intrusive/detail/config_begin.hpp>
17 #include <iterator>
18 #include <boost/intrusive/detail/pointer_to_other.hpp>
19 #include <boost/intrusive/detail/mpl.hpp>
21 namespace boost {
22 namespace intrusive {
24 template<class VoidPointer>
25 struct tree_node
27 typedef typename pointer_to_other
28 <VoidPointer
29 ,tree_node<VoidPointer> >::type node_ptr;
31 node_ptr parent_, left_, right_;
34 template<class VoidPointer>
35 struct tree_node_traits
37 typedef tree_node<VoidPointer> node;
39 typedef typename boost::pointer_to_other
40 <VoidPointer, node>::type node_ptr;
41 typedef typename boost::pointer_to_other
42 <VoidPointer, const node>::type const_node_ptr;
44 static node_ptr get_parent(const_node_ptr n)
45 { return n->parent_; }
47 static void set_parent(node_ptr n, node_ptr p)
48 { n->parent_ = p; }
50 static node_ptr get_left(const_node_ptr n)
51 { return n->left_; }
53 static void set_left(node_ptr n, node_ptr l)
54 { n->left_ = l; }
56 static node_ptr get_right(const_node_ptr n)
57 { return n->right_; }
59 static void set_right(node_ptr n, node_ptr r)
60 { n->right_ = r; }
63 /////////////////////////////////////////////////////////////////////////////
64 // //
65 // Implementation of the tree iterator //
66 // //
67 /////////////////////////////////////////////////////////////////////////////
69 // tree_iterator provides some basic functions for a
70 // node oriented bidirectional iterator:
71 template<class Container, bool IsConst>
72 class tree_iterator
73 : public std::iterator
74 < std::bidirectional_iterator_tag
75 , typename detail::add_const_if_c
76 <typename Container::value_type, IsConst>::type
79 protected:
80 typedef typename Container::real_value_traits real_value_traits;
81 typedef typename Container::node_algorithms node_algorithms;
82 typedef typename real_value_traits::node_traits node_traits;
83 typedef typename node_traits::node node;
84 typedef typename node_traits::node_ptr node_ptr;
85 typedef typename boost::pointer_to_other
86 <node_ptr, void>::type void_pointer;
87 static const bool store_container_ptr =
88 detail::store_cont_ptr_on_it<Container>::value;
90 public:
91 public:
92 typedef typename detail::add_const_if_c
93 <typename Container::value_type, IsConst>
94 ::type value_type;
95 typedef value_type & reference;
96 typedef value_type * pointer;
98 tree_iterator()
99 : members_ (0, 0)
102 explicit tree_iterator(node_ptr nodeptr, const Container *cont_ptr)
103 : members_ (nodeptr, cont_ptr)
106 tree_iterator(tree_iterator<Container, false> const& other)
107 : members_(other.pointed_node(), other.get_container())
110 const node_ptr &pointed_node() const
111 { return members_.nodeptr_; }
113 tree_iterator &operator=(const node_ptr &nodeptr)
114 { members_.nodeptr_ = nodeptr; return static_cast<tree_iterator&>(*this); }
116 public:
117 tree_iterator& operator++()
119 members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
120 return static_cast<tree_iterator&> (*this);
123 tree_iterator operator++(int)
125 tree_iterator result (*this);
126 members_.nodeptr_ = node_algorithms::next_node(members_.nodeptr_);
127 return result;
130 tree_iterator& operator--()
132 members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
133 return static_cast<tree_iterator&> (*this);
136 tree_iterator operator--(int)
138 tree_iterator result (*this);
139 members_.nodeptr_ = node_algorithms::prev_node(members_.nodeptr_);
140 return result;
143 bool operator== (const tree_iterator& i) const
144 { return members_.nodeptr_ == i.pointed_node(); }
146 bool operator!= (const tree_iterator& i) const
147 { return !operator== (i); }
149 value_type& operator*() const
150 { return *operator->(); }
152 pointer operator->() const
153 { return detail::get_pointer(this->get_real_value_traits()->to_value_ptr(members_.nodeptr_)); }
155 const Container *get_container() const
157 if(store_container_ptr)
158 return static_cast<const Container*>(members_.get_ptr());
159 else
160 return 0;
163 const real_value_traits *get_real_value_traits() const
165 if(store_container_ptr)
166 return &this->get_container()->get_real_value_traits();
167 else
168 return 0;
171 tree_iterator end_iterator_from_it() const
173 return tree_iterator(node_algorithms::get_header(this->pointed_node()), this->get_container());
176 tree_iterator<Container, false> unconst() const
177 { return tree_iterator<Container, false>(this->pointed_node(), this->get_container()); }
179 private:
180 struct members
181 : public detail::select_constptr
182 <void_pointer, store_container_ptr>::type
184 typedef typename detail::select_constptr
185 <void_pointer, store_container_ptr>::type Base;
187 members(const node_ptr &n_ptr, const void *cont)
188 : Base(cont), nodeptr_(n_ptr)
191 node_ptr nodeptr_;
192 } members_;
195 } //namespace intrusive
196 } //namespace boost
198 #include <boost/intrusive/detail/config_end.hpp>
200 #endif //BOOST_INTRUSIVE_TREE_NODE_HPP