diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2022-09-23 20:10:11 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2022-09-23 20:10:11 +0800 |
commit | b3e374d743c40a406e383b003f7e77563e55610b (patch) | |
tree | 33819ef729541e885f7c1b1ba4046fbc5c4dbe67 | |
parent | 13c4eb0a0146a00f0dd766bf4c78ca7be93b0c41 (diff) |
make use of into when extracting Spans from Cellbuffer, consuming the source
-rw-r--r-- | packages/svgbob/src/buffer/cell_buffer.rs | 119 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/cell_buffer/span/test_span.rs | 22 | ||||
-rw-r--r-- | packages/svgbob/src/buffer/fragment_buffer/fragment/text.rs | 4 | ||||
-rw-r--r-- | packages/svgbob/src/map/circle_map.rs | 6 | ||||
-rw-r--r-- | packages/svgbob/src/map/circle_map/test_circle_map.rs | 20 |
5 files changed, 77 insertions, 94 deletions
diff --git a/packages/svgbob/src/buffer/cell_buffer.rs b/packages/svgbob/src/buffer/cell_buffer.rs index ee0e3f8..3bc3437 100644 --- a/packages/svgbob/src/buffer/cell_buffer.rs +++ b/packages/svgbob/src/buffer/cell_buffer.rs @@ -30,7 +30,7 @@ mod span; /// The simplest buffer. /// This is maps which char belong to which cell skipping the whitespaces -#[derive(Debug, Default)] +#[derive(Debug, Default, Clone)] pub struct CellBuffer { map: BTreeMap<Cell, char>, /// class, <style> @@ -66,43 +66,21 @@ impl CellBuffer { self.css_styles.extend(css_styles); } - /// Groups cell that are adjacents (cells that are next to each other, horizontally or - /// vertically) - /// Note: using .rev() since this has a high change that the last cell is adjacent with the - /// current cell tested - pub fn group_adjacents(&self) -> Vec<Span> { - let mut adjacents: Vec<Span> = vec![]; - for (cell, ch) in self.iter() { - let belongs_to_adjacents = - adjacents.iter_mut().rev().any(|contacts| { - if contacts.is_adjacent(cell) { - contacts.push((*cell, *ch)); - true + /// return the group of contacting fragments + pub fn group_contacts(self) -> (Vec<Span>, Vec<Contacts>) { + let group_adjacents: Vec<Span> = self.into(); + let (spans, contacts): (Vec<Vec<Span>>, Vec<Vec<Contacts>>) = + group_adjacents + .into_iter() + .map(|span| { + let contacts: Vec<Contacts> = span.clone().into(); + if contacts.is_empty() { + (vec![span], vec![]) } else { - false + (vec![], contacts) } - }); - if !belongs_to_adjacents { - adjacents.push(Span::new(*cell, *ch)); - } - } - Span::merge_recursive(adjacents) - } - - /// return the group of contacting fragments - pub fn group_contacts(&self) -> (Vec<Span>, Vec<Contacts>) { - let (spans, contacts): (Vec<Vec<Span>>, Vec<Vec<Contacts>>) = self - .group_adjacents() - .into_iter() - .map(|span| { - let contacts: Vec<Contacts> = span.clone().into(); - if contacts.is_empty() { - (vec![span], vec![]) - } else { - (vec![], contacts) - } - }) - .unzip(); + }) + .unzip(); let spans: Vec<Span> = spans.into_iter().flatten().collect(); let contacts: Vec<Contacts> = contacts.into_iter().flatten().collect(); @@ -124,7 +102,7 @@ impl CellBuffer { } /// get the svg node of this cell buffer, using the default settings for the sizes - pub fn get_node<MSG>(&self) -> Node<MSG> { + pub fn get_node<MSG>(self) -> Node<MSG> { let (node, _w, _h) = self.get_node_with_size(&Settings::default()); node } @@ -141,47 +119,39 @@ impl CellBuffer { /// get all nodes of this cell buffer pub fn get_node_with_size<MSG>( - &self, + self, settings: &Settings, ) -> (Node<MSG>, f32, f32) { let (w, h) = self.get_size(settings); + let legend_css = self.legend_css(); let (group_nodes, fragments) = self.group_nodes_and_fragments(settings); - let svg_node = Self::fragments_to_node( - fragments, - self.legend_css(), - settings, - w, - h, - ) - .add_children(group_nodes); + let svg_node = + Self::fragments_to_node(fragments, legend_css, settings, w, h) + .add_children(group_nodes); (svg_node, w, h) } /// get all nodes and use the size supplied pub fn get_node_override_size<MSG>( - &self, + self, settings: &Settings, w: f32, h: f32, ) -> Node<MSG> { + let legend_css = self.legend_css(); let (group_nodes, fragments) = self.group_nodes_and_fragments(settings); - let svg_node = Self::fragments_to_node( - fragments, - self.legend_css(), - settings, - w, - h, - ) - .add_children(group_nodes); + let svg_node = + Self::fragments_to_node(fragments, legend_css, settings, w, h) + .add_children(group_nodes); svg_node } /// return fragments that are Rect, Circle, - pub fn get_shapes_fragment(&self) -> Vec<FragmentSpan> { + pub fn get_shapes_fragment(self) -> Vec<FragmentSpan> { let (single_member, _, endorsed_fragments) = self.group_single_members_from_other_fragments(); endorsed_fragments @@ -194,17 +164,17 @@ impl CellBuffer { /// returns (single_member, grouped, rest of the fragments fn group_single_members_from_other_fragments( - &self, + self, ) -> (Vec<FragmentSpan>, Vec<Vec<FragmentSpan>>, Vec<FragmentSpan>) { // endorsed_fragments are the fragment result of successful endorsement // // vec_groups are not endorsed, but are still touching, these will be grouped together in // the svg node + let group_adjacents: Vec<Span> = self.into(); let (endorsed_fragments, vec_contacts): ( Vec<Vec<FragmentSpan>>, Vec<Vec<Contacts>>, - ) = self - .group_adjacents() + ) = group_adjacents .into_iter() .map(|span| span.endorse()) .unzip(); @@ -235,10 +205,10 @@ impl CellBuffer { /// return the fragments that are (close objects, touching grouped fragments) pub fn get_fragment_spans( - &self, + self, ) -> (Vec<FragmentSpan>, Vec<Vec<FragmentSpan>>) { let (single_member_fragments, vec_group_fragments, vec_fragments) = - self.group_single_members_from_other_fragments(); + self.clone().group_single_members_from_other_fragments(); let escaped_text = self.escaped_text_nodes(); let regulars = @@ -250,9 +220,10 @@ impl CellBuffer { /// group nodes that can be group and the rest will be fragments /// Note: The grouped fragments is scaled here fn group_nodes_and_fragments<MSG>( - &self, + self, settings: &Settings, ) -> (Vec<Node<MSG>>, Vec<FragmentSpan>) { + let escaped_text_nodes = self.escaped_text_nodes(); let (single_member_fragments, vec_group_fragments, vec_fragments) = self.group_single_members_from_other_fragments(); @@ -275,7 +246,7 @@ impl CellBuffer { let mut fragments: Vec<FragmentSpan> = vec_fragments; fragments.extend(single_member_fragments); - fragments.extend(self.escaped_text_nodes()); + fragments.extend(escaped_text_nodes); (group_nodes, fragments) } @@ -297,7 +268,7 @@ impl CellBuffer { classes.join("\n") } - fn get_style<MSG>(settings: &Settings, legend_css: String) -> Node<MSG> { + fn style<MSG>(settings: &Settings, legend_css: String) -> Node<MSG> { use sauron::html::units::px; let stroke_color = settings.stroke_color.to_owned(); @@ -409,7 +380,7 @@ impl CellBuffer { let mut children = vec![]; if settings.include_styles { - children.push(Self::get_style(settings, legend_css)); + children.push(Self::style(settings, legend_css)); } if settings.include_defs { children.push(Self::get_defs()); @@ -632,6 +603,18 @@ impl From<StringBuffer> for CellBuffer { } } +/// Groups cell that are adjacents (cells that are next to each other, horizontally or +/// vertically) +/// Note: using .rev() since this has a high change that the last cell is adjacent with the +/// current cell tested +impl From<CellBuffer> for Vec<Span> { + fn from(cb: CellBuffer) -> Vec<Span> { + let spans: Vec<Span> = + cb.iter().map(|(cell, ch)| Span::new(*cell, *ch)).collect(); + Span::merge_recursive(spans) + } +} + #[cfg(test)] mod tests { use super::*; @@ -709,7 +692,7 @@ mod tests { |________| "#; let buffer = CellBuffer::from(art); - let adjacents = buffer.group_adjacents(); + let adjacents: Vec<Span> = buffer.into(); for (i, span) in adjacents.iter().enumerate() { println!("span: {}", i); println!("{}\n\n", span); @@ -771,7 +754,7 @@ This is a text ' "#; let buffer = CellBuffer::from(art); - let adjacents = buffer.group_adjacents(); + let adjacents: Vec<Span> = buffer.into(); for (i, span) in adjacents.iter().enumerate() { println!("span: {}", i); println!("{}\n\n", span); @@ -794,7 +777,7 @@ This is a text "#; let buffer = CellBuffer::from(art); - let adjacents = buffer.group_adjacents(); + let adjacents: Vec<Span> = buffer.into(); for (i, span) in adjacents.iter().enumerate() { println!("span: {}", i); println!("{}\n\n", span); diff --git a/packages/svgbob/src/buffer/cell_buffer/span/test_span.rs b/packages/svgbob/src/buffer/cell_buffer/span/test_span.rs index 76aa776..0b885b0 100644 --- a/packages/svgbob/src/buffer/cell_buffer/span/test_span.rs +++ b/packages/svgbob/src/buffer/cell_buffer/span/test_span.rs @@ -14,7 +14,7 @@ fn test_bounds() { |________| "#; let buffer = CellBuffer::from(art); - let adjacents = buffer.group_adjacents(); + let adjacents: Vec<Span> = buffer.into(); assert_eq!(1, adjacents.len()); let (min, max) = adjacents[0].bounds().unwrap(); assert_eq!(min, Cell::new(0, 1)); @@ -31,7 +31,7 @@ fn test_localize() { |________| "#; let buffer = CellBuffer::from(art); - let adjacents = buffer.group_adjacents(); + let adjacents: Vec<Span> = buffer.into(); assert_eq!(1, adjacents.len()); let (min, max) = adjacents[0].bounds().unwrap(); assert_eq!(min, Cell::new(8, 4)); @@ -52,7 +52,7 @@ fn test_1span_1group() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut adjacents = buffer.group_adjacents(); + let mut adjacents: Vec<Span> = buffer.into(); println!("There are {} adjacents", adjacents.len()); assert_eq!(1, adjacents.len()); let span = adjacents.remove(0); @@ -87,7 +87,7 @@ fn test_2spans_1group_for_each_span() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut spans = buffer.group_adjacents(); + let mut spans: Vec<Span> = buffer.into(); println!("There are {} adjacents", spans.len()); assert_eq!(2, spans.len()); let span2 = spans.remove(1); @@ -137,7 +137,7 @@ fn test_1spans_2group_for_each_span() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut spans = buffer.group_adjacents(); + let mut spans: Vec<Span> = buffer.into(); println!("There are {} adjacents", spans.len()); assert_eq!(1, spans.len()); let span1 = spans.remove(0); @@ -179,7 +179,7 @@ fn test_endorse_circle() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut adjacents = buffer.group_adjacents(); + let mut adjacents: Vec<Span> = buffer.into(); println!("There are {} adjacents", adjacents.len()); assert_eq!(1, adjacents.len()); let span = adjacents.remove(0); @@ -210,7 +210,7 @@ fn test_endorse_circle_with_rect() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut adjacents = buffer.group_adjacents(); + let mut adjacents: Vec<Span> = buffer.into(); println!("There are {} adjacents", adjacents.len()); assert_eq!(1, adjacents.len()); let span1 = adjacents.remove(0); @@ -259,7 +259,7 @@ fn test_endorse_with_big_circle() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut adjacents = buffer.group_adjacents(); + let mut adjacents: Vec<Span> = buffer.into(); println!("There are {} adjacents", adjacents.len()); assert_eq!(1, adjacents.len()); let span1 = adjacents.remove(0); @@ -295,7 +295,7 @@ fn test_endorse_with_big_circle_extra_match() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut adjacents = buffer.group_adjacents(); + let mut adjacents: Vec<Span> = buffer.into(); println!("There are {} adjacents", adjacents.len()); assert_eq!(2, adjacents.len()); let span1 = adjacents.remove(0); @@ -323,7 +323,7 @@ fn test_absolute_positions() { neighbor cell "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 2); let span1 = spans.remove(1); let span0 = spans.remove(0); @@ -348,7 +348,7 @@ fn test_endorse_arc() { "#; let buffer = CellBuffer::from(art); println!("buffer: {}", buffer); - let mut adjacents = buffer.group_adjacents(); + let mut adjacents: Vec<Span> = buffer.into(); println!("There are {} adjacents", adjacents.len()); assert_eq!(1, adjacents.len()); let span = adjacents.remove(0); diff --git a/packages/svgbob/src/buffer/fragment_buffer/fragment/text.rs b/packages/svgbob/src/buffer/fragment_buffer/fragment/text.rs index f02faf5..6a61e5d 100644 --- a/packages/svgbob/src/buffer/fragment_buffer/fragment/text.rs +++ b/packages/svgbob/src/buffer/fragment_buffer/fragment/text.rs @@ -260,7 +260,7 @@ mod tests { and pneumonoultramicrospocisilicovulcanoconiosis "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let groups: Vec<Contacts> = span1.localize().into(); @@ -307,7 +307,7 @@ mod tests { neighbor cell "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 2); let groups2: Vec<Contacts> = spans.remove(1).localize().into(); let groups1: Vec<Contacts> = spans.remove(0).localize().into(); diff --git a/packages/svgbob/src/map/circle_map.rs b/packages/svgbob/src/map/circle_map.rs index b42191b..11c9e91 100644 --- a/packages/svgbob/src/map/circle_map.rs +++ b/packages/svgbob/src/map/circle_map.rs @@ -550,7 +550,7 @@ lazy_static! { pub static ref DIAMETER_CIRCLE: HashMap<i32,(Point,Span)> = HashMap::from_iter( CIRCLE_MAP.iter().map(|circle_art|{ let cb = CellBuffer::from(circle_art.ascii_art); - let mut spans = cb.group_adjacents(); + let mut spans:Vec<Span> = cb.into(); assert_eq!(spans.len(), 1); let span = spans.remove(0).localize(); (circle_art.diameter(), (circle_art.center(), span)) @@ -561,7 +561,7 @@ lazy_static! { pub static ref CIRCLES_SPAN: BTreeMap<Circle, Span> = BTreeMap::from_iter( CIRCLE_MAP.iter().map(|circle_art|{ let cb = CellBuffer::from(circle_art.ascii_art); - let mut spans = cb.group_adjacents(); + let mut spans:Vec<Span> = cb.into(); assert_eq!(spans.len(), 1); let span = spans.remove(0).localize(); (Circle::new(circle_art.center(), circle_art.radius(), false), span) @@ -744,7 +744,7 @@ fn circle_art_to_group(art: &str) -> Vec<Contacts> { fn circle_art_to_span(art: &str) -> Span { let cell_buffer = CellBuffer::from(art); - let mut spans = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); spans.remove(0).localize() } diff --git a/packages/svgbob/src/map/circle_map/test_circle_map.rs b/packages/svgbob/src/map/circle_map/test_circle_map.rs index 388f794..dd782b0 100644 --- a/packages/svgbob/src/map/circle_map/test_circle_map.rs +++ b/packages/svgbob/src/map/circle_map/test_circle_map.rs @@ -15,7 +15,7 @@ fn test_circle1() { '-.......-' "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let groups: Vec<Contacts> = span1.into(); @@ -33,7 +33,7 @@ fn test_arc9_top_right() { \ "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -47,7 +47,7 @@ fn test_arc5_top_right() { ) "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -61,7 +61,7 @@ fn test_arc5_top_left() { ( "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -75,7 +75,7 @@ fn test_arc5_bottom_left() { `- "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -89,7 +89,7 @@ fn test_arc5_bottom_right() { -' "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -107,7 +107,7 @@ fn test_arc20_top_right() { | "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -125,7 +125,7 @@ fn test_arc20_top_left() { | "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -142,7 +142,7 @@ fn test_arc20_bottom_left() { '-.... "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); @@ -159,7 +159,7 @@ fn test_arc20_bottom_right() { ....-' "#; let cell_buffer = CellBuffer::from(art); - let mut spans: Vec<Span> = cell_buffer.group_adjacents(); + let mut spans: Vec<Span> = cell_buffer.into(); assert_eq!(spans.len(), 1); let span1 = spans.remove(0); let (arc, _) = endorse_quarter_arc_span(&span1).unwrap(); |