|
| 1 | +// Copyright (c) 2024 Simons Foundation |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0.txt |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell, chuffa |
| 16 | + |
| 17 | +/** |
| 18 | + * @file |
| 19 | + * @brief Provides a range adapting function for slicing a given range/view. |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef _ITERTOOLS_SORT_HPP |
| 23 | +#define _ITERTOOLS_SORT_HPP |
| 24 | + |
| 25 | +#include "./utils.hpp" |
| 26 | + |
| 27 | +#include <algorithm> // for iter_swap |
| 28 | +#include <cstddef> // for size_t |
| 29 | +#include <functional> // for less |
| 30 | +#include <iterator> // for forward_iterator, prev, random_access_iterator |
| 31 | + |
| 32 | +namespace itertools { |
| 33 | + |
| 34 | + /** |
| 35 | + * @ingroup sorting |
| 36 | + * @brief Bubble sort |
| 37 | + * |
| 38 | + * @details Sort the elements in the range [@p first, @p last) in the order |
| 39 | + * prescribed by the comparison function @p comp. The underlying sorting |
| 40 | + * algorithm is bubble sort. The sorting is stable, i.e. already sorted |
| 41 | + * elements will not be swapped. The number of swaps necessary to get the |
| 42 | + * elements into sorted order is recorded and returned. |
| 43 | + * |
| 44 | + * Computational complexity: \f$\mathcal{O}(n^2)\f$ |
| 45 | + * |
| 46 | + * This function is eager and puts the range in sorted order. |
| 47 | + * |
| 48 | + * @param first Forward iterator for the first element of the range. |
| 49 | + * @param last Forward iterator for the element \a after the last of the range. |
| 50 | + * @param comp Comparison function callable with two dereferenced iterators. |
| 51 | + * @return Number of swaps necessary to sort the range. |
| 52 | + */ |
| 53 | + template <class ForwardIt, class Compare> |
| 54 | + requires std::forward_iterator<ForwardIt> |
| 55 | + std::size_t bubble_sort(ForwardIt first, ForwardIt last, Compare comp) { |
| 56 | + if (first == last) { return 0; } |
| 57 | + std::size_t n_swaps = 0; |
| 58 | + for (ForwardIt sorted = first; first != last; last = sorted) { |
| 59 | + sorted = first; |
| 60 | + for (ForwardIt curr = first, prev = first; ++curr != last; ++prev) { |
| 61 | + if (comp(*curr, *prev)) { |
| 62 | + std::iter_swap(curr, prev); |
| 63 | + sorted = curr; |
| 64 | + ++n_swaps; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + return n_swaps; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @ingroup sorting |
| 73 | + * @overload |
| 74 | + * |
| 75 | + * Sets the default comparison function to `std::less{}`. |
| 76 | + */ |
| 77 | + template <class ForwardIt> |
| 78 | + requires std::forward_iterator<ForwardIt> |
| 79 | + std::size_t bubble_sort(ForwardIt first, ForwardIt last) { |
| 80 | + return bubble_sort(first, last, std::less{}); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @ingroup sorting |
| 85 | + * @brief Insertion sort |
| 86 | + * |
| 87 | + * @details Sort the elements in the range [@p first, @p last) in the order |
| 88 | + * prescribed by the comparison function @p comp. The underlying sorting |
| 89 | + * algorithm is insertion sort. The sorting is stable, i.e. already sorted |
| 90 | + * elements will not be swapped. The number of swaps necessary to get the |
| 91 | + * elements into sorted order is recorded and returned. |
| 92 | + * |
| 93 | + * Computational complexity: \f$\mathcal{O}(n^2)\f$ |
| 94 | + * |
| 95 | + * This function is eager and puts the range in sorted order. |
| 96 | + * |
| 97 | + * @param first Random-access iterator for the first element of the range. |
| 98 | + * @param last Random-access iterator for the element \a after the last of the range. |
| 99 | + * @param comp Comparison function callable with two dereferenced iterators. |
| 100 | + * @return Number of swaps necessary to sort the range. |
| 101 | + */ |
| 102 | + template <class RandomIt, class Compare> |
| 103 | + requires std::random_access_iterator<RandomIt> |
| 104 | + std::size_t insertion_sort(RandomIt first, RandomIt last, Compare comp) { |
| 105 | + if (first == last) { return 0; } |
| 106 | + std::size_t swaps = 0; |
| 107 | + for (RandomIt i = std::next(first); i != last; ++i) { |
| 108 | + for (RandomIt j = i; j != first && comp(*j, *std::prev(j)); --j) { |
| 109 | + std::iter_swap(std::prev(j), j); |
| 110 | + ++swaps; |
| 111 | + } |
| 112 | + } |
| 113 | + return swaps; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @ingroup sorting |
| 118 | + * @overload |
| 119 | + * |
| 120 | + * Sets the default comparison function to `std::less{}`. |
| 121 | + */ |
| 122 | + template <class RandomIt> |
| 123 | + requires std::random_access_iterator<RandomIt> |
| 124 | + std::size_t insertion_sort(RandomIt first, RandomIt last) { |
| 125 | + return insertion_sort(first, last, std::less{}); |
| 126 | + } |
| 127 | + |
| 128 | +} // namespace itertools |
| 129 | + |
| 130 | +#endif // _ITERTOOLS_SORT_HPP |
0 commit comments