Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
AlgorithmBubbleSort.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/PrimitiveTypes.h"
5
6namespace SC
7{
8namespace TypeTraits
9{
10// clang-format off
12template <class T> struct RemovePointer { using type = T; };
13template <class T> struct RemovePointer<T*> { using type = T; };
14template <class T> struct RemovePointer<T**> { using type = T; };
15// clang-format on
16} // namespace TypeTraits
19
20namespace Algorithms
21{
24
26template <typename T>
28{
33 constexpr bool operator()(const T& a, const T& b) { return a < b; }
34};
35
42template <typename Iterator, typename BinaryPredicate = smallerThan<typename TypeTraits::RemovePointer<Iterator>::type>>
43constexpr void bubbleSort(Iterator first, Iterator last, BinaryPredicate predicate = BinaryPredicate())
44{
45 if (first >= last)
46 {
47 return;
48 }
49 bool doSwap = true;
50 while (doSwap)
51 {
52 doSwap = false;
53 Iterator p0 = first;
54 Iterator p1 = first + 1;
55 while (p1 != last)
56 {
57 if (predicate(*p1, *p0))
58 {
59 swap(*p1, *p0);
60 doSwap = true;
61 }
62 ++p0;
63 ++p1;
64 }
65 }
66}
68} // namespace Algorithms
69} // namespace SC
constexpr void bubbleSort(Iterator first, Iterator last, BinaryPredicate predicate=BinaryPredicate())
Sorts iterator range according to BinaryPredicate (bubble sort).
Definition AlgorithmBubbleSort.h:43
constexpr void swap(T &t1, T &t2)
Swaps the values of two objects.
Definition Compiler.h:270
Functor that evaluates to a < b
Definition AlgorithmBubbleSort.h:28
constexpr bool operator()(const T &a, const T &b)
Returns true if a < b
Definition AlgorithmBubbleSort.h:33
RemovePointer removes the pointer qualification from a type T.
Definition AlgorithmBubbleSort.h:12