Skip to content

Commit ebeabae

Browse files
committed
Temporary ranges support for timsort
This notably allows timsort to accept an std::span.
1 parent 464a7bc commit ebeabae

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

include/gfx/timsort.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ template <
731731
typename Projection = std::identity
732732
>
733733
requires std::sortable<std::ranges::iterator_t<Range>, Compare, Projection>
734-
void timsort(Range &range, Compare comp={}, Projection proj={}) {
734+
void timsort(Range &&range, Compare comp={}, Projection proj={}) {
735735
gfx::timsort(std::begin(range), std::end(range), comp, proj);
736736
}
737737

tests/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ add_executable(cxx_17_tests
100100
configure_tests(cxx_17_tests)
101101
target_compile_features(cxx_17_tests PRIVATE cxx_std_17)
102102

103+
# Tests requiring C++20 support
104+
add_executable(cxx_20_tests
105+
cxx_20_tests.cpp
106+
)
107+
configure_tests(cxx_20_tests)
108+
target_compile_features(cxx_20_tests PRIVATE cxx_std_20)
109+
103110
# Windows-specific tests
104111
if (WIN32)
105112
add_executable(windows_tests
@@ -116,6 +123,7 @@ string(RANDOM LENGTH 5 ALPHABET 123456789 RNG_SEED)
116123
catch_discover_tests(cxx_98_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
117124
catch_discover_tests(cxx_11_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
118125
catch_discover_tests(cxx_17_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
126+
catch_discover_tests(cxx_20_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
119127
if (WIN32)
120128
catch_discover_tests(windows_tests EXTRA_ARGS --rng-seed ${RNG_SEED})
121129
endif()

tests/cxx_20_tests.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2024 Morwenn.
3+
* SPDX-License-Identifier: MIT
4+
*/
5+
#include <algorithm>
6+
#include <numeric>
7+
#include <random>
8+
#include <span>
9+
#include <vector>
10+
#include <catch2/catch_test_macros.hpp>
11+
#include <gfx/timsort.hpp>
12+
#include "test_helpers.hpp"
13+
14+
TEST_CASE( "support for temporary types" ) {
15+
SECTION( "timsort over std::span" ) {
16+
std::vector<int> vec(50);
17+
std::iota(vec.begin(), vec.end(), -25);
18+
test_helpers::shuffle(vec);
19+
20+
gfx::timsort(std::span(vec));
21+
CHECK(std::ranges::is_sorted(vec));
22+
}
23+
}

0 commit comments

Comments
 (0)