Skip to content

Commit

Permalink
Add sanity checks for user provided parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
psadda committed Sep 26, 2024
1 parent 082254d commit f99fe72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/crumsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,17 @@ void crumsort_swap(Iterator array, swap_space<T>& swap, size_t nmemb, Compare cm
template<typename Iterator, typename Compare>
void crumsort(Iterator begin, const Iterator end, Compare cmp, size_t max_swap_size = 512)
{
static_assert (
#if __cplusplus >= 202002L
std::random_access_iterator<Iterator>,
#else
std::is_convertible_v<typename std::iterator_traits<Iterator>::iterator_category, std::random_access_iterator_tag>,
#endif
"type 'Iterator' must be a random access iterator"
);

assert(max_swap_size > 0);

typedef std::remove_reference_t<decltype(*begin)> T;

size_t nmemb = static_cast<size_t>(end - begin);
Expand Down
9 changes: 9 additions & 0 deletions src/quadsort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,15 @@ void quadsort_swap(Iterator array, swap_space<T>& swap, size_t nmemb, Compare cm
template<typename Iterator, typename Compare>
void quadsort(Iterator begin, Iterator end, Compare cmp)
{
static_assert (
#if __cplusplus >= 202002L
std::random_access_iterator<Iterator>,
#else
std::is_convertible_v<typename std::iterator_traits<Iterator>::iterator_category, std::random_access_iterator_tag>,
#endif
"type 'Iterator' must be a random access iterator"
);

typedef std::remove_reference_t<decltype(*begin)> T;

size_t nmemb = std::distance(begin, end);
Expand Down

0 comments on commit f99fe72

Please sign in to comment.