Skip to main content

vortex_utils/
iter.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Iterator extension traits.
5
6use std::convert::Infallible;
7
8/// An extension trait for iterators that provides balanced binary tree reduction.
9///
10/// Unlike [`Iterator::reduce`], which builds a left-leaning linear chain of depth N,
11/// `reduce_balanced` builds a balanced binary tree of depth log(N). This avoids deep
12/// nesting that can cause stack overflows on drop or suboptimal evaluation.
13///
14/// ```text
15/// reduce:          reduce_balanced:
16///     f                  f
17///    / \                / \
18///   f   d              f   f
19///  / \                / \ / \
20/// f   c              a  b c  d
21/// |\
22/// a b
23/// ```
24pub trait ReduceBalancedIterExt: Iterator {
25    /// Like [`Iterator::reduce`], but builds a balanced binary tree instead of a linear chain.
26    ///
27    /// `[a, b, c, d]` becomes `combine(combine(a, b), combine(c, d))`.
28    ///
29    /// Returns `None` if the iterator is empty.
30    fn reduce_balanced<F>(self, combine: F) -> Option<Self::Item>
31    where
32        F: Fn(Self::Item, Self::Item) -> Self::Item;
33
34    /// Fallible version of [`reduce_balanced`](ReduceBalancedIterExt::reduce_balanced).
35    ///
36    /// Short-circuits on the first error.
37    fn try_reduce_balanced<F, E>(self, combine: F) -> Result<Option<Self::Item>, E>
38    where
39        F: Fn(Self::Item, Self::Item) -> Result<Self::Item, E>;
40}
41
42impl<I: Iterator + Sized> ReduceBalancedIterExt for I {
43    fn reduce_balanced<F>(self, combine: F) -> Option<Self::Item>
44    where
45        F: Fn(Self::Item, Self::Item) -> Self::Item,
46    {
47        match self.try_reduce_balanced(|lhs, rhs| Ok::<_, Infallible>(combine(lhs, rhs))) {
48            Ok(result) => result,
49            Err(never) => match never {},
50        }
51    }
52
53    fn try_reduce_balanced<F, E>(self, combine: F) -> Result<Option<Self::Item>, E>
54    where
55        F: Fn(Self::Item, Self::Item) -> Result<Self::Item, E>,
56    {
57        let mut items: Vec<_> = self.collect();
58        if items.is_empty() {
59            return Ok(None);
60        }
61        if items.len() == 1 {
62            return Ok(items.pop());
63        }
64
65        // Each pass consumes one level of the reduction tree, combining adjacent pairs into the
66        // next level. The two vectors swap roles between passes so items can be moved rather than
67        // cloned while retaining their allocations.
68        let mut next = Vec::with_capacity(items.len() / 2);
69        while items.len() > 1 {
70            next.clear();
71            let mut iter = items.drain(..);
72            while let Some(lhs) = iter.next() {
73                if let Some(rhs) = iter.next() {
74                    next.push(combine(lhs, rhs)?);
75                } else {
76                    // Folding an odd tail into the preceding pair keeps it at the current tree
77                    // level instead of carrying it forward as a shallower subtree.
78                    let Some(previous) = next.pop() else {
79                        unreachable!("a reduction level with an odd tail has a preceding pair")
80                    };
81                    next.push(combine(previous, lhs)?);
82                }
83            }
84            drop(iter);
85            std::mem::swap(&mut items, &mut next);
86        }
87
88        assert_eq!(items.len(), 1);
89        Ok(items.pop())
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_empty() {
99        let result = std::iter::empty::<i32>().reduce_balanced(|a, b| a + b);
100        assert_eq!(result, None);
101    }
102
103    #[test]
104    fn test_single() {
105        let result = [42].into_iter().reduce_balanced(|a, b| a + b);
106        assert_eq!(result, Some(42));
107    }
108
109    #[test]
110    fn test_two() {
111        let result = [1, 2].into_iter().reduce_balanced(|a, b| a + b);
112        assert_eq!(result, Some(3));
113    }
114
115    #[test]
116    fn test_balanced_structure() {
117        // Use string concatenation to verify the tree shape.
118        // [a, b, c, d] should produce ((a+b)+(c+d)), not (((a+b)+c)+d).
119        let result = ["a", "b", "c", "d"]
120            .into_iter()
121            .map(String::from)
122            .reduce_balanced(|a, b| format!("({a}+{b})"));
123        assert_eq!(result, Some("((a+b)+(c+d))".to_string()));
124    }
125
126    #[test]
127    fn test_balanced_structure_five() {
128        // [a, b, c, d, e] => ((a+b)+((c+d)+e))
129        let result = ["a", "b", "c", "d", "e"]
130            .into_iter()
131            .map(String::from)
132            .reduce_balanced(|a, b| format!("({a}+{b})"));
133        assert_eq!(result, Some("((a+b)+((c+d)+e))".to_string()));
134    }
135
136    #[test]
137    fn test_non_clone_items() {
138        #[derive(Debug, PartialEq, Eq)]
139        struct NonClone(String);
140
141        let result = ["a", "b", "c"]
142            .into_iter()
143            .map(|value| NonClone(value.to_owned()))
144            .reduce_balanced(|NonClone(lhs), NonClone(rhs)| NonClone(format!("({lhs}+{rhs})")));
145
146        assert_eq!(result, Some(NonClone("((a+b)+c)".to_owned())));
147    }
148
149    #[test]
150    fn test_try_reduce_balanced_ok() {
151        let result: Result<_, &str> = [1, 2, 3, 4]
152            .into_iter()
153            .try_reduce_balanced(|a, b| Ok(a + b));
154        assert_eq!(result, Ok(Some(10)));
155    }
156
157    #[test]
158    fn test_try_reduce_balanced_err() {
159        let result: Result<Option<i32>, &str> = [1, 2, 3, 4]
160            .into_iter()
161            .try_reduce_balanced(|a, b| if a + b > 4 { Err("too big") } else { Ok(a + b) });
162        assert_eq!(result, Err("too big"));
163    }
164
165    #[test]
166    fn test_try_reduce_balanced_empty() {
167        let result: Result<_, &str> =
168            std::iter::empty::<i32>().try_reduce_balanced(|a, b| Ok(a + b));
169        assert_eq!(result, Ok(None));
170    }
171
172    #[test]
173    fn test_try_reduce_balanced_single() {
174        let result: Result<_, &str> = [42].into_iter().try_reduce_balanced(|a, b| Ok(a + b));
175        assert_eq!(result, Ok(Some(42)));
176    }
177}