|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Morwenn |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + */ |
| 5 | +#ifndef CPPSORT_PROBES_SPEAR_H_ |
| 6 | +#define CPPSORT_PROBES_SPEAR_H_ |
| 7 | + |
| 8 | +//////////////////////////////////////////////////////////// |
| 9 | +// Headers |
| 10 | +//////////////////////////////////////////////////////////// |
| 11 | +#include <functional> |
| 12 | +#include <iterator> |
| 13 | +#include <utility> |
| 14 | +#include <cpp-sort/sorter_facade.h> |
| 15 | +#include <cpp-sort/sorter_traits.h> |
| 16 | +#include <cpp-sort/utility/as_function.h> |
| 17 | +#include <cpp-sort/utility/functional.h> |
| 18 | +#include <cpp-sort/utility/size.h> |
| 19 | +#include <cpp-sort/utility/static_const.h> |
| 20 | +#include "../detail/equal_range.h" |
| 21 | +#include "../detail/immovable_vector.h" |
| 22 | +#include "../detail/iterator_traits.h" |
| 23 | +#include "../detail/pdqsort.h" |
| 24 | +#include "../detail/type_traits.h" |
| 25 | + |
| 26 | +namespace cppsort |
| 27 | +{ |
| 28 | +namespace probe |
| 29 | +{ |
| 30 | + namespace detail |
| 31 | + { |
| 32 | + template<typename ForwardIterator, typename Compare, typename Projection> |
| 33 | + auto spear_probe_algo(ForwardIterator first, ForwardIterator last, |
| 34 | + cppsort::detail::difference_type_t<ForwardIterator> size, |
| 35 | + Compare compare, Projection projection) |
| 36 | + -> ::cppsort::detail::difference_type_t<ForwardIterator> |
| 37 | + { |
| 38 | + using difference_type = ::cppsort::detail::difference_type_t<ForwardIterator>; |
| 39 | + auto&& proj = utility::as_function(projection); |
| 40 | + |
| 41 | + if (size < 2) { |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + //////////////////////////////////////////////////////////// |
| 46 | + // Indirectly sort the iterators |
| 47 | + |
| 48 | + // Copy the iterators in a vector |
| 49 | + cppsort::detail::immovable_vector<ForwardIterator> iterators(size); |
| 50 | + for (auto it = first; it != last; ++it) { |
| 51 | + iterators.emplace_back(it); |
| 52 | + } |
| 53 | + |
| 54 | + // Sort the iterators on pointed values |
| 55 | + cppsort::detail::pdqsort( |
| 56 | + iterators.begin(), iterators.end(), |
| 57 | + compare, utility::indirect{} | projection |
| 58 | + ); |
| 59 | + |
| 60 | + //////////////////////////////////////////////////////////// |
| 61 | + // Sum of distances between an elements and their sorted |
| 62 | + // positions |
| 63 | + |
| 64 | + difference_type sum_dist = 0; |
| 65 | + difference_type it_pos = 0; |
| 66 | + for (auto it = first; it != last; ++it) { |
| 67 | + // Find the range where *it belongs once sorted |
| 68 | + auto rng = cppsort::detail::equal_range( |
| 69 | + iterators.begin(), iterators.end(), proj(*it), |
| 70 | + compare, utility::indirect{} | projection |
| 71 | + ); |
| 72 | + auto pos_min = rng.first - iterators.begin(); |
| 73 | + auto pos_max = rng.second - iterators.begin(); |
| 74 | + |
| 75 | + // If *it isn't into one of its sorted positions, computed the closest one |
| 76 | + if (it_pos < pos_min) { |
| 77 | + sum_dist += pos_min - it_pos; |
| 78 | + } else if (it_pos >= pos_max) { |
| 79 | + sum_dist += it_pos - pos_max + 1; |
| 80 | + } |
| 81 | + |
| 82 | + ++it_pos; |
| 83 | + } |
| 84 | + return sum_dist; |
| 85 | + } |
| 86 | + |
| 87 | + struct spear_impl |
| 88 | + { |
| 89 | + template< |
| 90 | + typename ForwardIterable, |
| 91 | + typename Compare = std::less<>, |
| 92 | + typename Projection = utility::identity, |
| 93 | + typename = cppsort::detail::enable_if_t< |
| 94 | + is_projection_v<Projection, ForwardIterable, Compare> |
| 95 | + > |
| 96 | + > |
| 97 | + auto operator()(ForwardIterable&& iterable, |
| 98 | + Compare compare={}, Projection projection={}) const |
| 99 | + -> decltype(auto) |
| 100 | + { |
| 101 | + return spear_probe_algo(std::begin(iterable), std::end(iterable), |
| 102 | + utility::size(iterable), |
| 103 | + std::move(compare), std::move(projection)); |
| 104 | + } |
| 105 | + |
| 106 | + template< |
| 107 | + typename ForwardIterator, |
| 108 | + typename Compare = std::less<>, |
| 109 | + typename Projection = utility::identity, |
| 110 | + typename = cppsort::detail::enable_if_t< |
| 111 | + is_projection_iterator_v<Projection, ForwardIterator, Compare> |
| 112 | + > |
| 113 | + > |
| 114 | + auto operator()(ForwardIterator first, ForwardIterator last, |
| 115 | + Compare compare={}, Projection projection={}) const |
| 116 | + -> decltype(auto) |
| 117 | + { |
| 118 | + auto dist = std::distance(first, last); |
| 119 | + return spear_probe_algo(std::move(first), std::move(last), dist, |
| 120 | + std::move(compare), std::move(projection)); |
| 121 | + } |
| 122 | + |
| 123 | + template<typename Integer> |
| 124 | + static constexpr auto max_for_size(Integer n) |
| 125 | + -> Integer |
| 126 | + { |
| 127 | + return n * n / 2; |
| 128 | + } |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + namespace |
| 133 | + { |
| 134 | + constexpr auto&& spear = utility::static_const< |
| 135 | + sorter_facade<detail::spear_impl> |
| 136 | + >::value; |
| 137 | + } |
| 138 | +}} |
| 139 | + |
| 140 | +#endif // CPPSORT_PROBES_SPEAR_H_ |
0 commit comments