merge: load legacy Merge values without allocating intermediate buffers

This commit is contained in:
Yuya Nishihara 2023-11-03 16:40:57 +09:00
parent 09987c1d27
commit 803b41c426

View File

@ -154,16 +154,14 @@ impl<T> Merge<T> {
removes: impl IntoIterator<Item = T>, removes: impl IntoIterator<Item = T>,
adds: impl IntoIterator<Item = T>, adds: impl IntoIterator<Item = T>,
) -> Merge<Option<T>> { ) -> Merge<Option<T>> {
// TODO: no need to build intermediate removes/adds vecs let removes = removes.into_iter();
let mut removes = removes.into_iter().map(Some).collect_vec(); let mut adds = adds.into_iter().fuse();
let mut adds = adds.into_iter().map(Some).collect_vec(); let mut values = smallvec_inline![adds.next()];
while removes.len() + 1 < adds.len() { for diff in removes.zip_longest(adds) {
removes.push(None); let (remove, add) = diff.map_any(Some, Some).or_default();
values.extend([remove, add]);
} }
while adds.len() < removes.len() + 1 { Merge { values }
adds.push(None);
}
Merge::new(removes, adds)
} }
/// The removed values, also called negative terms. /// The removed values, also called negative terms.