Skip to content

Commit

Permalink
Temporary ranges support for timsort
Browse files Browse the repository at this point in the history
This notably allows timsort to accept an std::span.
  • Loading branch information
Morwenn committed Jan 17, 2024
1 parent 464a7bc commit ebeabae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/gfx/timsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ template <
typename Projection = std::identity
>
requires std::sortable<std::ranges::iterator_t<Range>, Compare, Projection>
void timsort(Range &range, Compare comp={}, Projection proj={}) {
void timsort(Range &&range, Compare comp={}, Projection proj={}) {
gfx::timsort(std::begin(range), std::end(range), comp, proj);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ add_executable(cxx_17_tests
configure_tests(cxx_17_tests)
target_compile_features(cxx_17_tests PRIVATE cxx_std_17)

# Tests requiring C++20 support
add_executable(cxx_20_tests
cxx_20_tests.cpp
)
configure_tests(cxx_20_tests)
target_compile_features(cxx_20_tests PRIVATE cxx_std_20)

# Windows-specific tests
if (WIN32)
add_executable(windows_tests
Expand All @@ -116,6 +123,7 @@ string(RANDOM LENGTH 5 ALPHABET 123456789 RNG_SEED)
catch_discover_tests(cxx_98_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
catch_discover_tests(cxx_11_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
catch_discover_tests(cxx_17_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
catch_discover_tests(cxx_20_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
if (WIN32)
catch_discover_tests(windows_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
endif()
23 changes: 23 additions & 0 deletions tests/cxx_20_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 Morwenn.
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <numeric>
#include <random>
#include <span>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <gfx/timsort.hpp>
#include "test_helpers.hpp"

TEST_CASE( "support for temporary types" ) {
SECTION( "timsort over std::span" ) {
std::vector<int> vec(50);
std::iota(vec.begin(), vec.end(), -25);
test_helpers::shuffle(vec);

gfx::timsort(std::span(vec));
CHECK(std::ranges::is_sorted(vec));
}
}

0 comments on commit ebeabae

Please sign in to comment.