merge_vector.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //----------------------------------------------------------------------------
  2. /// @file merge_vector.hpp
  3. /// @brief In this file have the functions for to do a stable merge of
  4. // ranges, in a vector
  5. ///
  6. /// @author Copyright (c) 2016 Francisco Jose Tapia (fjtapia@gmail.com )\n
  7. /// Distributed under the Boost Software License, Version 1.0.\n
  8. /// ( See accompanying file LICENSE_1_0.txt or copy at
  9. /// http://www.boost.org/LICENSE_1_0.txt )
  10. /// @version 0.1
  11. ///
  12. /// @remarks
  13. //-----------------------------------------------------------------------------
  14. #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_MERGE_VECTOR_HPP
  15. #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_MERGE_VECTOR_HPP
  16. #include <boost/sort/common/merge_four.hpp>
  17. #include <functional>
  18. #include <iterator>
  19. #include <memory>
  20. #include <type_traits>
  21. #include <vector>
  22. namespace boost
  23. {
  24. namespace sort
  25. {
  26. namespace common
  27. {
  28. //############################################################################
  29. // ##
  30. // F U S I O N O F ##
  31. // ##
  32. // A V E C T O R O F R A N G E S ##
  33. // ##
  34. //############################################################################
  35. //
  36. //-----------------------------------------------------------------------------
  37. // function : merge_level4
  38. /// @brief merge the ranges in the vector v_input with the full_merge4 function.
  39. /// The v_output vector is used as auxiliary memory in the internal
  40. /// process. The final results is in the dest range.
  41. /// All the ranges of v_output are inside the range dest
  42. /// @param dest : range where move the elements merged
  43. /// @param v_input : vector of ranges to merge
  44. /// @param v_output : vector of ranges obtained
  45. /// @param comp : comparison object
  46. /// @return range with all the elements moved
  47. //-----------------------------------------------------------------------------
  48. template<class Iter1_t, class Iter2_t, class Compare>
  49. void merge_level4(range<Iter1_t> dest, std::vector<range<Iter2_t> > &v_input,
  50. std::vector<range<Iter1_t> > &v_output, Compare comp)
  51. {
  52. typedef range<Iter1_t> range1_t;
  53. typedef util::value_iter<Iter1_t> type1;
  54. typedef util::value_iter<Iter2_t> type2;
  55. static_assert (std::is_same< type1, type2 >::value,
  56. "Incompatible iterators\n");
  57. v_output.clear();
  58. if (v_input.size() == 0) return;
  59. if (v_input.size() == 1)
  60. {
  61. v_output.emplace_back(move_forward(dest, v_input[0]));
  62. return;
  63. };
  64. uint32_t nrange = v_input.size();
  65. uint32_t pos_ini = 0;
  66. while (pos_ini < v_input.size())
  67. {
  68. uint32_t nmerge = (nrange + 3) >> 2;
  69. uint32_t nelem = (nrange + nmerge - 1) / nmerge;
  70. range1_t rz = full_merge4(dest, &v_input[pos_ini], nelem, comp);
  71. v_output.emplace_back(rz);
  72. dest.first = rz.last;
  73. pos_ini += nelem;
  74. nrange -= nelem;
  75. };
  76. return;
  77. };
  78. //
  79. //-----------------------------------------------------------------------------
  80. // function : uninit_merge_level4
  81. /// @brief merge the ranges moving the objects and constructing them in
  82. /// uninitialized memory, in the vector v_input
  83. /// using full_merge4. The v_output vector is used as auxiliary memory
  84. /// in the internal process. The final results is in the dest range.
  85. /// All the ranges of v_output are inside the range dest
  86. ///
  87. /// @param dest : range where move the elements merged
  88. /// @param v_input : vector of ranges to merge
  89. /// @param v_output : vector of ranges obtained
  90. /// @param comp : comparison object
  91. /// @return range with all the elements moved and constructed
  92. //-----------------------------------------------------------------------------
  93. template<class Value_t, class Iter_t, class Compare>
  94. void uninit_merge_level4(range<Value_t *> dest,
  95. std::vector<range<Iter_t> > &v_input,
  96. std::vector<range<Value_t *> > &v_output, Compare comp)
  97. {
  98. typedef range<Value_t *> range1_t;
  99. typedef util::value_iter<Iter_t> type1;
  100. static_assert (std::is_same< type1, Value_t >::value,
  101. "Incompatible iterators\n");
  102. v_output.clear();
  103. if (v_input.size() == 0) return;
  104. if (v_input.size() == 1)
  105. {
  106. v_output.emplace_back(move_construct(dest, v_input[0]));
  107. return;
  108. };
  109. uint32_t nrange = v_input.size();
  110. uint32_t pos_ini = 0;
  111. while (pos_ini < v_input.size())
  112. {
  113. uint32_t nmerge = (nrange + 3) >> 2;
  114. uint32_t nelem = (nrange + nmerge - 1) / nmerge;
  115. range1_t rz = uninit_full_merge4(dest, &v_input[pos_ini], nelem, comp);
  116. v_output.emplace_back(rz);
  117. dest.first = rz.last;
  118. pos_ini += nelem;
  119. nrange -= nelem;
  120. };
  121. return;
  122. };
  123. //
  124. //-----------------------------------------------------------------------------
  125. // function : merge_vector4
  126. /// @brief merge the ranges in the vector v_input using the merge_level4
  127. /// function. The v_output vector is used as auxiliary memory in the
  128. /// internal process
  129. /// The final results is in the range_output range.
  130. /// All the ranges of v_output are inside the range range_output
  131. /// All the ranges of v_input are inside the range range_input
  132. /// @param range_input : range including all the ranges of v_input
  133. /// @param ange_output : range including all the elements of v_output
  134. /// @param v_input : vector of ranges to merge
  135. /// @param v_output : vector of ranges obtained
  136. /// @param comp : comparison object
  137. /// @return range with all the elements moved
  138. //-----------------------------------------------------------------------------
  139. template<class Iter1_t, class Iter2_t, class Compare>
  140. range<Iter2_t> merge_vector4(range<Iter1_t> range_input,
  141. range<Iter2_t> range_output,
  142. std::vector<range<Iter1_t> > &v_input,
  143. std::vector<range<Iter2_t> > &v_output,
  144. Compare comp)
  145. {
  146. typedef range<Iter2_t> range2_t;
  147. typedef util::value_iter<Iter1_t> type1;
  148. typedef util::value_iter<Iter2_t> type2;
  149. static_assert (std::is_same< type1, type2 >::value,
  150. "Incompatible iterators\n");
  151. v_output.clear();
  152. if (v_input.size() == 0)
  153. {
  154. return range2_t(range_output.first, range_output.first);
  155. };
  156. if (v_input.size() == 1)
  157. {
  158. return move_forward(range_output, v_input[0]);
  159. };
  160. bool sw = false;
  161. uint32_t nrange = v_input.size();
  162. while (nrange > 1)
  163. {
  164. if (sw)
  165. {
  166. merge_level4(range_input, v_output, v_input, comp);
  167. sw = false;
  168. nrange = v_input.size();
  169. }
  170. else
  171. {
  172. merge_level4(range_output, v_input, v_output, comp);
  173. sw = true;
  174. nrange = v_output.size();
  175. };
  176. };
  177. return (sw) ? v_output[0] : move_forward(range_output, v_input[0]);
  178. };
  179. //****************************************************************************
  180. };// End namespace common
  181. };// End namespace sort
  182. };// End namespace boost
  183. //****************************************************************************
  184. //
  185. #endif