1 /* Copyright 2003-2015 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_RND_INDEX_OPS_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_RND_INDEX_OPS_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/multi_index/detail/rnd_index_ptr_array.hpp>
22 namespace multi_index{
26 /* Common code for random_access_index memfuns having templatized and
27 * non-templatized versions.
30 template<typename Node,typename Allocator,typename Predicate>
31 Node* random_access_index_remove(
32 random_access_index_ptr_array<Allocator>& ptrs,Predicate pred)
34 typedef typename Node::value_type value_type;
35 typedef typename Node::impl_ptr_pointer impl_ptr_pointer;
37 impl_ptr_pointer first=ptrs.begin(),
40 for(;first!=last;++first){
42 const_cast<const value_type&>(Node::from_impl(*first)->value()))){
44 std::swap(*first,*res);
51 return Node::from_impl(*res);
54 template<typename Node,typename Allocator,class BinaryPredicate>
55 Node* random_access_index_unique(
56 random_access_index_ptr_array<Allocator>& ptrs,BinaryPredicate binary_pred)
58 typedef typename Node::value_type value_type;
59 typedef typename Node::impl_ptr_pointer impl_ptr_pointer;
61 impl_ptr_pointer first=ptrs.begin(),
67 const_cast<const value_type&>(Node::from_impl(*res)->value()),
68 const_cast<const value_type&>(Node::from_impl(*first)->value()))){
71 std::swap(*first,*res);
79 return Node::from_impl(*res);
82 template<typename Node,typename Allocator,typename Compare>
83 void random_access_index_inplace_merge(
85 random_access_index_ptr_array<Allocator>& ptrs,
86 BOOST_DEDUCED_TYPENAME Node::impl_ptr_pointer first1,Compare comp)
88 typedef typename Node::value_type value_type;
89 typedef typename Node::impl_pointer impl_pointer;
90 typedef typename Node::impl_ptr_pointer impl_ptr_pointer;
92 auto_space<impl_pointer,Allocator> spc(al,ptrs.size());
94 impl_ptr_pointer first0=ptrs.begin(),
98 while(first0!=last0&&first1!=last1){
100 const_cast<const value_type&>(Node::from_impl(*first1)->value()),
101 const_cast<const value_type&>(Node::from_impl(*first0)->value()))){
108 std::copy(&*first0,&*last0,&*out);
109 std::copy(&*first1,&*last1,&*out);
113 while(first1!=last1){
115 (*first1)->up()=first1;
122 /* auxiliary stuff */
124 template<typename Node,typename Compare>
125 struct random_access_index_sort_compare
127 typedef typename Node::impl_pointer first_argument_type;
128 typedef typename Node::impl_pointer second_argument_type;
129 typedef bool result_type;
131 random_access_index_sort_compare(Compare comp_=Compare()):comp(comp_){}
134 typename Node::impl_pointer x,typename Node::impl_pointer y)const
136 typedef typename Node::value_type value_type;
139 const_cast<const value_type&>(Node::from_impl(x)->value()),
140 const_cast<const value_type&>(Node::from_impl(y)->value()));
147 template<typename Node,typename Allocator,class Compare>
148 void random_access_index_sort(
150 random_access_index_ptr_array<Allocator>& ptrs,
153 /* The implementation is extremely simple: an auxiliary
154 * array of pointers is sorted using stdlib facilities and
155 * then used to rearrange the index. This is suboptimal
156 * in space and time, but has some advantages over other
157 * possible approaches:
158 * - Use std::stable_sort() directly on ptrs using some
159 * special iterator in charge of maintaining pointers
160 * and up() pointers in sync: we cannot guarantee
161 * preservation of the container invariants in the face of
162 * exceptions, if, for instance, std::stable_sort throws
163 * when ptrs transitorily contains duplicate elements.
164 * - Rewrite the internal algorithms of std::stable_sort
165 * adapted for this case: besides being a fair amount of
166 * work, making a stable sort compatible with Boost.MultiIndex
167 * invariants (basically, no duplicates or missing elements
168 * even if an exception is thrown) is complicated, error-prone
169 * and possibly won't perform much better than the
173 if(ptrs.size()<=1)return;
175 typedef typename Node::impl_pointer impl_pointer;
176 typedef typename Node::impl_ptr_pointer impl_ptr_pointer;
177 typedef random_access_index_sort_compare<
178 Node,Compare> ptr_compare;
180 impl_ptr_pointer first=ptrs.begin();
181 impl_ptr_pointer last=ptrs.end();
184 Allocator> spc(al,ptrs.size());
185 impl_ptr_pointer buf=spc.data();
187 std::copy(&*first,&*last,&*buf);
188 std::stable_sort(&*buf,&*buf+ptrs.size(),ptr_compare(comp));
192 (*first)->up()=first;
197 } /* namespace multi_index::detail */
199 } /* namespace multi_index */
201 } /* namespace boost */