merge_vector.hpp 7.3 KB

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