pavan51 commented on code in PR #23827:
URL: https://github.com/apache/datafusion/pull/23827#discussion_r3643933874


##########
datafusion/functions-aggregate/src/min_max.rs:
##########
@@ -790,72 +790,55 @@ impl<T: Clone + PartialOrd> MovingMin<T> {
     #[inline]
     pub fn with_capacity(capacity: usize) -> Self {
         Self {
-            push_stack: Vec::with_capacity(capacity),
-            pop_stack: Vec::with_capacity(capacity),
+            fifo: std::collections::VecDeque::with_capacity(capacity),
+            deque: std::collections::VecDeque::with_capacity(capacity),
         }
     }
 
     /// Returns the minimum of the sliding window or `None` if the window is
     /// empty.
     #[inline]
     pub fn min(&self) -> Option<&T> {
-        match (self.push_stack.last(), self.pop_stack.last()) {
-            (None, None) => None,
-            (Some((_, min)), None) => Some(min),
-            (None, Some((_, min))) => Some(min),
-            (Some((_, a)), Some((_, b))) => Some(if a < b { a } else { b }),
-        }
+        self.deque.front()
     }
 
     /// Pushes a new element into the sliding window.
     #[inline]
     pub fn push(&mut self, val: T) {
-        self.push_stack.push(match self.push_stack.last() {
-            Some((_, min)) => {
-                if val > *min {
-                    (val, min.clone())
-                } else {
-                    (val.clone(), val)
-                }
-            }
-            None => (val.clone(), val),
-        });
+        self.fifo.push_back(val.clone());
+        while self.deque.back().map_or(false, |back_val| *back_val > val) {
+            self.deque.pop_back();
+        }
+        self.deque.push_back(val);
     }
 
     /// Removes and returns the last value of the sliding window.
     #[inline]
     pub fn pop(&mut self) -> Option<T> {

Review Comment:
   I have refactored the implementation to use this sequence number approach. 
The fifo queue has been removed entirely, and  pop() now returns (), which 
significantly simplified the code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to