merge: add a function for padding a Merge to a given size

This will be useful for padding merge objects in `MergedTreeBuilder`
to the size of the widest override.
This commit is contained in:
Martin von Zweigbergk 2023-08-31 08:44:57 -07:00 committed by Martin von Zweigbergk
parent 772e2b8be6
commit d29c831ef9

View file

@ -215,6 +215,18 @@ impl<T> Merge<T> {
trivial_merge(&self.removes, &self.adds)
}
/// Pads this merge with to the specified number of sides with the specified
/// value. No-op if the requested size is not larger than the current size.
pub fn pad_to(&mut self, num_sides: usize, value: &T)
where
T: Clone,
{
for _ in self.num_sides()..num_sides {
self.removes.push(value.clone());
self.adds.push(value.clone());
}
}
/// Returns an iterator over references to the terms. The items will
/// alternate between positive and negative terms, starting with
/// positive (since there's one more of those).
@ -733,6 +745,16 @@ mod tests {
}
}
#[test]
fn test_pad_to() {
let mut x = c(&[], &[1]);
x.pad_to(3, &2);
assert_eq!(x, c(&[2, 2], &[1, 2, 2]));
// No change if the requested size is smaller
x.pad_to(1, &3);
assert_eq!(x, c(&[2, 2], &[1, 2, 2]));
}
#[test]
fn test_iter() {
// 1-way merge