1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
namespace sort {
template <typename Itr>
using type = typename std::iterator_traits<Itr>::value_type;
template <typename T>
using viterator = typename std::vector<T>::iterator;
template <typename Itr, typename Comp = std::less<type<Itr>>>
void merge(Itr beg, Itr mid, Itr end,
viterator<type<Itr>> vbeg, viterator<type<Itr>> vend, Comp comp = {}) {
auto beg1 = beg;
auto beg2 = mid;
while (beg1 != mid && beg2 != end)
*vbeg++ = std::move(comp(*beg1, *beg2)? *beg1++: *beg2++);
while (beg1 != mid)
*vbeg++ = std::move(*beg1++);
while (beg2 != end)
*vbeg++ = std::move(*beg2++);
while (end != beg) {
*--end = std::move(*--vend);
}
}
template <typename Itr, typename Comp = std::less<type<Itr>>>
void merge_sort(Itr beg, Itr end,
viterator<type<Itr>> vbeg, viterator<type<Itr>> vend, Comp comp = {}) {
if (1< std::distance(beg, end)) {
auto mid = beg;
std::advance(mid, std::distance(beg, end)/2);
auto vmid = vbeg;
std::advance(vmid, std::distance(vbeg, vend)/2);
merge_sort(beg, mid, vbeg, vmid, comp);
merge_sort(mid, end, vmid, vend, comp);
merge(beg, mid, end, vbeg, vend, comp);
// if i prefixed sort:: to merge, it would work. but why?
// i don't include algorithm header. i compile it at Xcode
}
}
template <typename Itr, typename Comp = std::less<type<Itr>>>
void merge_sort(Itr beg, Itr end, Comp comp = {}) {
std::vector<type<Itr>> v(end - beg);
merge_sort(beg, end, v.begin(), v.end(), comp);
}
}
|