Skip to content

Commit

Permalink
constexpr hybird_adapter (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Jul 21, 2022
1 parent c29f383 commit 96a2d51
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
14 changes: 7 additions & 7 deletions include/cpp-sort/adapters/hybrid_adapter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2021 Morwenn
* Copyright (c) 2015-2022 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_ADAPTERS_HYBRID_ADAPTER_H_
Expand Down Expand Up @@ -107,14 +107,14 @@ namespace cppsort
// position into the sorters

template<typename... Args>
auto operator()(choice<Ind>, Args&&... args) const
constexpr auto operator()(choice<Ind>, Args&&... args) const
-> decltype(this->get()(std::forward<Args>(args)...))
{
return this->get()(std::forward<Args>(args)...);
}

template<typename... Args>
static auto _detail_stability(choice<Ind>, Args&&... args)
static constexpr auto _detail_stability(choice<Ind>, Args&&... args)
-> detail::enable_if_t<
is_invocable_v<Sorter, Args...>,
is_stable<Sorter(Args...)>
Expand Down Expand Up @@ -272,7 +272,7 @@ namespace cppsort
// Call operator

template<typename Iterable, typename... Args>
auto operator()(Iterable&& iterable, Args&&... args) const
constexpr auto operator()(Iterable&& iterable, Args&&... args) const
-> decltype(base_class::operator()(
detail::choice_for_it<decltype(std::begin(iterable)), sizeof...(Sorters)>{},
std::forward<Iterable>(iterable),
Expand All @@ -287,7 +287,7 @@ namespace cppsort
}

template<typename Iterator, typename... Args>
auto operator()(Iterator first, Iterator last, Args&&... args) const
constexpr auto operator()(Iterator first, Iterator last, Args&&... args) const
-> decltype(base_class::operator()(
detail::choice_for_it<Iterator, sizeof...(Sorters)>{},
std::move(first), std::move(last),
Expand All @@ -305,15 +305,15 @@ namespace cppsort
// Stability of a call

template<typename Iterable, typename... Args>
static auto _detail_stability(Iterable&& iterable, Args&&... args)
static constexpr auto _detail_stability(Iterable&& iterable, Args&&... args)
-> decltype(base_class::_detail_stability(
detail::choice_for_it<decltype(std::begin(iterable)), sizeof...(Sorters)>{},
std::forward<Iterable>(iterable),
std::forward<Args>(args)...
));

template<typename Iterator, typename... Args>
static auto _detail_stability(Iterator first, Iterator last, Args&&... args)
static constexpr auto _detail_stability(Iterator first, Iterator last, Args&&... args)
-> decltype(base_class::_detail_stability(
detail::choice_for_it<Iterator, sizeof...(Sorters)>{},
std::move(first), std::move(last),
Expand Down
23 changes: 18 additions & 5 deletions tests/sorter_facade_constexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <type_traits>
#include <utility>
#include <catch2/catch_test_macros.hpp>
#include <cpp-sort/adapters/hybrid_adapter.h>
#include <cpp-sort/sorter_facade.h>
#include <cpp-sort/sorter_traits.h>
#include <cpp-sort/utility/as_function.h>
Expand Down Expand Up @@ -63,21 +64,33 @@ namespace
cppsort::sorter_facade<constexpr_insertion_sorter_impl>
{};

template<typename Sorter>
constexpr auto test_sorter()
-> bool
{
constexpr std::size_t size = 13;
int collection[size] = { 15, 6, 0, 2, 2, 3, 8, 12, 10, 5, 9, 7, 10 };
constexpr_insertion_sorter sorter;
Sorter sorter;
sorter(collection, collection + size);
return helpers::is_sorted(collection, collection + size);
}
}

TEST_CASE( "test basic constexpr support", "[sorter_facade][constexpr]" )
{
// Check that sorter_facade can be useful in a constexpr constext
// if the sorter implementation is constexpr-friendly itself
constexpr bool is_sorted = test_sorter();
STATIC_CHECK( is_sorted );
SECTION( "simple test" )
{
// Check that sorter_facade can be useful in a constexpr constext
// if the sorter implementation is constexpr-friendly itself
constexpr bool is_sorted = test_sorter<constexpr_insertion_sorter>();
STATIC_CHECK( is_sorted );
}

SECTION( "hybrid_adapter test" )
{
constexpr bool is_sorted = test_sorter<
cppsort::hybrid_adapter<constexpr_insertion_sorter>
>();
STATIC_CHECK( is_sorted );
}
}

0 comments on commit 96a2d51

Please sign in to comment.