From 0b8fa61da797c222f365e2aacff9bbbf5f2ddde2 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Tue, 13 Oct 2020 08:38:22 +0200 Subject: [PATCH] added MovExt to avoid code duplication --- movext.hh | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/movext.hh b/movext.hh index 4fffbd6..9e7fc37 100644 --- a/movext.hh +++ b/movext.hh @@ -10,21 +10,23 @@ Copyright 2020 Ahmet Inan namespace DSP { -template -class MovMin +template +class MovExt { Deque window, dispenser, refill; + EQUAL equal; + COMP comp; public: TYPE operator () (TYPE input) { if (window.full()) { - if (window.front() == dispenser.front()) + if (equal(window.front(), dispenser.front())) dispenser.pop_front(); window.pop_front(); } window.push_back(input); - while (!refill.empty() && input < refill.front()) + while (!refill.empty() && comp(input, refill.front())) refill.pop_front(); refill.push_front(input); @@ -35,36 +37,33 @@ public: } return dispenser.front(); } - return dispenser.front() < refill.back() ? dispenser.front() : refill.back(); + return comp(dispenser.front(), refill.back()) ? dispenser.front() : refill.back(); + } +}; + +template +class MovMin +{ + struct Equal { bool operator () (TYPE a, TYPE b) { return a == b; } }; + struct Less { bool operator () (TYPE a, TYPE b) { return a < b; } }; + MovExt movmin; +public: + TYPE operator () (TYPE input) + { + return movmin(input); } }; template class MovMax { - Deque window, dispenser, refill; + struct Equal { bool operator () (TYPE a, TYPE b) { return a == b; } }; + struct Greater { bool operator () (TYPE a, TYPE b) { return a > b; } }; + MovExt movmax; public: TYPE operator () (TYPE input) { - if (window.full()) { - if (window.front() == dispenser.front()) - dispenser.pop_front(); - window.pop_front(); - } - window.push_back(input); - - while (!refill.empty() && input > refill.front()) - refill.pop_front(); - refill.push_front(input); - - if (dispenser.empty()) { - while (!refill.empty()) { - dispenser.push_front(refill.front()); - refill.pop_front(); - } - return dispenser.front(); - } - return dispenser.front() > refill.back() ? dispenser.front() : refill.back(); + return movmax(input); } };