From ebaf804f19110f1cbdc8d8999dcd6848ec30c034 Mon Sep 17 00:00:00 2001 From: Nick Drozd Date: Sun, 15 Sep 2024 11:26:08 -0400 Subject: [PATCH] Cut block reuse --- src/tape.rs | 71 +++++++++++++++-------------------------------------- 1 file changed, 20 insertions(+), 51 deletions(-) diff --git a/src/tape.rs b/src/tape.rs index a55ef509..c63e2e58 100644 --- a/src/tape.rs +++ b/src/tape.rs @@ -15,17 +15,12 @@ pub trait Block: Display { fn new(color: Color, count: Count) -> Self; fn get_color(&self) -> Color; - fn set_color(&mut self, color: Color); fn get_count(&self) -> Count; fn set_count(&mut self, count: Count); fn add_count(&mut self, count: Count); - fn inc_count(&mut self) { - self.add_count(1); - } - fn dec_count(&mut self); fn show(&self, f: &mut Formatter) -> Result { @@ -58,10 +53,6 @@ impl Block for BasicBlock { self.color } - fn set_color(&mut self, color: Color) { - self.color = color; - } - fn get_count(&self) -> Count { self.count } @@ -137,18 +128,12 @@ impl Span { self.0.iter().map(ToString::to_string) } - fn pull( - &mut self, - scan: Color, - skip: bool, - ) -> (Color, Count, Option) { - let mut push_block = + fn pull(&mut self, scan: Color, skip: bool) -> (Color, Count) { + let stepped = (skip && !self.blank() && self.0[0].get_color() == scan) - .then(|| self.0.remove(0)); - - let stepped = push_block - .as_ref() - .map_or_else(|| 1, |block| 1 + block.get_count()); + .then(|| self.0.remove(0)) + .as_ref() + .map_or_else(|| 1, |block| 1 + block.get_count()); let next_scan = if self.blank() { 0 @@ -160,39 +145,28 @@ impl Span { if next_pull.get_count() > 1 { next_pull.dec_count(); } else { - let mut popped = self.0.remove(0); - - if push_block.is_none() { - popped.set_count(0); - push_block = Some(popped); - } + self.0.remove(0); } pull_color }; - (next_scan, stepped, push_block) + (next_scan, stepped) } - fn push( - &mut self, - print: Color, - stepped: Count, - mut push_block: Option, - ) { - if !self.blank() && self.0[0].get_color() == print { - self.0[0].add_count(stepped); - } else if !self.blank() || print != 0 { - if let Some(block) = &mut push_block { - block.set_color(print); - block.inc_count(); - } else { - push_block = Some(Block::new(print, 1)); + fn push(&mut self, print: Color, stepped: Count) { + if self.blank() { + if print != 0 { + self.0.insert(0, Block::new(print, stepped)); } - if let Some(block) = push_block { - self.0.insert(0, block); - } + return; + } + + if self.0[0].get_color() == print { + self.0[0].add_count(stepped); + } else { + self.0.insert(0, Block::new(print, stepped)); } } } @@ -372,10 +346,9 @@ impl Tape { (&mut self.lspan, &mut self.rspan) }; - let (next_scan, stepped, push_block) = - pull.pull(self.scan, skip); + let (next_scan, stepped) = pull.pull(self.scan, skip); - push.push(color, stepped, push_block); + push.push(color, stepped); self.scan = next_scan; @@ -584,10 +557,6 @@ impl Block for EnumBlock { self.color } - fn set_color(&mut self, color: Color) { - self.color = color; - } - fn get_count(&self) -> Count { self.count }