Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
226 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) 2022 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#ifndef CPPSORT_ADAPTERS_SPLIT_ADAPTER_H_ | ||
#define CPPSORT_ADAPTERS_SPLIT_ADAPTER_H_ | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Headers | ||
//////////////////////////////////////////////////////////// | ||
#include <functional> | ||
#include <iterator> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <cpp-sort/sorter_facade.h> | ||
#include <cpp-sort/sorter_traits.h> | ||
#include <cpp-sort/utility/adapter_storage.h> | ||
#include <cpp-sort/utility/functional.h> | ||
#include "../detail/split_sort.h" | ||
#include "../detail/type_traits.h" | ||
|
||
namespace cppsort | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
// Adapter | ||
|
||
namespace detail | ||
{ | ||
template<typename Sorter, typename=void> | ||
struct split_adapter_iterator_category | ||
{ | ||
using type = std::bidirectional_iterator_tag; | ||
}; | ||
|
||
template<typename Sorter> | ||
struct split_adapter_iterator_category< | ||
Sorter, | ||
void_t<typename sorter_traits<Sorter>::iterator_category> | ||
> | ||
{ | ||
using type = detail::conditional_t< | ||
std::is_base_of< | ||
std::bidirectional_iterator_tag, | ||
typename sorter_traits<Sorter>::iterator_category | ||
>::value, | ||
typename sorter_traits<Sorter>::iterator_category, | ||
std::bidirectional_iterator_tag | ||
>; | ||
}; | ||
|
||
template<typename Sorter> | ||
struct split_adapter_impl: | ||
utility::adapter_storage<Sorter> | ||
{ | ||
split_adapter_impl() = default; | ||
|
||
constexpr explicit split_adapter_impl(Sorter&& sorter): | ||
utility::adapter_storage<Sorter>(std::move(sorter)) | ||
{} | ||
|
||
template< | ||
typename BidirectionalIterator, | ||
typename Compare = std::less<>, | ||
typename Projection = utility::identity, | ||
typename = detail::enable_if_t< | ||
is_projection_iterator_v<Projection, BidirectionalIterator, Compare> | ||
> | ||
> | ||
auto operator()(BidirectionalIterator first, BidirectionalIterator last, | ||
Compare compare={}, Projection projection={}) const | ||
-> void | ||
{ | ||
static_assert( | ||
std::is_base_of< | ||
iterator_category, | ||
iterator_category_t<BidirectionalIterator> | ||
>::value, | ||
"split_adapter requires a stronger iterator category" | ||
); | ||
|
||
split_sort(std::move(first), std::move(last), | ||
std::move(compare), std::move(projection), | ||
this->get()); | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Sorter traits | ||
|
||
using iterator_category | ||
= typename split_adapter_iterator_category<Sorter>::type; | ||
using is_always_stable = std::false_type; | ||
}; | ||
} | ||
|
||
template<typename Sorter> | ||
struct split_adapter: | ||
sorter_facade<detail::split_adapter_impl<Sorter>> | ||
{ | ||
split_adapter() = default; | ||
|
||
constexpr explicit split_adapter(Sorter sorter): | ||
sorter_facade<detail::split_adapter_impl<Sorter>>(std::move(sorter)) | ||
{} | ||
}; | ||
} | ||
|
||
#endif // CPPSORT_ADAPTERS_SPLIT_ADAPTER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2022 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <list> | ||
#include <vector> | ||
#include <catch2/catch_template_test_macros.hpp> | ||
#include <cpp-sort/adapters/split_adapter.h> | ||
#include <cpp-sort/sorters.h> | ||
#include <cpp-sort/utility/buffer.h> | ||
#include <testing-tools/distributions.h> | ||
|
||
TEMPLATE_TEST_CASE( "every random-access sorter with split_adapter", "[split_adapter]", | ||
cppsort::adaptive_shivers_sorter, | ||
cppsort::cartesian_tree_sorter, | ||
cppsort::default_sorter, | ||
cppsort::drop_merge_sorter, | ||
cppsort::grail_sorter<>, | ||
cppsort::heap_sorter, | ||
cppsort::insertion_sorter, | ||
cppsort::mel_sorter, | ||
cppsort::merge_insertion_sorter, | ||
cppsort::merge_sorter, | ||
cppsort::pdq_sorter, | ||
cppsort::poplar_sorter, | ||
cppsort::quick_merge_sorter, | ||
cppsort::quick_sorter, | ||
cppsort::selection_sorter, | ||
cppsort::slab_sorter, | ||
cppsort::ska_sorter, | ||
cppsort::smooth_sorter, | ||
cppsort::spin_sorter, | ||
cppsort::split_sorter, | ||
cppsort::spread_sorter, | ||
cppsort::std_sorter, | ||
cppsort::tim_sorter, | ||
cppsort::wiki_sorter<cppsort::utility::fixed_buffer<0>> ) | ||
{ | ||
std::vector<double> collection; | ||
collection.reserve(1'000); | ||
auto distribution = dist::inversions(0.2); // Big enough merges | ||
distribution.call<double>(std::back_inserter(collection), 1'000); | ||
|
||
cppsort::split_adapter<TestType> sorter; | ||
sorter(collection); | ||
CHECK( std::is_sorted(collection.begin(), collection.end()) ); | ||
} | ||
|
||
TEMPLATE_TEST_CASE( "every bidirectional sorter with split_adapter", "[split_adapter]", | ||
cppsort::cartesian_tree_sorter, | ||
cppsort::default_sorter, | ||
cppsort::drop_merge_sorter, | ||
cppsort::insertion_sorter, | ||
cppsort::mel_sorter, | ||
cppsort::merge_sorter, | ||
cppsort::quick_merge_sorter, | ||
cppsort::quick_sorter, | ||
cppsort::selection_sorter, | ||
cppsort::slab_sorter ) | ||
{ | ||
std::list<double> collection; | ||
auto distribution = dist::inversions(0.2); // Big enough merges | ||
distribution.call<double>(std::back_inserter(collection), 1'000); | ||
|
||
cppsort::split_adapter<TestType> sorter; | ||
sorter(collection); | ||
CHECK( std::is_sorted(collection.begin(), collection.end()) ); | ||
} |