mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Turn backlog into iterator
This commit is contained in:
parent
a6f260ca39
commit
f71c38b38d
@ -45,8 +45,8 @@ impl<T> Spec<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Compare to whether two instances are equal when compared field-by-field
|
||||
/// with `f`.
|
||||
/// Compares if this instance's field are equal to that of another with
|
||||
/// respect to `eq`.
|
||||
pub fn eq_by<U, F>(&self, other: &Spec<U>, eq: F) -> bool
|
||||
where
|
||||
F: Fn(&T, &U) -> bool,
|
||||
|
@ -434,7 +434,8 @@ impl<'a> GridLayouter<'a> {
|
||||
// Prepare regions.
|
||||
let size = self.to_size(first);
|
||||
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.
|
||||
let mut pos = Gen::zero();
|
||||
|
@ -1,17 +1,14 @@
|
||||
use crate::geom::{Size, Spec};
|
||||
|
||||
/// A sequence of regions to layout into.
|
||||
#[derive(Debug, Clone, Eq, PartialEq)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Regions {
|
||||
/// The remaining size of the current region.
|
||||
pub current: Size,
|
||||
/// The base size for relative sizing.
|
||||
pub base: Size,
|
||||
/// A stack of followup regions.
|
||||
///
|
||||
/// Note that this is a stack and not a queue! The size of the next region is
|
||||
/// `backlog.last()`.
|
||||
pub backlog: Vec<Size>,
|
||||
/// An iterator of followup regions.
|
||||
pub backlog: std::vec::IntoIter<Size>,
|
||||
/// The final region that is repeated once the backlog is drained.
|
||||
pub last: Option<Size>,
|
||||
/// Whether nodes should expand to fill the regions instead of shrinking to
|
||||
@ -28,7 +25,7 @@ impl Regions {
|
||||
Self {
|
||||
current: size,
|
||||
base,
|
||||
backlog: vec![],
|
||||
backlog: vec![].into_iter(),
|
||||
last: None,
|
||||
expand,
|
||||
}
|
||||
@ -39,7 +36,7 @@ impl Regions {
|
||||
Self {
|
||||
current: size,
|
||||
base,
|
||||
backlog: vec![],
|
||||
backlog: vec![].into_iter(),
|
||||
last: Some(size),
|
||||
expand,
|
||||
}
|
||||
@ -59,7 +56,7 @@ impl Regions {
|
||||
///
|
||||
/// If this is true, calling `next()` will have no effect.
|
||||
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
|
||||
@ -67,14 +64,14 @@ impl Regions {
|
||||
/// until all regions are exhausted.
|
||||
pub fn iter(&self) -> impl Iterator<Item = (Size, Size)> + '_ {
|
||||
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();
|
||||
first.chain(backlog.chain(last).map(|&s| (s, s)))
|
||||
}
|
||||
|
||||
/// Advance to the next region if there is any.
|
||||
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.base = size;
|
||||
}
|
||||
@ -88,6 +85,6 @@ impl Regions {
|
||||
f(&mut self.current);
|
||||
f(&mut self.base);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user