Skip to content

Instantly share code, notes, and snippets.

@Morwenn
Created May 1, 2019 14:28
Show Gist options
  • Save Morwenn/b823b7a2805d4314e0d817b411f96725 to your computer and use it in GitHub Desktop.
Save Morwenn/b823b7a2805d4314e0d817b411f96725 to your computer and use it in GitHub Desktop.
Overly complicated merge function
template<typename InputIterator1, typename InputIterator2,
typename OutputIterator, typename Size,
typename Compare, typename Projection>
auto half_inplace_merge5(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Size,
Compare compare, Projection projection)
-> void
{
using utility::iter_move;
auto&& comp = utility::as_function(compare);
auto&& proj = utility::as_function(projection);
if (first1 == last1) {
return;
}
if (first2 == last2) {
detail::move(first1, last1, result);
return;
}
if (comp(proj(*first2), proj(*first1))) {
merge2:
auto begin = first2;
++first2;
if (first2 == last2) {
*result = iter_move(begin);
detail::move(first1, last1, ++result);
return;
}
if (comp(proj(*first2), proj(*first1))) {
do {
++first2;
if (first2 == last2) {
result = detail::move(begin, first2, result);
detail::move(first1, last1, result);
return;
}
} while (comp(proj(*first2), proj(*first1)));
result = detail::move(begin, first2, result);
goto merge1;
} else {
*result = iter_move(begin);
++result;
goto merge1;
}
} else {
merge1:
auto begin = first1;
++first1;
if (first1 == last1) {
*result = iter_move(begin);
return;
}
if (not comp(proj(*first2), proj(*first1))) {
do {
++first1;
if (first1 == last1) {
detail::move(begin, first1, result);
return;
}
} while (not comp(proj(*first2), proj(*first1)));
result = detail::move(begin, first1, result);
goto merge2;
} else {
*result = iter_move(begin);
++result;
goto merge2;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment