merge.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // -*- C++ -*-
  2. // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/merge.h
  21. * @brief Parallel implementation of std::merge().
  22. * This file is a GNU parallel extension to the Standard C++ Library.
  23. */
  24. // Written by Johannes Singler.
  25. #ifndef _GLIBCXX_PARALLEL_MERGE_H
  26. #define _GLIBCXX_PARALLEL_MERGE_H 1
  27. #include <parallel/basic_iterator.h>
  28. #include <bits/stl_algo.h>
  29. namespace __gnu_parallel
  30. {
  31. /** @brief Merge routine being able to merge only the @c max_length
  32. * smallest elements.
  33. *
  34. * The @c begin iterators are advanced accordingly, they might not
  35. * reach @c end, in contrast to the usual variant.
  36. * @param begin1 Begin iterator of first sequence.
  37. * @param end1 End iterator of first sequence.
  38. * @param begin2 Begin iterator of second sequence.
  39. * @param end2 End iterator of second sequence.
  40. * @param target Target begin iterator.
  41. * @param max_length Maximum number of elements to merge.
  42. * @param comp Comparator.
  43. * @return Output end iterator. */
  44. template<typename RandomAccessIterator1, typename RandomAccessIterator2,
  45. typename OutputIterator, typename _DifferenceTp,
  46. typename Comparator>
  47. OutputIterator
  48. merge_advance_usual(RandomAccessIterator1& begin1,
  49. RandomAccessIterator1 end1,
  50. RandomAccessIterator2& begin2,
  51. RandomAccessIterator2 end2, OutputIterator target,
  52. _DifferenceTp max_length, Comparator comp)
  53. {
  54. typedef _DifferenceTp difference_type;
  55. while (begin1 != end1 && begin2 != end2 && max_length > 0)
  56. {
  57. // array1[i1] < array0[i0]
  58. if (comp(*begin2, *begin1))
  59. *target++ = *begin2++;
  60. else
  61. *target++ = *begin1++;
  62. --max_length;
  63. }
  64. if (begin1 != end1)
  65. {
  66. target = std::copy(begin1, begin1 + max_length, target);
  67. begin1 += max_length;
  68. }
  69. else
  70. {
  71. target = std::copy(begin2, begin2 + max_length, target);
  72. begin2 += max_length;
  73. }
  74. return target;
  75. }
  76. /** @brief Merge routine being able to merge only the @c max_length
  77. * smallest elements.
  78. *
  79. * The @c begin iterators are advanced accordingly, they might not
  80. * reach @c end, in contrast to the usual variant.
  81. * Specially designed code should allow the compiler to generate
  82. * conditional moves instead of branches.
  83. * @param begin1 Begin iterator of first sequence.
  84. * @param end1 End iterator of first sequence.
  85. * @param begin2 Begin iterator of second sequence.
  86. * @param end2 End iterator of second sequence.
  87. * @param target Target begin iterator.
  88. * @param max_length Maximum number of elements to merge.
  89. * @param comp Comparator.
  90. * @return Output end iterator. */
  91. template<typename RandomAccessIterator1, typename RandomAccessIterator2,
  92. typename OutputIterator, typename _DifferenceTp,
  93. typename Comparator>
  94. OutputIterator
  95. merge_advance_movc(RandomAccessIterator1& begin1,
  96. RandomAccessIterator1 end1,
  97. RandomAccessIterator2& begin2,
  98. RandomAccessIterator2 end2,
  99. OutputIterator target,
  100. _DifferenceTp max_length, Comparator comp)
  101. {
  102. typedef _DifferenceTp difference_type;
  103. typedef typename std::iterator_traits<RandomAccessIterator1>::value_type
  104. value_type1;
  105. typedef typename std::iterator_traits<RandomAccessIterator2>::value_type
  106. value_type2;
  107. #if _GLIBCXX_ASSERTIONS
  108. _GLIBCXX_PARALLEL_ASSERT(max_length >= 0);
  109. #endif
  110. while (begin1 != end1 && begin2 != end2 && max_length > 0)
  111. {
  112. RandomAccessIterator1 next1 = begin1 + 1;
  113. RandomAccessIterator2 next2 = begin2 + 1;
  114. value_type1 element1 = *begin1;
  115. value_type2 element2 = *begin2;
  116. if (comp(element2, element1))
  117. {
  118. element1 = element2;
  119. begin2 = next2;
  120. }
  121. else
  122. begin1 = next1;
  123. *target = element1;
  124. ++target;
  125. --max_length;
  126. }
  127. if (begin1 != end1)
  128. {
  129. target = std::copy(begin1, begin1 + max_length, target);
  130. begin1 += max_length;
  131. }
  132. else
  133. {
  134. target = std::copy(begin2, begin2 + max_length, target);
  135. begin2 += max_length;
  136. }
  137. return target;
  138. }
  139. /** @brief Merge routine being able to merge only the @c max_length
  140. * smallest elements.
  141. *
  142. * The @c begin iterators are advanced accordingly, they might not
  143. * reach @c end, in contrast to the usual variant.
  144. * Static switch on whether to use the conditional-move variant.
  145. * @param begin1 Begin iterator of first sequence.
  146. * @param end1 End iterator of first sequence.
  147. * @param begin2 Begin iterator of second sequence.
  148. * @param end2 End iterator of second sequence.
  149. * @param target Target begin iterator.
  150. * @param max_length Maximum number of elements to merge.
  151. * @param comp Comparator.
  152. * @return Output end iterator. */
  153. template<typename RandomAccessIterator1, typename RandomAccessIterator2,
  154. typename OutputIterator, typename _DifferenceTp,
  155. typename Comparator>
  156. inline OutputIterator
  157. merge_advance(RandomAccessIterator1& begin1, RandomAccessIterator1 end1,
  158. RandomAccessIterator2& begin2, RandomAccessIterator2 end2,
  159. OutputIterator target, _DifferenceTp max_length,
  160. Comparator comp)
  161. {
  162. _GLIBCXX_CALL(max_length)
  163. return merge_advance_movc(begin1, end1, begin2, end2, target,
  164. max_length, comp);
  165. }
  166. /** @brief Merge routine fallback to sequential in case the
  167. iterators of the two input sequences are of different type.
  168. * @param begin1 Begin iterator of first sequence.
  169. * @param end1 End iterator of first sequence.
  170. * @param begin2 Begin iterator of second sequence.
  171. * @param end2 End iterator of second sequence.
  172. * @param target Target begin iterator.
  173. * @param max_length Maximum number of elements to merge.
  174. * @param comp Comparator.
  175. * @return Output end iterator. */
  176. template<typename RandomAccessIterator1, typename RandomAccessIterator2,
  177. typename RandomAccessIterator3, typename Comparator>
  178. inline RandomAccessIterator3
  179. parallel_merge_advance(RandomAccessIterator1& begin1,
  180. RandomAccessIterator1 end1,
  181. RandomAccessIterator2& begin2,
  182. // different iterators, parallel implementation
  183. // not available
  184. RandomAccessIterator2 end2,
  185. RandomAccessIterator3 target, typename
  186. std::iterator_traits<RandomAccessIterator1>::
  187. difference_type max_length, Comparator comp)
  188. { return merge_advance(begin1, end1, begin2, end2, target,
  189. max_length, comp); }
  190. /** @brief Parallel merge routine being able to merge only the @c
  191. * max_length smallest elements.
  192. *
  193. * The @c begin iterators are advanced accordingly, they might not
  194. * reach @c end, in contrast to the usual variant.
  195. * The functionality is projected onto parallel_multiway_merge.
  196. * @param begin1 Begin iterator of first sequence.
  197. * @param end1 End iterator of first sequence.
  198. * @param begin2 Begin iterator of second sequence.
  199. * @param end2 End iterator of second sequence.
  200. * @param target Target begin iterator.
  201. * @param max_length Maximum number of elements to merge.
  202. * @param comp Comparator.
  203. * @return Output end iterator.
  204. */
  205. template<typename RandomAccessIterator1, typename RandomAccessIterator3,
  206. typename Comparator>
  207. inline RandomAccessIterator3
  208. parallel_merge_advance(RandomAccessIterator1& begin1,
  209. RandomAccessIterator1 end1,
  210. RandomAccessIterator1& begin2,
  211. RandomAccessIterator1 end2,
  212. RandomAccessIterator3 target, typename
  213. std::iterator_traits<RandomAccessIterator1>::
  214. difference_type max_length, Comparator comp)
  215. {
  216. typedef typename
  217. std::iterator_traits<RandomAccessIterator1>::value_type value_type;
  218. typedef typename std::iterator_traits<RandomAccessIterator1>::
  219. difference_type difference_type1 /* == difference_type2 */;
  220. typedef typename std::iterator_traits<RandomAccessIterator3>::
  221. difference_type difference_type3;
  222. typedef typename std::pair<RandomAccessIterator1, RandomAccessIterator1>
  223. iterator_pair;
  224. iterator_pair
  225. seqs[2] = { std::make_pair(begin1, end1),
  226. std::make_pair(begin2, end2) };
  227. RandomAccessIterator3
  228. target_end = parallel_multiway_merge
  229. < /* stable = */ true, /* sentinels = */ false>(
  230. seqs, seqs + 2, target,
  231. multiway_merge_exact_splitting
  232. < /* stable = */ true, iterator_pair*,
  233. Comparator, difference_type1>,
  234. max_length, comp, omp_get_max_threads());
  235. return target_end;
  236. }
  237. } //namespace __gnu_parallel
  238. #endif /* _GLIBCXX_PARALLEL_MERGE_H */