seq_index_ops.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright 2003-2016 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)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_SEQ_INDEX_OPS_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_SEQ_INDEX_OPS_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/detail/no_exceptions_support.hpp>
  15. #include <boost/multi_index/detail/seq_index_node.hpp>
  16. #include <boost/limits.hpp>
  17. #include <boost/type_traits/aligned_storage.hpp>
  18. #include <boost/type_traits/alignment_of.hpp>
  19. #include <cstddef>
  20. namespace boost{
  21. namespace multi_index{
  22. namespace detail{
  23. /* Common code for sequenced_index memfuns having templatized and
  24. * non-templatized versions.
  25. */
  26. template <typename SequencedIndex,typename Predicate>
  27. void sequenced_index_remove(SequencedIndex& x,Predicate pred)
  28. {
  29. typedef typename SequencedIndex::iterator iterator;
  30. iterator first=x.begin(),last=x.end();
  31. while(first!=last){
  32. if(pred(*first))x.erase(first++);
  33. else ++first;
  34. }
  35. }
  36. template <typename SequencedIndex,class BinaryPredicate>
  37. void sequenced_index_unique(SequencedIndex& x,BinaryPredicate binary_pred)
  38. {
  39. typedef typename SequencedIndex::iterator iterator;
  40. iterator first=x.begin();
  41. iterator last=x.end();
  42. if(first!=last){
  43. for(iterator middle=first;++middle!=last;middle=first){
  44. if(binary_pred(*middle,*first))x.erase(middle);
  45. else first=middle;
  46. }
  47. }
  48. }
  49. template <typename SequencedIndex,typename Compare>
  50. void sequenced_index_merge(SequencedIndex& x,SequencedIndex& y,Compare comp)
  51. {
  52. typedef typename SequencedIndex::iterator iterator;
  53. if(&x!=&y){
  54. iterator first0=x.begin(),last0=x.end();
  55. iterator first1=y.begin(),last1=y.end();
  56. while(first0!=last0&&first1!=last1){
  57. if(comp(*first1,*first0))x.splice(first0,y,first1++);
  58. else ++first0;
  59. }
  60. x.splice(last0,y,first1,last1);
  61. }
  62. }
  63. /* sorting */
  64. /* auxiliary stuff */
  65. template<typename Node,typename Compare>
  66. void sequenced_index_collate(
  67. BOOST_DEDUCED_TYPENAME Node::impl_type* x,
  68. BOOST_DEDUCED_TYPENAME Node::impl_type* y,
  69. Compare comp)
  70. {
  71. typedef typename Node::impl_type impl_type;
  72. typedef typename Node::impl_pointer impl_pointer;
  73. impl_pointer first0=x->next();
  74. impl_pointer last0=x;
  75. impl_pointer first1=y->next();
  76. impl_pointer last1=y;
  77. while(first0!=last0&&first1!=last1){
  78. if(comp(
  79. Node::from_impl(first1)->value(),Node::from_impl(first0)->value())){
  80. impl_pointer tmp=first1->next();
  81. impl_type::relink(first0,first1);
  82. first1=tmp;
  83. }
  84. else first0=first0->next();
  85. }
  86. impl_type::relink(last0,first1,last1);
  87. }
  88. /* Some versions of CGG require a bogus typename in counter_spc
  89. * inside sequenced_index_sort if the following is defined
  90. * also inside sequenced_index_sort.
  91. */
  92. BOOST_STATIC_CONSTANT(
  93. std::size_t,
  94. sequenced_index_sort_max_fill=
  95. (std::size_t)std::numeric_limits<std::size_t>::digits+1);
  96. #include <boost/multi_index/detail/ignore_wstrict_aliasing.hpp>
  97. template<typename Node,typename Compare>
  98. void sequenced_index_sort(Node* header,Compare comp)
  99. {
  100. /* Musser's mergesort, see http://www.cs.rpi.edu/~musser/gp/List/lists1.html.
  101. * The implementation is a little convoluted: in the original code
  102. * counter elements and carry are std::lists: here we do not want
  103. * to use multi_index instead, so we do things at a lower level, managing
  104. * directly the internal node representation.
  105. * Incidentally, the implementations I've seen of this algorithm (SGI,
  106. * Dinkumware, STLPort) are not exception-safe: this is. Moreover, we do not
  107. * use any dynamic storage.
  108. */
  109. if(header->next()==header->impl()||
  110. header->next()->next()==header->impl())return;
  111. typedef typename Node::impl_type impl_type;
  112. typedef typename Node::impl_pointer impl_pointer;
  113. typedef typename aligned_storage<
  114. sizeof(impl_type),
  115. alignment_of<impl_type>::value
  116. >::type carry_spc_type;
  117. carry_spc_type carry_spc;
  118. impl_type& carry=
  119. *reinterpret_cast<impl_type*>(&carry_spc);
  120. typedef typename aligned_storage<
  121. sizeof(
  122. impl_type
  123. [sequenced_index_sort_max_fill]),
  124. alignment_of<
  125. impl_type
  126. [sequenced_index_sort_max_fill]
  127. >::value
  128. >::type counter_spc_type;
  129. counter_spc_type counter_spc;
  130. impl_type* counter=
  131. reinterpret_cast<impl_type*>(&counter_spc);
  132. std::size_t fill=0;
  133. carry.prior()=carry.next()=static_cast<impl_pointer>(&carry);
  134. counter[0].prior()=counter[0].next()=static_cast<impl_pointer>(&counter[0]);
  135. BOOST_TRY{
  136. while(header->next()!=header->impl()){
  137. impl_type::relink(carry.next(),header->next());
  138. std::size_t i=0;
  139. while(i<fill&&counter[i].next()!=static_cast<impl_pointer>(&counter[i])){
  140. sequenced_index_collate<Node>(&carry,&counter[i++],comp);
  141. }
  142. impl_type::swap(
  143. static_cast<impl_pointer>(&carry),
  144. static_cast<impl_pointer>(&counter[i]));
  145. if(i==fill){
  146. ++fill;
  147. counter[fill].prior()=counter[fill].next()=
  148. static_cast<impl_pointer>(&counter[fill]);
  149. }
  150. }
  151. for(std::size_t i=1;i<fill;++i){
  152. sequenced_index_collate<Node>(&counter[i],&counter[i-1],comp);
  153. }
  154. impl_type::swap(
  155. header->impl(),static_cast<impl_pointer>(&counter[fill-1]));
  156. }
  157. BOOST_CATCH(...)
  158. {
  159. impl_type::relink(
  160. header->impl(),carry.next(),static_cast<impl_pointer>(&carry));
  161. for(std::size_t i=0;i<=fill;++i){
  162. impl_type::relink(
  163. header->impl(),counter[i].next(),
  164. static_cast<impl_pointer>(&counter[i]));
  165. }
  166. BOOST_RETHROW;
  167. }
  168. BOOST_CATCH_END
  169. }
  170. #include <boost/multi_index/detail/restore_wstrict_aliasing.hpp>
  171. } /* namespace multi_index::detail */
  172. } /* namespace multi_index */
  173. } /* namespace boost */
  174. #endif