1 /* Copyright 2003-2018 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 */
19 #include <boost/detail/allocator_utilities.hpp>
20 #include <boost/multi_index/detail/raw_ptr.hpp>
24 namespace multi_index{
28 /* doubly-linked node for use by sequenced_index */
30 template<typename Allocator>
31 struct sequenced_index_node_impl
34 boost::detail::allocator::rebind_to<
35 Allocator,sequenced_index_node_impl
36 >::type node_allocator;
37 #ifdef BOOST_NO_CXX11_ALLOCATOR
38 typedef typename node_allocator::pointer pointer;
39 typedef typename node_allocator::const_pointer const_pointer;
41 typedef std::allocator_traits<node_allocator> allocator_traits;
42 typedef typename allocator_traits::pointer pointer;
43 typedef typename allocator_traits::const_pointer const_pointer;
45 pointer& prior(){return prior_;}
46 pointer prior()const{return prior_;}
47 pointer& next(){return next_;}
48 pointer next()const{return next_;}
50 /* interoperability with bidir_node_iterator */
52 static void increment(pointer& x){x=x->next();}
53 static void decrement(pointer& x){x=x->prior();}
55 /* algorithmic stuff */
57 static void link(pointer x,pointer header)
59 x->prior()=header->prior();
61 x->prior()->next()=x->next()->prior()=x;
64 static void unlink(pointer x)
66 x->prior()->next()=x->next();
67 x->next()->prior()=x->prior();
70 static void relink(pointer position,pointer x)
73 x->prior()=position->prior();
75 x->prior()->next()=x->next()->prior()=x;
78 static void relink(pointer position,pointer x,pointer y)
80 /* position is assumed not to be in [x,y) */
85 y->prior()=x->prior();
86 x->prior()=position->prior();
93 static void reverse(pointer header)
98 std::swap(x->prior(),x->next());
103 static void swap(pointer x,pointer y)
105 /* This swap function does not exchange the header nodes,
106 * but rather their pointers. This is *not* used for implementing
107 * sequenced_index::swap.
112 std::swap(x->next(),y->next());
113 std::swap(x->prior(),y->prior());
114 x->next()->prior()=x->prior()->next()=x;
115 y->next()->prior()=y->prior()->next()=y;
119 y->prior()=x->prior();
120 x->next()=x->prior()=x;
121 y->next()->prior()=y->prior()->next()=y;
124 else if(y->next()!=y){
126 x->prior()=y->prior();
127 y->next()=y->prior()=y;
128 x->next()->prior()=x->prior()->next()=x;
137 template<typename Super>
138 struct sequenced_index_node_trampoline:
139 sequenced_index_node_impl<
140 typename boost::detail::allocator::rebind_to<
141 typename Super::allocator_type,
146 typedef sequenced_index_node_impl<
147 typename boost::detail::allocator::rebind_to<
148 typename Super::allocator_type,
154 template<typename Super>
155 struct sequenced_index_node:Super,sequenced_index_node_trampoline<Super>
158 typedef sequenced_index_node_trampoline<Super> trampoline;
161 typedef typename trampoline::impl_type impl_type;
162 typedef typename trampoline::pointer impl_pointer;
163 typedef typename trampoline::const_pointer const_impl_pointer;
165 impl_pointer& prior(){return trampoline::prior();}
166 impl_pointer prior()const{return trampoline::prior();}
167 impl_pointer& next(){return trampoline::next();}
168 impl_pointer next()const{return trampoline::next();}
172 return static_cast<impl_pointer>(
173 static_cast<impl_type*>(static_cast<trampoline*>(this)));
176 const_impl_pointer impl()const
178 return static_cast<const_impl_pointer>(
179 static_cast<const impl_type*>(static_cast<const trampoline*>(this)));
182 static sequenced_index_node* from_impl(impl_pointer x)
185 static_cast<sequenced_index_node*>(
186 static_cast<trampoline*>(
187 raw_ptr<impl_type*>(x)));
190 static const sequenced_index_node* from_impl(const_impl_pointer x)
193 static_cast<const sequenced_index_node*>(
194 static_cast<const trampoline*>(
195 raw_ptr<const impl_type*>(x)));
198 /* interoperability with bidir_node_iterator */
200 static void increment(sequenced_index_node*& x)
202 impl_pointer xi=x->impl();
203 trampoline::increment(xi);
207 static void decrement(sequenced_index_node*& x)
209 impl_pointer xi=x->impl();
210 trampoline::decrement(xi);
215 } /* namespace multi_index::detail */
217 } /* namespace multi_index */
219 } /* namespace boost */