|
| 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 functions for sorting ranges. |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef _ITERTOOLS_SORT_HPP |
| 23 | +#define _ITERTOOLS_SORT_HPP |
| 24 | + |
| 25 | +#include <algorithm> |
| 26 | +#include <cstddef> |
| 27 | +#include <functional> |
| 28 | +#include <iterator> |
| 29 | +#include <ranges> |
| 30 | + |
| 31 | +namespace itertools { |
| 32 | + |
| 33 | + /** |
| 34 | + * @addtogroup sorting |
| 35 | + * @{ |
| 36 | + */ |
| 37 | + |
| 38 | + /** |
| 39 | + * @brief Bubble sort elements in the given range. |
| 40 | + * |
| 41 | + * @details Sort the elements in the range `[first, last)` in the order prescribed by the comparison function `comp`. |
| 42 | + * The underlying sorting algorithm is a stable bubble sort, i.e. already sorted elements will not be swapped. The |
| 43 | + * number of swaps necessary to get the elements into sorted order is recorded and returned. |
| 44 | + * |
| 45 | + * Computational complexity: \f$ \mathcal{O}(n^2) \f$. |
| 46 | + * |
| 47 | + * This function is eager and puts the range in sorted order. |
| 48 | + * |
| 49 | + * @tparam ForwardIt Forward iterator type. |
| 50 | + * @tparam Compare Comparison function type. |
| 51 | + * @param first Forward iterator to the first element of the range. |
| 52 | + * @param last Forward iterator to the element after the last of the range. |
| 53 | + * @param comp Comparison function callable with two dereferenced iterators. |
| 54 | + * @return Number of swaps necessary to sort the range. |
| 55 | + */ |
| 56 | + template <std::forward_iterator ForwardIt, class Compare = std::less<>> |
| 57 | + std::size_t bubble_sort(ForwardIt first, ForwardIt last, Compare comp = {}) { |
| 58 | + if (first == last) { return 0; } |
| 59 | + std::size_t n_swaps = 0; |
| 60 | + for (ForwardIt sorted = first; first != last; last = sorted) { |
| 61 | + sorted = first; |
| 62 | + for (ForwardIt curr = first, prev = first; ++curr != last; ++prev) { |
| 63 | + if (comp(*curr, *prev)) { |
| 64 | + std::iter_swap(curr, prev); |
| 65 | + sorted = curr; |
| 66 | + ++n_swaps; |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return n_swaps; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @brief Insertion sort elements in the given range. |
| 75 | + * |
| 76 | + * @details Sort the elements in the range `[first, last)` in the order prescribed by the comparison function `comp`. |
| 77 | + * The underlying sorting algorithm is a stable insertion sort, i.e. already sorted elements will not be swapped. The |
| 78 | + * number of swaps necessary to get the elements into sorted order is recorded and returned. |
| 79 | + * |
| 80 | + * Computational complexity: \f$ \mathcal{O}(n^2) \f$. |
| 81 | + * |
| 82 | + * This function is eager and puts the range in sorted order. |
| 83 | + * |
| 84 | + * @tparam BidirIt Bidirectional iterator type. |
| 85 | + * @tparam Compare Comparison function type. |
| 86 | + * @param first Bidirectional iterator to the first element of the range. |
| 87 | + * @param last Bidirectional iterator to the element after the last of the range. |
| 88 | + * @param comp Comparison function callable with two dereferenced iterators. |
| 89 | + * @return Number of swaps necessary to sort the range. |
| 90 | + */ |
| 91 | + template <std::bidirectional_iterator BidirIt, class Compare = std::less<>> |
| 92 | + std::size_t insertion_sort(BidirIt first, BidirIt last, Compare comp = {}) { |
| 93 | + if (first == last) { return 0; } |
| 94 | + std::size_t swaps = 0; |
| 95 | + for (BidirIt i = std::next(first); i != last; ++i) { |
| 96 | + for (BidirIt j = i; j != first && comp(*j, *std::prev(j)); --j) { |
| 97 | + std::iter_swap(std::prev(j), j); |
| 98 | + ++swaps; |
| 99 | + } |
| 100 | + } |
| 101 | + return swaps; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @brief Bubble sort elements in the given range. |
| 106 | + * |
| 107 | + * @details See itertools::bubble_sort for more details. |
| 108 | + * |
| 109 | + * @tparam Range Forward range type. |
| 110 | + * @tparam Compare Comparison function type. |
| 111 | + * @param rng A forward range to sort. |
| 112 | + * @param comp Comparison function callable with two dereferenced iterators. |
| 113 | + * @return Number of swaps necessary to sort the range. |
| 114 | + */ |
| 115 | + template <std::ranges::forward_range Range, class Compare = std::less<>> |
| 116 | + std::size_t bubble_sort(Range &&rng, Compare comp = {}) { // NOLINT (ranges need not be forwarded) |
| 117 | + return bubble_sort(std::ranges::begin(rng), std::ranges::end(rng), comp); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief Insertion sort elements in the given range. |
| 122 | + * |
| 123 | + * @details See itertools::insertion_sort for more details. |
| 124 | + * |
| 125 | + * @tparam Range Bidirectional range type. |
| 126 | + * @tparam Compare Comparison function type. |
| 127 | + * @param rng A bidirectional range to sort. |
| 128 | + * @param comp Comparison function callable with two dereferenced iterators. |
| 129 | + * @return Number of swaps necessary to sort the range. |
| 130 | + */ |
| 131 | + template <std::ranges::bidirectional_range Range, class Compare = std::less<>> |
| 132 | + std::size_t insertion_sort(Range &&rng, Compare comp = {}) { // NOLINT (ranges need not be forwarded) |
| 133 | + return insertion_sort(std::ranges::begin(rng), std::ranges::end(rng), comp); |
| 134 | + } |
| 135 | + |
| 136 | + /** @} */ |
| 137 | + |
| 138 | +} // namespace itertools |
| 139 | + |
| 140 | +#endif // _ITERTOOLS_SORT_HPP |
0 commit comments