-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New adapter: drop_merge_adapter (#148)
- Loading branch information
Showing
8 changed files
with
209 additions
and
56 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,84 @@ | ||
/* | ||
* Copyright (c) 2022 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#ifndef CPPSORT_ADAPTERS_DROP_MERGE_ADAPTER_H_ | ||
#define CPPSORT_ADAPTERS_DROP_MERGE_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/drop_merge_sort.h" | ||
#include "../detail/type_traits.h" | ||
|
||
namespace cppsort | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
// Adapter | ||
|
||
namespace detail | ||
{ | ||
template<typename Sorter> | ||
struct drop_merge_adapter_impl: | ||
utility::adapter_storage<Sorter> | ||
{ | ||
drop_merge_adapter_impl() = default; | ||
|
||
constexpr explicit drop_merge_adapter_impl(Sorter&& sorter): | ||
utility::adapter_storage<Sorter>(std::move(sorter)) | ||
{} | ||
|
||
template< | ||
typename ForwardIterator, | ||
typename Compare = std::less<>, | ||
typename Projection = utility::identity, | ||
typename = detail::enable_if_t< | ||
is_projection_iterator_v<Projection, ForwardIterator, Compare> | ||
> | ||
> | ||
auto operator()(ForwardIterator first, ForwardIterator last, | ||
Compare compare={}, Projection projection={}) const | ||
-> void | ||
{ | ||
static_assert( | ||
std::is_base_of< | ||
iterator_category, | ||
iterator_category_t<ForwardIterator> | ||
>::value, | ||
"drop_merge_adapter requires at least bidirectional iterators" | ||
); | ||
|
||
drop_merge_sort(std::move(first), std::move(last), | ||
std::move(compare), std::move(projection), | ||
this->get()); | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Sorter traits | ||
|
||
using iterator_category = std::bidirectional_iterator_tag; | ||
using is_always_stable = std::false_type; | ||
}; | ||
} | ||
|
||
template<typename Sorter> | ||
struct drop_merge_adapter: | ||
sorter_facade<detail::drop_merge_adapter_impl<Sorter>> | ||
{ | ||
drop_merge_adapter() = default; | ||
|
||
constexpr explicit drop_merge_adapter(Sorter sorter): | ||
sorter_facade<detail::drop_merge_adapter_impl<Sorter>>(std::move(sorter)) | ||
{} | ||
}; | ||
} | ||
|
||
#endif // CPPSORT_ADAPTERS_DROP_MERGE_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,84 @@ | ||
/* | ||
* 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/drop_merge_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 drop_merge_adapter", "[drop_merge_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::drop_merge_adapter<TestType> sorter; | ||
sorter(collection); | ||
CHECK( std::is_sorted(collection.begin(), collection.end()) ); | ||
} | ||
|
||
TEMPLATE_TEST_CASE( "every bidirectional sorter with drop_merge_adapter", "[drop_merge_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::list<double> collection; | ||
auto distribution = dist::inversions(0.2); // Big enough merges | ||
distribution.call<double>(std::back_inserter(collection), 1'000); | ||
|
||
cppsort::drop_merge_adapter<TestType> sorter; | ||
sorter(collection); | ||
CHECK( std::is_sorted(collection.begin(), collection.end()) ); | ||
} |