From 1200b1a038d950c108f583c9e5a5b742bead64e4 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 19 May 2017 15:22:44 +1000 Subject: [PATCH] algo/sort: assert we're operating on random iterators --- algo/sort.ipp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/algo/sort.ipp b/algo/sort.ipp index e2cbd811..cd31f815 100644 --- a/algo/sort.ipp +++ b/algo/sort.ipp @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include "../debug.hpp" #include "../tuple.hpp" @@ -37,6 +39,13 @@ namespace cruft::util::sort::detail { void index_swap (IndexA a, IndexB b, RandomIt value) { + static_assert( + std::is_base_of< + std::random_access_iterator_tag, + typename std::iterator_traits::iterator_category + >::value + ); + std::swap (value[a], value[b]); } @@ -58,6 +67,20 @@ cruft::util::sort::reorder (IndexIt idx_first, ValueIt value, OtherIt ...tail) { + static_assert ( + std::is_base_of< + std::random_access_iterator_tag, + typename std::iterator_traits::iterator_category + >::value + ); + + static_assert ( + std::is_base_of< + std::random_access_iterator_tag, + typename std::iterator_traits::iterator_category + >::value + ); + // Bail early on zero size arrays, partly so we can simplify some // debugging checks auto size = std::distance (idx_first, idx_last); @@ -84,6 +107,13 @@ cruft::util::sort::soa (RandomIt key_first, Comparator comp, Args ...values) { + static_assert ( + std::is_base_of< + std::random_access_iterator_tag, + typename std::iterator_traits::iterator_category + >::value + ); + // bail early on guaranteed sorted or degenerate cases. we can make some // assumptions about minimum array sizes and non-wrapping indices later on // this way.