Turn backlog into iterator

This commit is contained in:
Laurenz 2021-08-21 15:28:26 +02:00
parent a6f260ca39
commit f71c38b38d
3 changed files with 13 additions and 15 deletions

View File

@ -45,8 +45,8 @@ impl<T> Spec<T> {
} }
} }
/// Compare to whether two instances are equal when compared field-by-field /// Compares if this instance's field are equal to that of another with
/// with `f`. /// respect to `eq`.
pub fn eq_by<U, F>(&self, other: &Spec<U>, eq: F) -> bool pub fn eq_by<U, F>(&self, other: &Spec<U>, eq: F) -> bool
where where
F: Fn(&T, &U) -> bool, F: Fn(&T, &U) -> bool,

View File

@ -434,7 +434,8 @@ impl<'a> GridLayouter<'a> {
// Prepare regions. // Prepare regions.
let size = self.to_size(first); let size = self.to_size(first);
let mut regions = Regions::one(size, size, Spec::splat(true)); let mut regions = Regions::one(size, size, Spec::splat(true));
regions.backlog = rest.iter().rev().map(|&v| self.to_size(v)).collect(); regions.backlog =
rest.iter().map(|&v| self.to_size(v)).collect::<Vec<_>>().into_iter();
// Layout the row. // Layout the row.
let mut pos = Gen::zero(); let mut pos = Gen::zero();

View File

@ -1,17 +1,14 @@
use crate::geom::{Size, Spec}; use crate::geom::{Size, Spec};
/// A sequence of regions to layout into. /// A sequence of regions to layout into.
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone)]
pub struct Regions { pub struct Regions {
/// The remaining size of the current region. /// The remaining size of the current region.
pub current: Size, pub current: Size,
/// The base size for relative sizing. /// The base size for relative sizing.
pub base: Size, pub base: Size,
/// A stack of followup regions. /// An iterator of followup regions.
/// pub backlog: std::vec::IntoIter<Size>,
/// Note that this is a stack and not a queue! The size of the next region is
/// `backlog.last()`.
pub backlog: Vec<Size>,
/// The final region that is repeated once the backlog is drained. /// The final region that is repeated once the backlog is drained.
pub last: Option<Size>, pub last: Option<Size>,
/// Whether nodes should expand to fill the regions instead of shrinking to /// Whether nodes should expand to fill the regions instead of shrinking to
@ -28,7 +25,7 @@ impl Regions {
Self { Self {
current: size, current: size,
base, base,
backlog: vec![], backlog: vec![].into_iter(),
last: None, last: None,
expand, expand,
} }
@ -39,7 +36,7 @@ impl Regions {
Self { Self {
current: size, current: size,
base, base,
backlog: vec![], backlog: vec![].into_iter(),
last: Some(size), last: Some(size),
expand, expand,
} }
@ -59,7 +56,7 @@ impl Regions {
/// ///
/// If this is true, calling `next()` will have no effect. /// If this is true, calling `next()` will have no effect.
pub fn in_full_last(&self) -> bool { pub fn in_full_last(&self) -> bool {
self.backlog.is_empty() && self.last.map_or(true, |size| self.current == size) self.backlog.len() == 0 && self.last.map_or(true, |size| self.current == size)
} }
/// An iterator that returns pairs of `(current, base)` that are equivalent /// An iterator that returns pairs of `(current, base)` that are equivalent
@ -67,14 +64,14 @@ impl Regions {
/// until all regions are exhausted. /// until all regions are exhausted.
pub fn iter(&self) -> impl Iterator<Item = (Size, Size)> + '_ { pub fn iter(&self) -> impl Iterator<Item = (Size, Size)> + '_ {
let first = std::iter::once((self.current, self.base)); let first = std::iter::once((self.current, self.base));
let backlog = self.backlog.iter().rev(); let backlog = self.backlog.as_slice().iter();
let last = self.last.iter().cycle(); let last = self.last.iter().cycle();
first.chain(backlog.chain(last).map(|&s| (s, s))) first.chain(backlog.chain(last).map(|&s| (s, s)))
} }
/// Advance to the next region if there is any. /// Advance to the next region if there is any.
pub fn next(&mut self) { pub fn next(&mut self) {
if let Some(size) = self.backlog.pop().or(self.last) { if let Some(size) = self.backlog.next().or(self.last) {
self.current = size; self.current = size;
self.base = size; self.base = size;
} }
@ -88,6 +85,6 @@ impl Regions {
f(&mut self.current); f(&mut self.current);
f(&mut self.base); f(&mut self.base);
self.last.as_mut().map(|x| f(x)); self.last.as_mut().map(|x| f(x));
self.backlog.iter_mut().for_each(f); self.backlog.as_mut_slice().iter_mut().for_each(f);
} }
} }