-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New component: sorted_iterators (#200)
- Loading branch information
Showing
5 changed files
with
265 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* Copyright (c) 2022 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#ifndef CPPSORT_UTILITY_SORTED_ITERATORS_H_ | ||
#define CPPSORT_UTILITY_SORTED_ITERATORS_H_ | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Headers | ||
//////////////////////////////////////////////////////////// | ||
#include <functional> | ||
#include <iterator> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
#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 <cpp-sort/utility/size.h> | ||
#include "../detail/checkers.h" | ||
#include "../detail/functional.h" | ||
#include "../detail/iterator_traits.h" | ||
#include "../detail/type_traits.h" | ||
|
||
namespace cppsort | ||
{ | ||
namespace utility | ||
{ | ||
namespace detail | ||
{ | ||
template< | ||
typename Sorter, | ||
typename Iterator, | ||
typename Compare, | ||
typename Projection | ||
> | ||
auto compute_sorted_iterators(Sorter&& sorter, Iterator first, Iterator last, | ||
cppsort::detail::difference_type_t<Iterator> size, | ||
Compare compare, Projection projection) | ||
-> std::vector<Iterator> | ||
{ | ||
// Copy the iterators in a vector | ||
std::vector<Iterator> iterators; | ||
iterators.reserve(size); | ||
for (auto it = first; it != last; ++it) { | ||
iterators.emplace_back(it); | ||
} | ||
|
||
// Sort the iterators on pointed values | ||
std::forward<Sorter>(sorter)(iterators.begin(), iterators.end(), | ||
std::move(compare), | ||
cppsort::detail::indirect(std::move(projection))); | ||
|
||
return iterators; | ||
} | ||
|
||
template<typename Sorter> | ||
struct sorted_iterators_impl: | ||
utility::adapter_storage<Sorter>, | ||
cppsort::detail::check_is_always_stable<Sorter> | ||
{ | ||
sorted_iterators_impl() = default; | ||
|
||
constexpr explicit sorted_iterators_impl(Sorter&& sorter): | ||
utility::adapter_storage<Sorter>(std::move(sorter)) | ||
{} | ||
|
||
template< | ||
typename ForwardIterable, | ||
typename Compare = std::less<>, | ||
typename Projection = utility::identity, | ||
typename = cppsort::detail::enable_if_t< | ||
is_projection_v<Projection, ForwardIterable, Compare> | ||
> | ||
> | ||
auto operator()(ForwardIterable&& iterable, Compare compare={}, Projection projection={}) const | ||
-> std::vector<cppsort::detail::remove_cvref_t<decltype(std::begin(iterable))>> | ||
{ | ||
using category = cppsort::detail::iterator_category_t<decltype(std::begin(iterable))>; | ||
static_assert( | ||
std::is_base_of<iterator_category, category>::value, | ||
"sorted_iterators requires at least forward iterators" | ||
); | ||
|
||
auto dist = cppsort::utility::size(iterable); | ||
return compute_sorted_iterators(this->get(), std::begin(iterable), std::end(iterable), | ||
dist, std::move(compare), std::move(projection)); | ||
} | ||
|
||
template< | ||
typename ForwardIterator, | ||
typename Compare = std::less<>, | ||
typename Projection = utility::identity, | ||
typename = cppsort::detail::enable_if_t< | ||
is_projection_iterator_v<Projection, ForwardIterator, Compare> | ||
> | ||
> | ||
auto operator()(ForwardIterator first, ForwardIterator last, | ||
Compare compare={}, Projection projection={}) const | ||
-> std::vector<ForwardIterator> | ||
{ | ||
using category = cppsort::detail::iterator_category_t<ForwardIterator>; | ||
static_assert( | ||
std::is_base_of<iterator_category, category>::value, | ||
"sorted_iterators requires at least forward iterators" | ||
); | ||
|
||
auto dist = std::distance(first, last); | ||
return compute_sorted_iterators(this->get(), std::move(first), std::move(last), | ||
dist, std::move(compare), std::move(projection)); | ||
} | ||
|
||
//////////////////////////////////////////////////////////// | ||
// Sorter traits | ||
|
||
using iterator_category = std::forward_iterator_tag; | ||
}; | ||
} | ||
|
||
template<typename Sorter> | ||
struct sorted_iterators: | ||
sorter_facade<detail::sorted_iterators_impl<Sorter>> | ||
{ | ||
sorted_iterators() = default; | ||
|
||
constexpr explicit sorted_iterators(Sorter sorter): | ||
sorter_facade<detail::sorted_iterators_impl<Sorter>>(std::move(sorter)) | ||
{} | ||
}; | ||
}} | ||
|
||
namespace cppsort | ||
{ | ||
//////////////////////////////////////////////////////////// | ||
// is_stable specialization | ||
|
||
template<typename Sorter, typename... Args> | ||
struct is_stable<cppsort::utility::sorted_iterators<Sorter>(Args...)>: | ||
is_stable<Sorter(Args...)> | ||
{}; | ||
} | ||
|
||
#endif // CPPSORT_UTILITY_SORTED_ITERATORS_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright (c) 2022 Morwenn | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
#include <iterator> | ||
#include <vector> | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <cpp-sort/sorters/heap_sorter.h> | ||
#include <cpp-sort/sorters/insertion_sorter.h> | ||
#include <cpp-sort/utility/sorted_iterators.h> | ||
#include <testing-tools/distributions.h> | ||
|
||
TEST_CASE( "basic sorted_iterators test", "[utility][sorted_iterators]" ) | ||
{ | ||
SECTION( "simple case" ) | ||
{ | ||
auto get_sorted_iterators_for = cppsort::utility::sorted_iterators<cppsort::heap_sorter>{}; | ||
const std::vector<int> vec = { 6, 4, 2, 1, 8, 7, 0, 9, 5, 3 }; | ||
auto indices = get_sorted_iterators_for(vec.data(), vec.data() + vec.size()); | ||
|
||
std::vector<const int*> expected = { | ||
vec.data() + 6, vec.data() + 3, vec.data() + 2, | ||
vec.data() + 9, vec.data() + 1, vec.data() + 8, | ||
vec.data() + 0, vec.data() + 5, vec.data() + 4, | ||
vec.data() + 7 | ||
}; | ||
CHECK( indices == expected ); | ||
} | ||
|
||
SECTION( "empty collection" ) | ||
{ | ||
auto get_sorted_iterators_for = cppsort::utility::sorted_iterators<cppsort::heap_sorter>{}; | ||
const std::vector<int> vec = {}; | ||
auto indices = get_sorted_iterators_for(vec.data(), vec.data() + vec.size()); | ||
|
||
std::vector<const int*> expected = {}; | ||
CHECK( indices == expected ); | ||
|
||
} | ||
|
||
SECTION( "all_equal" ) | ||
{ | ||
// Use a stable algorithm to get deterministic results for equivalent elements | ||
auto get_sorted_iterators_for = cppsort::utility::sorted_iterators<cppsort::insertion_sorter>{}; | ||
std::vector<int> vec; | ||
auto distribution = dist::all_equal{}; | ||
distribution(std::back_inserter(vec), 10); | ||
auto indices = get_sorted_iterators_for(vec.data(), vec.data() + vec.size()); | ||
|
||
std::vector<int*> expected = { | ||
vec.data() + 0, vec.data() + 1, vec.data() + 2, | ||
vec.data() + 3, vec.data() + 4, vec.data() + 5, | ||
vec.data() + 6, vec.data() + 7, vec.data() + 8, | ||
vec.data() + 9 | ||
}; | ||
CHECK( indices == expected ); | ||
} | ||
|
||
SECTION( "ascending" ) | ||
{ | ||
auto get_sorted_iterators_for = cppsort::utility::sorted_iterators<cppsort::heap_sorter>{}; | ||
std::vector<int> vec; | ||
auto distribution = dist::ascending{}; | ||
distribution(std::back_inserter(vec), 10); | ||
auto indices = get_sorted_iterators_for(vec.data(), vec.data() + vec.size()); | ||
|
||
std::vector<int*> expected = { | ||
vec.data() + 0, vec.data() + 1, vec.data() + 2, | ||
vec.data() + 3, vec.data() + 4, vec.data() + 5, | ||
vec.data() + 6, vec.data() + 7, vec.data() + 8, | ||
vec.data() + 9 | ||
}; | ||
CHECK( indices == expected ); | ||
} | ||
|
||
SECTION( "descending" ) | ||
{ | ||
auto get_sorted_iterators_for = cppsort::utility::sorted_iterators<cppsort::heap_sorter>{}; | ||
std::vector<int> vec; | ||
auto distribution = dist::descending{}; | ||
distribution(std::back_inserter(vec), 10); | ||
auto indices = get_sorted_iterators_for(vec.data(), vec.data() + vec.size()); | ||
|
||
std::vector<int*> expected = { | ||
vec.data() + 9, vec.data() + 8, vec.data() + 7, | ||
vec.data() + 6, vec.data() + 5, vec.data() + 4, | ||
vec.data() + 3, vec.data() + 2, vec.data() + 1, | ||
vec.data() + 0 | ||
}; | ||
CHECK( indices == expected ); | ||
} | ||
} |