1 /* Copyright 2003-2019 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_DETAIL_SEQ_INDEX_NODE_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_SEQ_INDEX_NODE_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/multi_index/detail/allocator_traits.hpp>
19 #include <boost/multi_index/detail/raw_ptr.hpp>
23 namespace multi_index{
27 /* doubly-linked node for use by sequenced_index */
29 template<typename Allocator>
30 struct sequenced_index_node_impl
32 typedef typename rebind_alloc_for<
33 Allocator,sequenced_index_node_impl
34 >::type node_allocator;
35 typedef allocator_traits<node_allocator> alloc_traits;
36 typedef typename alloc_traits::pointer pointer;
37 typedef typename alloc_traits::const_pointer const_pointer;
38 typedef typename alloc_traits::difference_type difference_type;
40 pointer& prior(){return prior_;}
41 pointer prior()const{return prior_;}
42 pointer& next(){return next_;}
43 pointer next()const{return next_;}
45 /* interoperability with bidir_node_iterator */
47 static void increment(pointer& x){x=x->next();}
48 static void decrement(pointer& x){x=x->prior();}
50 /* algorithmic stuff */
52 static void link(pointer x,pointer header)
54 x->prior()=header->prior();
56 x->prior()->next()=x->next()->prior()=x;
59 static void unlink(pointer x)
61 x->prior()->next()=x->next();
62 x->next()->prior()=x->prior();
65 static void relink(pointer position,pointer x)
68 x->prior()=position->prior();
70 x->prior()->next()=x->next()->prior()=x;
73 static void relink(pointer position,pointer x,pointer y)
75 /* position is assumed not to be in [x,y) */
80 y->prior()=x->prior();
81 x->prior()=position->prior();
88 static void reverse(pointer header)
93 std::swap(x->prior(),x->next());
98 static void swap(pointer x,pointer y)
100 /* This swap function does not exchange the header nodes,
101 * but rather their pointers. This is *not* used for implementing
102 * sequenced_index::swap.
107 std::swap(x->next(),y->next());
108 std::swap(x->prior(),y->prior());
109 x->next()->prior()=x->prior()->next()=x;
110 y->next()->prior()=y->prior()->next()=y;
114 y->prior()=x->prior();
115 x->next()=x->prior()=x;
116 y->next()->prior()=y->prior()->next()=y;
119 else if(y->next()!=y){
121 x->prior()=y->prior();
122 y->next()=y->prior()=y;
123 x->next()->prior()=x->prior()->next()=x;
132 template<typename Super>
133 struct sequenced_index_node_trampoline:
134 sequenced_index_node_impl<
135 typename rebind_alloc_for<
136 typename Super::allocator_type,
141 typedef sequenced_index_node_impl<
142 typename rebind_alloc_for<
143 typename Super::allocator_type,
149 template<typename Super>
150 struct sequenced_index_node:Super,sequenced_index_node_trampoline<Super>
153 typedef sequenced_index_node_trampoline<Super> trampoline;
156 typedef typename trampoline::impl_type impl_type;
157 typedef typename trampoline::pointer impl_pointer;
158 typedef typename trampoline::const_pointer const_impl_pointer;
159 typedef typename trampoline::difference_type difference_type;
161 impl_pointer& prior(){return trampoline::prior();}
162 impl_pointer prior()const{return trampoline::prior();}
163 impl_pointer& next(){return trampoline::next();}
164 impl_pointer next()const{return trampoline::next();}
168 return static_cast<impl_pointer>(
169 static_cast<impl_type*>(static_cast<trampoline*>(this)));
172 const_impl_pointer impl()const
174 return static_cast<const_impl_pointer>(
175 static_cast<const impl_type*>(static_cast<const trampoline*>(this)));
178 static sequenced_index_node* from_impl(impl_pointer x)
181 static_cast<sequenced_index_node*>(
182 static_cast<trampoline*>(
183 raw_ptr<impl_type*>(x)));
186 static const sequenced_index_node* from_impl(const_impl_pointer x)
189 static_cast<const sequenced_index_node*>(
190 static_cast<const trampoline*>(
191 raw_ptr<const impl_type*>(x)));
194 /* interoperability with bidir_node_iterator */
196 static void increment(sequenced_index_node*& x)
198 impl_pointer xi=x->impl();
199 trampoline::increment(xi);
203 static void decrement(sequenced_index_node*& x)
205 impl_pointer xi=x->impl();
206 trampoline::decrement(xi);
211 } /* namespace multi_index::detail */
213 } /* namespace multi_index */
215 } /* namespace boost */