Skip to content

Commit

Permalink
constexpr selection_sorter (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Jul 10, 2022
1 parent 403fdda commit 7afc95a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
14 changes: 8 additions & 6 deletions include/cpp-sort/detail/min_element.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2018 Morwenn
* Copyright (c) 2015-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_DETAIL_MIN_ELEMENT_H_
Expand All @@ -17,8 +17,8 @@ namespace cppsort
namespace detail
{
template<typename ForwardIterator, typename Compare, typename Projection>
auto unchecked_min_element(ForwardIterator first, ForwardIterator last,
Compare compare, Projection projection)
constexpr auto unchecked_min_element(ForwardIterator first, ForwardIterator last,
Compare compare, Projection projection)
-> ForwardIterator
{
// Same algorithm as min_element, but assumes that the
Expand All @@ -38,11 +38,13 @@ namespace detail
}

template<typename ForwardIterator, typename Compare, typename Projection>
auto min_element(ForwardIterator first, ForwardIterator last,
Compare compare, Projection projection)
constexpr auto min_element(ForwardIterator first, ForwardIterator last,
Compare compare, Projection projection)
-> ForwardIterator
{
if (first == last) return last;
if (first == last) {
return last;
}
return unchecked_min_element(std::move(first), std::move(last),
std::move(compare), std::move(projection));
}
Expand Down
8 changes: 4 additions & 4 deletions include/cpp-sort/detail/selection_sort.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2017 Morwenn
* Copyright (c) 2015-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_DETAIL_SELECTION_SORT_H_
Expand All @@ -16,11 +16,11 @@ namespace cppsort
namespace detail
{
template<typename ForwardIterator, typename Compare, typename Projection>
auto selection_sort(ForwardIterator first, ForwardIterator last,
Compare compare, Projection projection)
constexpr auto selection_sort(ForwardIterator first, ForwardIterator last,
Compare compare, Projection projection)
-> void
{
for (ForwardIterator it = first ; it != last ; ++it) {
for (auto it = first; it != last; ++it) {
using utility::iter_swap;
iter_swap(it, unchecked_min_element(it, last, compare, projection));
}
Expand Down
4 changes: 2 additions & 2 deletions include/cpp-sort/sorters/selection_sorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ namespace cppsort
is_projection_iterator_v<Projection, ForwardIterator, Compare>
>
>
auto operator()(ForwardIterator first, ForwardIterator last,
Compare compare={}, Projection projection={}) const
constexpr auto operator()(ForwardIterator first, ForwardIterator last,
Compare compare={}, Projection projection={}) const
-> void
{
static_assert(
Expand Down
3 changes: 2 additions & 1 deletion tests/sorters/every_sorter_constexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace
}

TEMPLATE_TEST_CASE( "test every constexpr sorter", "[sorters][constexpr]",
cppsort::insertion_sorter )
cppsort::insertion_sorter,
cppsort::selection_sorter )
{
constexpr bool is_sorted = test_constexpr_sorter<TestType>();
STATIC_CHECK( is_sorted );
Expand Down

0 comments on commit 7afc95a

Please sign in to comment.