Skip to content

Commit

Permalink
Constrain main entry points with concepts
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Jan 16, 2024
1 parent 5e502fa commit 5235a89
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,32 @@ The list of available signatures is as follows (in namespace `gfx`):
// timsort

template <
typename RandomAccessIterator,
std::random_access_iterator Iterator,
typename Compare = std::ranges::less,
typename Projection = std::identity
>
void timsort(RandomAccessIterator const first, RandomAccessIterator const last,
requires std::sortable<Iterator, Compare, Projection>
void timsort(Iterator first, Iterator last,
Compare compare={}, Projection projection={});

template <
typename RandomAccessRange,
std::ranges::random_access_range Range,
typename Compare = std::ranges::less,
typename Projection = std::identity
>
void timsort(RandomAccessRange &range, Compare compare={}, Projection projection={});
requires std::sortable<std::ranges::iterator_t<Range>, Compare, Projection>
void timsort(Range &range, Compare compare={}, Projection projection={});

// timmerge

template <
typename RandomAccessIterator,
std::random_access_iterator Iterator,
typename Compare = std::ranges::less,
typename Projection = std::identity
>
void timmerge(RandomAccessIterator first, RandomAccessIterator middle,
RandomAccessIterator last, Compare compare={}, Projection projection={});
requires std::sortable<Iterator, Compare, Projection>
void timmerge(Iterator first, Iterator middle, Iterator last,
Compare compare={}, Projection projection={});
```
## EXAMPLE
Expand Down
20 changes: 12 additions & 8 deletions include/gfx/timsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <algorithm>
#include <functional>
#include <iterator>
#include <ranges>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -693,41 +694,44 @@ template <typename RandomAccessIterator, typename Compare, typename Projection>
* sorted range [first, last) with a comparison function and a projection function.
*/
template <
typename RandomAccessIterator,
std::random_access_iterator Iterator,
typename Compare = std::less<>,
typename Projection = std::identity
>
void timmerge(RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last,
requires std::sortable<Iterator, Compare, Projection>
void timmerge(Iterator first, Iterator middle, Iterator last,
Compare comp={}, Projection proj={}) {
GFX_TIMSORT_AUDIT(std::is_sorted(first, middle, comp, proj) && "Precondition");
GFX_TIMSORT_AUDIT(std::is_sorted(middle, last, comp, proj) && "Precondition");
detail::TimSort<RandomAccessIterator, Compare, Projection>::merge(first, middle, last, comp, proj);
detail::TimSort<Iterator, Compare, Projection>::merge(first, middle, last, comp, proj);
GFX_TIMSORT_AUDIT(std::is_sorted(first, last, comp, proj) && "Postcondition");
}

/**
* Stably sorts a range with a comparison function and a projection function.
*/
template <
typename RandomAccessIterator,
std::random_access_iterator Iterator,
typename Compare = std::ranges::less,
typename Projection = std::identity
>
void timsort(RandomAccessIterator const first, RandomAccessIterator const last,
requires std::sortable<Iterator, Compare, Projection>
void timsort(Iterator first, Iterator last,
Compare comp={}, Projection proj={}) {
detail::TimSort<RandomAccessIterator, Compare, Projection>::sort(first, last, comp, proj);
detail::TimSort<Iterator, Compare, Projection>::sort(first, last, comp, proj);
GFX_TIMSORT_AUDIT(std::is_sorted(first, last, comp, proj) && "Postcondition");
}

/**
* Stably sorts a range with a comparison function and a projection function.
*/
template <
typename RandomAccessRange,
std::ranges::random_access_range Range,
typename Compare = std::ranges::less,
typename Projection = std::identity
>
void timsort(RandomAccessRange &range, Compare comp={}, Projection proj={}) {
requires std::sortable<std::ranges::iterator_t<Range>, Compare, Projection>
void timsort(Range &range, Compare comp={}, Projection proj={}) {
gfx::timsort(std::begin(range), std::end(range), comp, proj);
}

Expand Down

0 comments on commit 5235a89

Please sign in to comment.