Skip to content

Commit 95d8ae0

Browse files
committed
Remove quadratic fallback of probe::osc
1 parent 4dd7bab commit 95d8ae0

File tree

3 files changed

+4
-65
lines changed

3 files changed

+4
-65
lines changed

docs/Measures-of-presortedness.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,11 @@ Computes the *Oscillation* measure described by C. Levcopoulos and O. Petersson
231231
| Complexity | Memory | Iterators |
232232
| ----------- | ----------- | ------------- |
233233
| n log n | n | Forward |
234-
|| 1 | Forward |
235-
236-
When there isn't enough extra memory available, `probe::osc` falls back to an in-place O(n²) algorithm.
237234

238235
`max_for_size`: (|*X*| * (|*X*| - 2) - 1) / 2 when the values in *X* are strongly oscillating.
239236

240237
**Note:** *Osc* does not respect Mannila's criterion 5: $Osc(\langle 2, 4, 1, 3, 1, 3 \rangle) \not \le |\langle 4, 1, 3, 1, 3 \rangle| + Osc(\langle 4, 1, 3, 1, 3 \rangle)$, though it is possible that it only happens when equivalent elements are involved.
241238

242-
***WARNING:** the O(n²) fallback of `probe::osc` is deprecated since version 1.12.0 and removed in version 2.0.0.*
243-
244239
*Changed in version 1.12.0:* `probe::osc` is now O(n log n) instead of O(n²) but now also requires O(n) memory. The O(n²) is kept for backward compatibility but will be removed in the future.
245240

246241
### *Rem*

include/cpp-sort/probes/osc.h

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
////////////////////////////////////////////////////////////
99
// Headers
1010
////////////////////////////////////////////////////////////
11-
#include <algorithm>
1211
#include <functional>
1312
#include <iterator>
14-
#include <new>
1513
#include <numeric>
1614
#include <utility>
1715
#include <vector>
@@ -33,44 +31,9 @@ namespace probe
3331
namespace detail
3432
{
3533
template<typename ForwardIterator, typename Compare, typename Projection>
36-
auto inplace_osc_algo(ForwardIterator first, ForwardIterator last,
37-
cppsort::detail::difference_type_t<ForwardIterator> size,
38-
Compare compare, Projection projection)
39-
-> ::cppsort::detail::difference_type_t<ForwardIterator>
40-
{
41-
// Deprecated in-place O(n^2) algorithm
42-
43-
using difference_type = cppsort::detail::difference_type_t<ForwardIterator>;
44-
auto&& comp = utility::as_function(compare);
45-
auto&& proj = utility::as_function(projection);
46-
47-
if (size < 2) {
48-
return 0;
49-
}
50-
51-
difference_type count = 0;
52-
for (auto it = first; it != last; ++it) {
53-
auto&& value = proj(*it);
54-
55-
auto current = first;
56-
auto next = std::next(first);
57-
58-
while (next != last) {
59-
if (comp((std::min)(proj(*current), proj(*next), comp), value) &&
60-
comp(value, (std::max)(proj(*current), proj(*next), comp))) {
61-
++count;
62-
}
63-
++current;
64-
++next;
65-
}
66-
}
67-
return count;
68-
}
69-
70-
template<typename ForwardIterator, typename Compare, typename Projection>
71-
auto allocating_osc_algo(ForwardIterator first, ForwardIterator last,
72-
cppsort::detail::difference_type_t<ForwardIterator> size,
73-
Compare compare, Projection projection)
34+
auto osc_algo(ForwardIterator first, ForwardIterator last,
35+
cppsort::detail::difference_type_t<ForwardIterator> size,
36+
Compare compare, Projection projection)
7437
-> ::cppsort::detail::difference_type_t<ForwardIterator>
7538
{
7639
using difference_type = ::cppsort::detail::difference_type_t<ForwardIterator>;
@@ -155,22 +118,6 @@ namespace probe
155118
return std::accumulate(cross.begin(), cross.end(), difference_type(0));
156119
}
157120

158-
template<typename ForwardIterator, typename Compare, typename Projection>
159-
auto osc_algo(ForwardIterator first, ForwardIterator last,
160-
cppsort::detail::difference_type_t<ForwardIterator> size,
161-
Compare compare, Projection projection)
162-
-> ::cppsort::detail::difference_type_t<ForwardIterator>
163-
{
164-
try {
165-
return allocating_osc_algo(first, last, size, compare, projection);
166-
} catch (std::bad_alloc&) {
167-
return inplace_osc_algo(
168-
first, last, size,
169-
std::move(compare), std::move(projection)
170-
);
171-
}
172-
}
173-
174121
struct osc_impl
175122
{
176123
template<

tests/probes/every_probe_heap_memory_exhaustion.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020-2022 Morwenn
2+
* Copyright (c) 2020-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#include <algorithm>
@@ -22,7 +22,6 @@
2222
TEMPLATE_TEST_CASE( "heap exhaustion for random-access probes", "[probe][heap_exhaustion]",
2323
decltype(cppsort::probe::dis),
2424
decltype(cppsort::probe::mono),
25-
decltype(cppsort::probe::osc),
2625
decltype(cppsort::probe::runs) )
2726
{
2827
std::vector<int> collection; collection.reserve(491);
@@ -41,7 +40,6 @@ TEMPLATE_TEST_CASE( "heap exhaustion for random-access probes", "[probe][heap_ex
4140
TEMPLATE_TEST_CASE( "heap exhaustion for bidirectional probes", "[probe][heap_exhaustion]",
4241
decltype(cppsort::probe::dis),
4342
decltype(cppsort::probe::mono),
44-
decltype(cppsort::probe::osc),
4543
decltype(cppsort::probe::runs) )
4644
{
4745
std::list<int> collection;
@@ -60,7 +58,6 @@ TEMPLATE_TEST_CASE( "heap exhaustion for bidirectional probes", "[probe][heap_ex
6058
TEMPLATE_TEST_CASE( "heap exhaustion for forward probes", "[probe][heap_exhaustion]",
6159
decltype(cppsort::probe::dis),
6260
decltype(cppsort::probe::mono),
63-
decltype(cppsort::probe::osc),
6461
decltype(cppsort::probe::runs) )
6562
{
6663
std::forward_list<int> collection;

0 commit comments

Comments
 (0)