Skip to content

Make dmsort work with bidirectional iterators #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions drop_merge_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ namespace detail {
// with-trivial-copies version
template<typename Iter, typename Comp>
void dmsort(Iter begin, Iter end, Comp comp, std::true_type) {
size_t size = end - begin;

if (size < 2)
if (begin == end || std::next(begin) == end)
return;

using Value = typename std::iterator_traits<Iter>::value_type;
Expand All @@ -45,11 +43,12 @@ namespace detail {
constexpr size_t recency = 8;

while (read != end) {
if (begin != write && comp(*read, *(write - 1))) {
if (begin != write && comp(*read, *std::prev(write))) {

if (double_comparison && num_dropped_in_row == 0 && write > begin+1 && !comp(*read, *(write-2))) {
dropped.push_back(*(write-1));
*(write-1) = *read;
if (double_comparison && num_dropped_in_row == 0 && write != std::next(begin)
&& !comp(*read, *std::prev(write, 2))) {
dropped.push_back(*std::prev(write));
*std::prev(write) = *read;
++read;
continue;
}
Expand All @@ -62,7 +61,7 @@ namespace detail {
for (int i = 0; i < num_dropped_in_row; ++i) {
dropped.pop_back();
}
read -= num_dropped_in_row;
std::advance(read, -num_dropped_in_row);

--write;
dropped.push_back(*write);
Expand All @@ -86,7 +85,7 @@ namespace detail {
while (!dropped.empty()) {
auto & last_dropped = dropped.back();

while (begin != write && comp(last_dropped, *(write - 1))) {
while (begin != write && comp(last_dropped, *std::prev(write))) {
--back;
--write;
*back = std::move(*write);
Expand All @@ -100,9 +99,7 @@ namespace detail {
// move-only version
template<typename Iter, typename Comp>
void dmsort(Iter begin, Iter end, Comp comp, std::false_type) {
size_t size = end - begin;

if (size < 2)
if (begin == end || std::next(begin) == end)
return;

using Value = typename std::iterator_traits<Iter>::value_type;
Expand All @@ -115,11 +112,12 @@ namespace detail {
constexpr size_t recency = 8;

while (read != end) {
if (begin != write && comp(*read, *(write - 1))) {
if (begin != write && comp(*read, *std::prev(write))) {

if (double_comparison && num_dropped_in_row == 0 && write > begin+1 && !comp(*read, *(write-2))) {
dropped.push_back(std::move(*(write-1)));
*(write-1) = std::move(*read);
if (double_comparison && num_dropped_in_row == 0 && write != std::next(begin)
&& !comp(*read, *std::prev(write, 2))) {
dropped.push_back(std::move(*std::prev(write)));
*std::prev(write) = std::move(*read);
++read;
continue;
}
Expand Down Expand Up @@ -157,7 +155,7 @@ namespace detail {
while (!dropped.empty()) {
auto & last_dropped = dropped.back();

while (begin != write && comp(last_dropped, *(write - 1))) {
while (begin != write && comp(last_dropped, *std::prev(write))) {
--back;
--write;
*back = std::move(*write);
Expand Down
28 changes: 26 additions & 2 deletions tests.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <vector>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <memory>
#include <random>
#include <vector>
#include "drop_merge_sort.hpp"

/*
Expand Down Expand Up @@ -59,6 +62,24 @@ void test_counts(){
PRINT(moveass);
}

/*
Check that dmsort can sort bidirectional iterators
*/

void test_list(){
std::vector<int> vec;
for(int i = 0; i < 500000; ++i)
vec.push_back(i);

// Create a shuffled list
std::minstd_rand engine(123456);
std::shuffle(vec.begin(), vec.end(), engine);
std::list<int> li(vec.begin(), vec.end());

dmsort(li.begin(), li.end());
std::cout << std::is_sorted(li.begin(), li.end()) << std::endl;
}

/*
Check that the move-only version actually works with move-only types
*/
Expand Down Expand Up @@ -106,7 +127,10 @@ void test_non_default_constructible(){
}

int main(){
std::cout << std::boolalpha;

test_counts();
test_list();
test_noncopyable();
test_non_default_constructible();
}