Skip to content

Commit 93ef41f

Browse files
author
Alexandr Borzykh
committed
algorithm/bubble_sort: add random_access_iterator check
1 parent 6d27832 commit 93ef41f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

include/cslib/algorithm/sort/bubble_sort.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,18 @@ namespace algorithm
3737
*
3838
* Interesting fact:
3939
* If CompareT::operator() does strong check (i.e. < OR >), this algorithm is stable. Overwise it's not.
40-
*
41-
* TODO: iterator must be RandomAccessIterator, add this check
4240
*/
4341

44-
template < typename IteratorT, typename CompareT = std::less<typename std::iterator_traits<IteratorT>::value_type> >
42+
template <
43+
typename IteratorT,
44+
typename CompareT = std::less<typename std::iterator_traits<IteratorT>::value_type> >,
45+
typename = typename std::enable_if<
46+
std::is_convertible<
47+
typename std::iterator_traits<IteratorT>::iterator_category,
48+
typename std::random_access_iterator_tag
49+
>::value
50+
>::type
51+
>
4552
void bubble_sort(IteratorT first, IteratorT last, CompareT compare = CompareT())
4653
{
4754
for (IteratorT iIter = first; iIter != last; ++iIter)

0 commit comments

Comments
 (0)