merge_four.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. //----------------------------------------------------------------------------
  2. /// @file merge_four.hpp
  3. /// @brief This file have the functions for to merge 4 buffers
  4. ///
  5. /// @author Copyright (c) 2016 Francisco José Tapia ([email protected] )\n
  6. /// Distributed under the Boost Software License, Version 1.0.\n
  7. /// ( See accompanying file LICENSE_1_0.txt or copy at
  8. /// http://www.boost.org/LICENSE_1_0.txt )
  9. /// @version 0.1
  10. ///
  11. /// @remarks
  12. //-----------------------------------------------------------------------------
  13. #ifndef __BOOST_SORT_PARALLEL_DETAIL_UTIL_MERGE_FOUR_HPP
  14. #define __BOOST_SORT_PARALLEL_DETAIL_UTIL_MERGE_FOUR_HPP
  15. #include <ciso646>
  16. #include <functional>
  17. #include <iterator>
  18. #include <memory>
  19. #include <vector>
  20. #include <boost/sort/common/util/traits.hpp>
  21. #include <boost/sort/common/range.hpp>
  22. namespace boost
  23. {
  24. namespace sort
  25. {
  26. namespace common
  27. {
  28. //
  29. //############################################################################
  30. // ##
  31. // F U S I O N O F ##
  32. // ##
  33. // F O U R E L E M E N T S R A N G E ##
  34. // ##
  35. //############################################################################
  36. //
  37. //-----------------------------------------------------------------------------
  38. // function : less_range
  39. /// @brief Compare the elements pointed by it1 and it2, and if they
  40. /// are equals, compare their position, doing a stable comparison
  41. ///
  42. /// @param it1 : iterator to the first element
  43. /// @param pos1 : position of the object pointed by it1
  44. /// @param it2 : iterator to the second element
  45. /// @param pos2 : position of the element pointed by it2
  46. /// @param comp : comparison object
  47. /// @return result of the comparison
  48. //-----------------------------------------------------------------------------
  49. template<class Iter_t, class Compare = typename util::compare_iter<Iter_t> >
  50. inline bool less_range(Iter_t it1, uint32_t pos1, Iter_t it2, uint32_t pos2,
  51. Compare comp = Compare())
  52. {
  53. return (comp(*it1, *it2)) ? true :
  54. (pos2 < pos1) ? false : not (comp(*it2, *it1));
  55. };
  56. //-----------------------------------------------------------------------------
  57. // function : full_merge4
  58. /// @brief Merge four ranges
  59. ///
  60. /// @param dest: range where move the elements merged. Their size must be
  61. /// greater or equal than the sum of the sizes of the ranges
  62. /// in vrange_input
  63. /// @param vrange_input : array of ranges to merge
  64. /// @param nrange_input : number of ranges in vrange_input
  65. /// @param comp : comparison object
  66. /// @return range with all the elements moved with the size adjusted
  67. //-----------------------------------------------------------------------------
  68. template<class Iter1_t, class Iter2_t, class Compare>
  69. range<Iter1_t> full_merge4(const range<Iter1_t> &rdest,
  70. range<Iter2_t> vrange_input[4],
  71. uint32_t nrange_input, Compare comp)
  72. {
  73. using std::swap;
  74. typedef range<Iter1_t> range1_t;
  75. typedef util::value_iter<Iter1_t> type1;
  76. typedef util::value_iter<Iter2_t> type2;
  77. static_assert (std::is_same< type1, type2 >::value,
  78. "Incompatible iterators\n");
  79. size_t ndest = 0;
  80. uint32_t i = 0;
  81. while (i < nrange_input)
  82. {
  83. if (vrange_input[i].size() != 0)
  84. {
  85. ndest += vrange_input[i++].size();
  86. }
  87. else
  88. {
  89. for (uint32_t k = i + 1; k < nrange_input; ++k)
  90. {
  91. vrange_input[k - 1] = vrange_input[k];
  92. };
  93. --nrange_input;
  94. };
  95. };
  96. if (nrange_input == 0) return range1_t(rdest.first, rdest.first);
  97. if (nrange_input == 1) return move_forward(rdest, vrange_input[0]);
  98. if (nrange_input == 2)
  99. {
  100. return merge(rdest, vrange_input[0], vrange_input[1], comp);
  101. };
  102. //------------------------------------------------------------------------
  103. // Initial sort
  104. //------------------------------------------------------------------------
  105. uint32_t pos[4] =
  106. { 0, 1, 2, 3 }, npos = nrange_input;
  107. //-----------------------------------------------------------------------
  108. // thanks to Steven Ross by their suggestion about the optimal
  109. // sorting networks
  110. //-----------------------------------------------------------------------
  111. if (less_range(vrange_input[pos[1]].first, pos[1],
  112. vrange_input[pos[0]].first, pos[0], comp))
  113. {
  114. swap(pos[0], pos[1]);
  115. };
  116. if (npos == 4 and less_range(vrange_input[pos[3]].first, pos[3],
  117. vrange_input[pos[2]].first, pos[2], comp))
  118. {
  119. swap(pos[3], pos[2]);
  120. };
  121. if (less_range (vrange_input[pos[2]].first, pos[2],
  122. vrange_input[pos[0]].first, pos[0], comp))
  123. {
  124. swap(pos[0], pos[2]);
  125. };
  126. if (npos == 4
  127. and less_range (vrange_input[pos[3]].first, pos[3],
  128. vrange_input[pos[1]].first, pos[1], comp))
  129. {
  130. swap(pos[1], pos[3]);
  131. };
  132. if (less_range (vrange_input[pos[2]].first, pos[2],
  133. vrange_input[pos[1]].first, pos[1], comp))
  134. {
  135. swap(pos[1], pos[2]);
  136. };
  137. Iter1_t it_dest = rdest.first;
  138. while (npos > 2)
  139. {
  140. *(it_dest++) = std::move(*(vrange_input[pos[0]].first++));
  141. if (vrange_input[pos[0]].size() == 0)
  142. {
  143. pos[0] = pos[1];
  144. pos[1] = pos[2];
  145. pos[2] = pos[3];
  146. --npos;
  147. }
  148. else
  149. {
  150. if (less_range(vrange_input[pos[1]].first, pos[1],
  151. vrange_input[pos[0]].first, pos[0], comp))
  152. {
  153. swap(pos[0], pos[1]);
  154. if (less_range(vrange_input[pos[2]].first, pos[2],
  155. vrange_input[pos[1]].first, pos[1], comp))
  156. {
  157. swap(pos[1], pos[2]);
  158. if (npos == 4
  159. and less_range(vrange_input[pos[3]].first,
  160. pos[3],
  161. vrange_input[pos[2]].first,
  162. pos[2], comp))
  163. {
  164. swap(pos[2], pos[3]);
  165. };
  166. };
  167. };
  168. };
  169. };
  170. range1_t raux1(rdest.first, it_dest), raux2(it_dest, rdest.last);
  171. if (pos[0] < pos[1])
  172. {
  173. return concat(raux1,merge(raux2, vrange_input[pos[0]],
  174. vrange_input[pos[1]], comp));
  175. }
  176. else
  177. {
  178. return concat(raux1, merge (raux2, vrange_input[pos[1]],
  179. vrange_input[pos[0]], comp));
  180. };
  181. };
  182. //-----------------------------------------------------------------------------
  183. // function : uninit_full_merge4
  184. /// @brief Merge four ranges and put the result in uninitialized memory
  185. ///
  186. /// @param dest: range where create and move the elements merged. Their
  187. /// size must be greater or equal than the sum of the sizes
  188. /// of the ranges in the array R
  189. /// @param vrange_input : array of ranges to merge
  190. /// @param nrange_input : number of ranges in vrange_input
  191. /// @param comp : comparison object
  192. /// @return range with all the elements move with the size adjusted
  193. //-----------------------------------------------------------------------------
  194. template<class Value_t, class Iter_t, class Compare>
  195. range<Value_t *> uninit_full_merge4(const range<Value_t *> &dest,
  196. range<Iter_t> vrange_input[4],
  197. uint32_t nrange_input, Compare comp)
  198. {
  199. using std::swap;
  200. typedef util::value_iter<Iter_t> type1;
  201. static_assert (std::is_same< type1, Value_t >::value,
  202. "Incompatible iterators\n");
  203. size_t ndest = 0;
  204. uint32_t i = 0;
  205. while (i < nrange_input)
  206. {
  207. if (vrange_input[i].size() != 0)
  208. {
  209. ndest += vrange_input[i++].size();
  210. }
  211. else
  212. {
  213. for (uint32_t k = i + 1; k < nrange_input; ++k)
  214. {
  215. vrange_input[k - 1] = vrange_input[k];
  216. };
  217. --nrange_input;
  218. };
  219. };
  220. if (nrange_input == 0) return range<Value_t *>(dest.first, dest.first);
  221. if (nrange_input == 1) return move_construct(dest, vrange_input[0]);
  222. if (nrange_input == 2)
  223. {
  224. return merge_construct(dest, vrange_input[0], vrange_input[1], comp);
  225. };
  226. //------------------------------------------------------------------------
  227. // Initial sort
  228. //------------------------------------------------------------------------
  229. uint32_t pos[4] = { 0, 1, 2, 3 }, npos = nrange_input;
  230. //-----------------------------------------------------------------------
  231. // thanks to Steven Ross by their suggestion about the optimal
  232. // sorting networks
  233. //-----------------------------------------------------------------------
  234. if (less_range(vrange_input[pos[1]].first, pos[1],
  235. vrange_input[pos[0]].first, pos[0], comp))
  236. {
  237. swap(pos[0], pos[1]);
  238. };
  239. if (npos == 4 and less_range(vrange_input[pos[3]].first, pos[3],
  240. vrange_input[pos[2]].first, pos[2], comp))
  241. {
  242. swap(pos[3], pos[2]);
  243. };
  244. if (less_range(vrange_input[pos[2]].first, pos[2],
  245. vrange_input[pos[0]].first, pos[0], comp))
  246. {
  247. swap(pos[0], pos[2]);
  248. };
  249. if (npos == 4 and less_range(vrange_input[pos[3]].first, pos[3],
  250. vrange_input[pos[1]].first, pos[1], comp))
  251. {
  252. swap(pos[1], pos[3]);
  253. };
  254. if (less_range(vrange_input[pos[2]].first, pos[2],
  255. vrange_input[pos[1]].first, pos[1], comp))
  256. {
  257. swap(pos[1], pos[2]);
  258. };
  259. Value_t *it_dest = dest.first;
  260. while (npos > 2)
  261. {
  262. util::construct_object(&(*(it_dest++)),
  263. std::move(*(vrange_input[pos[0]].first++)));
  264. if (vrange_input[pos[0]].size() == 0)
  265. {
  266. pos[0] = pos[1];
  267. pos[1] = pos[2];
  268. pos[2] = pos[3];
  269. --npos;
  270. }
  271. else
  272. {
  273. if (less_range (vrange_input[pos[1]].first, pos[1],
  274. vrange_input[pos[0]].first, pos[0], comp))
  275. {
  276. swap(pos[0], pos[1]);
  277. if (less_range (vrange_input[pos[2]].first, pos[2],
  278. vrange_input[pos[1]].first, pos[1], comp))
  279. {
  280. swap(pos[1], pos[2]);
  281. if (npos == 4 and less_range(vrange_input[pos[3]].first,
  282. pos[3],
  283. vrange_input[pos[2]].first,
  284. pos[2], comp))
  285. {
  286. swap(pos[2], pos[3]);
  287. };
  288. };
  289. };
  290. };
  291. }; // end while (npos > 2)
  292. range<Value_t *> raux1(dest.first, it_dest), raux2(it_dest, dest.last);
  293. if (pos[0] < pos[1])
  294. {
  295. return concat(raux1,
  296. merge_construct(raux2, vrange_input[pos[0]],
  297. vrange_input[pos[1]], comp));
  298. }
  299. else
  300. {
  301. return concat(raux1,
  302. merge_construct(raux2, vrange_input[pos[1]],
  303. vrange_input[pos[0]], comp));
  304. };
  305. };
  306. //****************************************************************************
  307. };// End namespace common
  308. };// End namespace sort
  309. };// End namespace boost
  310. //****************************************************************************
  311. //
  312. #endif