From 1f05ea431ea59caf06c80a07f005c141b841e6e7 Mon Sep 17 00:00:00 2001 From: Julian Orth Date: Mon, 4 Apr 2022 23:44:25 +0200 Subject: [PATCH] autocommit 2022-04-04 23:44:25 CEST --- src/rect.rs | 2 +- src/rect/region.rs | 38 +++++++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/rect.rs b/src/rect.rs index 20a15d82..0e344be2 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -17,7 +17,7 @@ pub struct Rect { type Container = SmallVec<[Rect; 1]>; -#[derive(Default)] +#[derive(Default, Clone)] pub struct Region { rects: Container, extents: Rect, diff --git a/src/rect/region.rs b/src/rect/region.rs index b83a3700..11298dc4 100644 --- a/src/rect/region.rs +++ b/src/rect/region.rs @@ -8,43 +8,52 @@ use std::ops::Deref; use std::rc::Rc; impl Region { - pub fn new(rect: Rect) -> Self { + pub fn new(rect: Rect) -> Rc { let mut rects = SmallVec::new(); rects.push(rect); - Self { + Rc::new(Self { rects, extents: rect, - } + }) } - pub fn from_rects(rects: &[Rect]) -> Self { + pub fn from_rects(rects: &[Rect]) -> Rc { if rects.is_empty() { - return Self::default(); + return Rc::new(Self::default()); } if rects.len() == 1 { return Self::new(rects[0]); } let rects = rects_to_bands(rects); - Self { + Rc::new(Self { extents: extents(&rects), rects, - } + }) } - pub fn union(&self, other: &Self) -> Self { + pub fn union(self: &Rc, other: &Rc) -> Rc { + if self.extents.is_empty() { + return other.clone(); + } + if other.extents.is_empty() { + return self.clone(); + } let rects = op::(&self.rects, &other.rects); - Self { + Rc::new(Self { rects, extents: self.extents.union(other.extents), - } + }) } - pub fn subtract(&self, other: &Self) -> Self { + pub fn subtract(self: &Rc, other: &Rc) -> Rc { + if self.extents.is_empty() || other.extents.is_empty() { + return self.clone(); + } let rects = op::(&self.rects, &other.rects); - Self { + Rc::new(Self { extents: extents(&rects), rects, - } + }) } pub fn extents(&self) -> Rect { @@ -562,10 +571,9 @@ impl RegionBuilder { return; } let region = Region::from_rects(&self.pending); - let base = match self.op { + self.base = match self.op { BuilderOp::Add => self.base.union(®ion), BuilderOp::Sub => self.base.subtract(®ion), }; - self.base = Rc::new(base); } }