Quicker moving median (max one call to lower_bound, we're copying the elements between the two values anyway so we might as well compare against them too) - this checks out

This commit is contained in:
Chris Cannam
2022-06-07 08:50:25 +01:00
parent 7519ef47cc
commit d7163b36f6

View File

@@ -30,6 +30,8 @@
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
//#define DEBUG_MM 1
namespace RubberBand namespace RubberBand
{ {
@@ -109,31 +111,56 @@ private:
// (and one instance of toDrop has been removed) // (and one instance of toDrop has been removed)
int n = P::m_size; int n = P::m_size;
int dropIx = std::lower_bound(m_sorted, m_sorted + n, toDrop) - m_sorted; int dropIx = std::lower_bound(m_sorted, m_sorted + n, toDrop) - m_sorted;
// if (m_sorted[dropIx] != toDrop) {
// throw std::runtime_error("not found"); #ifdef DEBUG_MM
// } std::cout << "\nbefore: [";
int putIx; for (int i = 0; i < P::m_size; ++i) {
if (i > 0) std::cout << ",";
std::cout << m_sorted[i];
}
std::cout << "]" << std::endl;
std::cout << "toDrop = " << toDrop << ", dropIx = " << dropIx << std::endl;
std::cout << "toPut = " << toPut /* << ", putIx = " << putIx */ << std::endl;
if (m_sorted[dropIx] != toDrop) {
throw std::runtime_error("element not found");
}
#endif
if (toPut > toDrop) { if (toPut > toDrop) {
putIx = std::lower_bound(m_sorted + dropIx, m_sorted + n, toPut) - m_sorted; int i = dropIx;
} else if (toPut < toDrop) { while (i+1 < n) {
putIx = std::lower_bound(m_sorted, m_sorted + dropIx, toPut) - m_sorted; if (m_sorted[i+1] > toPut) {
} else { break;
m_sorted[dropIx] = toPut;
return;
} }
if (putIx > dropIx) {
for (int i = dropIx; i+1 < putIx; ++i) {
m_sorted[i] = m_sorted[i+1]; m_sorted[i] = m_sorted[i+1];
++i;
} }
m_sorted[putIx-1] = toPut; m_sorted[i] = toPut;
} else if (putIx < dropIx) { } else if (toPut < toDrop) {
for (int i = dropIx; i > putIx; --i) { int i = dropIx;
m_sorted[i] = m_sorted[i-1]; while (i >= 0) {
--i;
if (i < 0 || m_sorted[i] < toPut) {
break;
} }
m_sorted[putIx] = toPut; m_sorted[i+1] = m_sorted[i];
} else {
m_sorted[putIx] = toPut;
} }
m_sorted[i+1] = toPut;
}
#ifdef DEBUG_MM
std::cout << "after: [";
for (int i = 0; i < P::m_size; ++i) {
if (i > 0) std::cout << ",";
std::cout << m_sorted[i];
}
std::cout << "]" << std::endl;
if (!std::is_sorted(m_sorted, m_sorted + n)) {
throw std::runtime_error("array is not sorted");
}
#endif
} }
MovingMedian(const MovingMedian &) =delete; MovingMedian(const MovingMedian &) =delete;