use std::collections::BTreeMap;
use std::ops::Range;
#[cfg(feature = "coloring")]
use std::sync::Arc;
use crate::column::Column;
use crate::column::ColumnsExt;
#[cfg(feature = "coloring")]
use crate::pipeline::coloring;
use crate::pipeline::types::Ancestor;
use crate::pipeline::types::DashStyle;
use crate::pipeline::types::GraphRowShape;
use crate::pipeline::types::GraphRowShapeOptions;
use crate::pipeline::types::LinkLine;
use crate::pipeline::types::NodeLine;
use crate::pipeline::types::PadLine;
pub struct GraphRowShaper<N> {
columns: Vec<Column<N>>,
options: GraphRowShapeOptions,
previous_node_column: Option<usize>,
#[cfg(feature = "coloring")]
coloring: coloring::ColoringState,
#[cfg(feature = "coloring")]
line_id_allocator: Option<Arc<coloring::LineIdAllocator<N>>>,
}
impl<N> GraphRowShaper<N>
where
N: Clone + Eq,
{
pub fn new() -> Self {
Self::with_options(Default::default())
}
pub fn with_options(options: GraphRowShapeOptions) -> Self {
Self {
columns: Vec::new(),
options,
previous_node_column: None,
#[cfg(feature = "coloring")]
coloring: Default::default(),
#[cfg(feature = "coloring")]
line_id_allocator: None,
}
}
#[cfg(feature = "coloring")]
pub fn with_line_id_allocator(
mut self,
allocator: Option<Arc<coloring::LineIdAllocator<N>>>,
) -> Self {
self.line_id_allocator = allocator;
self
}
pub fn options(&self) -> &GraphRowShapeOptions {
&self.options
}
pub fn options_mut(&mut self) -> &mut GraphRowShapeOptions {
&mut self.options
}
pub fn reserve(&mut self, node: N) {
if self.columns.find(&node).is_none() {
if let Some(index) = self.columns.first_empty() {
self.columns[index] = Column::Reserved(node);
} else {
self.columns.push(Column::Reserved(node));
}
}
}
pub fn width(&self, node: Option<&N>, parents: Option<&Vec<Ancestor<N>>>) -> u64 {
self.width_with_options(node, parents, self.options)
}
pub fn width_with_options(
&self,
node: Option<&N>,
parents: Option<&Vec<Ancestor<N>>>,
options: GraphRowShapeOptions,
) -> u64 {
let mut width = self.columns.len();
let mut empty_columns = self
.columns
.iter()
.filter(|&column| column == &Column::Empty)
.count();
if let Some(node) = node {
if self.columns.find(node).is_none() {
if options.min_row_height <= 1 && options.stagger_consecutive_disconnected_nodes {
if let Some(previous_node_column) = self.previous_node_column {
if self.columns.get(previous_node_column) == Some(&Column::Empty) {
empty_columns = empty_columns.saturating_sub(1);
} else if previous_node_column == self.columns.len() {
width += 1;
}
}
}
if empty_columns == 0 {
width += 1;
} else {
empty_columns = empty_columns.saturating_sub(1);
}
}
}
if let Some(parents) = parents {
let unallocated_parents = parents
.iter()
.filter(|parent| {
ancestor_id(parent).is_none_or(|parent| self.columns.find(parent).is_none())
})
.count()
.saturating_sub(empty_columns);
width += unallocated_parents.saturating_sub(1);
}
width as u64
}
pub fn next_row_shape(&mut self, node: N, parents: Vec<Ancestor<N>>) -> GraphRowShape<N> {
let existing_column = self.columns.find(&node);
let column = existing_column.unwrap_or_else(|| self.find_column_for_unallocated_node());
let is_head = existing_column
.is_none_or(|index| matches!(self.columns.get(index), Some(Column::Reserved(_))));
let is_root = parents.is_empty();
#[cfg(feature = "coloring")]
let mut row_coloring = self.line_id_allocator.as_ref().map(|allocator| {
let state = &mut self.coloring;
let row = state.begin_row(self.columns.len(), column);
(allocator, state, row)
});
self.columns[column] = Column::Empty;
let merge = parents.len() > 1;
let mut node_line: Vec<_> = self.columns.iter().map(column_to_node_line).collect();
node_line[column] = NodeLine::Node;
let mut link_line: Vec<_> = self.columns.iter().map(column_to_link_line).collect();
let mut need_link_line = false;
let mut term_line: Vec<_> = self.columns.iter().map(|_| false).collect();
let mut need_term_line = false;
let mut ancestry_line: Vec<_> = self.columns.iter().map(column_to_pad_line).collect();
let mut parent_columns = BTreeMap::new();
for parent in parents.iter() {
let index =
if let Some(index) = ancestor_id(parent).and_then(|id| self.columns.find(id)) {
self.columns[index].merge(&ancestor_to_column(parent));
index
} else if let Some(index) = self.columns.find_empty(column) {
self.columns[index].merge(&ancestor_to_column(parent));
index
} else {
let index = self.columns.len();
node_line.push(NodeLine::Blank);
ancestry_line.push(PadLine::Blank);
link_line.push(LinkLine::default());
term_line.push(false);
self.columns.push(ancestor_to_column(parent));
index
};
#[cfg(feature = "coloring")]
if let Some((allocator, state, row)) = row_coloring.as_mut() {
state.add_parent(
row,
index,
|before_link, column_line_ids, allocated_line_ids| {
allocator(&coloring::LineIdContext {
node: &node,
parent,
parent_count: parents.len(),
node_column: column,
parent_column: index,
before_link,
column_line_ids,
allocated_line_ids,
})
},
);
}
parent_columns.insert(index, parent);
}
for (index, parent) in parent_columns.iter() {
if ancestor_id(parent).is_none() {
term_line[*index] = true;
need_term_line = true;
}
}
let separator_line = existing_column.is_none()
&& self.options.min_row_height <= 1
&& !self.options.stagger_consecutive_disconnected_nodes
&& Some(column) == self.previous_node_column
&& !need_term_line;
if parents.len() == 1 {
if let Some((&parent_column, _)) = parent_columns.iter().next() {
if parent_column > column {
self.columns.swap(column, parent_column);
#[cfg(feature = "coloring")]
if let Some((_, state, row)) = row_coloring.as_mut() {
state.move_parent(row, parent_column);
}
if let Some(parent) = parent_columns.remove(&parent_column) {
parent_columns.insert(column, parent);
}
let was_direct = link_line[parent_column].contains(LinkLine::VERT_PARENT);
link_line[column] |= if was_direct {
LinkLine::RIGHT_FORK_PARENT
} else {
LinkLine::RIGHT_FORK_ANCESTOR
};
#[allow(clippy::needless_range_loop)]
for i in column + 1..parent_column {
link_line[i] |= if was_direct {
LinkLine::HORIZ_PARENT
} else {
LinkLine::HORIZ_ANCESTOR
};
}
link_line[parent_column] = if was_direct {
LinkLine::LEFT_MERGE_PARENT
} else {
LinkLine::LEFT_MERGE_ANCESTOR
};
need_link_line = true;
ancestry_line[parent_column] = PadLine::Blank;
}
}
}
if let Some(bounds) = AncestorColumnBounds::new(&parent_columns, column) {
for i in bounds.range() {
link_line[i] |= bounds.horizontal_line(i);
need_link_line = true;
}
if bounds.max_parent > column {
link_line[column] |= LinkLine::RIGHT_MERGE_PARENT;
need_link_line = true;
} else if bounds.max_ancestor > column {
link_line[column] |= LinkLine::RIGHT_MERGE_ANCESTOR;
need_link_line = true;
}
if bounds.min_parent < column {
link_line[column] |= LinkLine::LEFT_MERGE_PARENT;
need_link_line = true;
} else if bounds.min_ancestor < column {
link_line[column] |= LinkLine::LEFT_MERGE_ANCESTOR;
need_link_line = true;
}
#[allow(clippy::comparison_chain)]
for (&index, parent) in parent_columns.iter() {
ancestry_line[index] = column_to_pad_line(&self.columns[index]);
if index < column {
link_line[index] |= ancestor_to_link_line(
parent,
LinkLine::RIGHT_FORK_PARENT,
LinkLine::RIGHT_FORK_ANCESTOR,
);
} else if index == column {
link_line[index] |= LinkLine::CHILD
| ancestor_to_link_line(
parent,
LinkLine::VERT_PARENT,
LinkLine::VERT_ANCESTOR,
);
} else {
link_line[index] |= ancestor_to_link_line(
parent,
LinkLine::LEFT_FORK_PARENT,
LinkLine::LEFT_FORK_ANCESTOR,
);
}
}
}
if self.options.graph_row_shape_style.dash_style == DashStyle::Once {
self.columns.convert_ancestor_to_parent();
}
self.columns.reset();
self.previous_node_column = Some(column);
let link_line = need_link_line.then_some(link_line);
#[cfg(feature = "coloring")]
let line_ids = row_coloring
.map(|(_, state, row)| state.finish_row(row, link_line.as_deref(), &self.columns));
GraphRowShape {
node,
merge,
is_head,
is_root,
style: self.options.graph_row_shape_style,
node_column: column,
parent_columns: parent_columns.keys().copied().collect(),
node_line,
link_line,
term_line: Some(term_line).filter(|_| need_term_line),
ancestry_line,
separator_line,
#[cfg(feature = "coloring")]
line_ids,
}
}
fn find_column_for_unallocated_node(&mut self) -> usize {
if self.options.min_row_height <= 1 && self.options.stagger_consecutive_disconnected_nodes {
if let Some(index) = self.columns.iter().enumerate().find_map(|(index, column)| {
(*column == Column::Empty && Some(index) != self.previous_node_column)
.then_some(index)
}) {
index
} else {
if self.previous_node_column == Some(self.columns.len()) {
self.columns.push(Column::Empty);
}
self.columns.new_empty()
}
} else {
self.columns
.first_empty()
.unwrap_or_else(|| self.columns.new_empty())
}
}
}
impl<N> Default for GraphRowShaper<N>
where
N: Clone + Eq,
{
fn default() -> Self {
Self::new()
}
}
struct AncestorColumnBounds {
target: usize,
min_ancestor: usize,
min_parent: usize,
max_parent: usize,
max_ancestor: usize,
}
impl AncestorColumnBounds {
fn new<N>(columns: &BTreeMap<usize, &Ancestor<N>>, target: usize) -> Option<Self> {
if columns.is_empty() {
return None;
}
let min_ancestor = columns
.iter()
.next()
.map_or(target, |(index, _)| *index)
.min(target);
let max_ancestor = columns
.iter()
.next_back()
.map_or(target, |(index, _)| *index)
.max(target);
let min_parent = columns
.iter()
.find(|(_, ancestor)| ancestor_is_direct(ancestor))
.map_or(target, |(index, _)| *index)
.min(target);
let max_parent = columns
.iter()
.rev()
.find(|(_, ancestor)| ancestor_is_direct(ancestor))
.map_or(target, |(index, _)| *index)
.max(target);
Some(Self {
target,
min_ancestor,
min_parent,
max_parent,
max_ancestor,
})
}
fn range(&self) -> Range<usize> {
if self.min_ancestor < self.max_ancestor {
self.min_ancestor + 1..self.max_ancestor
} else {
Default::default()
}
}
fn horizontal_line(&self, index: usize) -> LinkLine {
if index == self.target {
LinkLine::empty()
} else if index > self.min_parent && index < self.max_parent {
LinkLine::HORIZ_PARENT
} else if index > self.min_ancestor && index < self.max_ancestor {
LinkLine::HORIZ_ANCESTOR
} else {
LinkLine::empty()
}
}
}
fn ancestor_to_column<N>(ancestor: &Ancestor<N>) -> Column<N>
where
N: Clone,
{
match ancestor {
Ancestor::Ancestor(node) => Column::Ancestor(node.clone()),
Ancestor::Parent(node) => Column::Parent(node.clone()),
Ancestor::Anonymous => Column::Blocked,
}
}
fn ancestor_id<N>(ancestor: &Ancestor<N>) -> Option<&N> {
match ancestor {
Ancestor::Ancestor(node) => Some(node),
Ancestor::Parent(node) => Some(node),
Ancestor::Anonymous => None,
}
}
fn ancestor_is_direct<N>(ancestor: &Ancestor<N>) -> bool {
match ancestor {
Ancestor::Ancestor(_) => false,
Ancestor::Parent(_) => true,
Ancestor::Anonymous => true,
}
}
fn ancestor_to_link_line<N>(
ancestor: &Ancestor<N>,
direct: LinkLine,
indirect: LinkLine,
) -> LinkLine {
if ancestor_is_direct(ancestor) {
direct
} else {
indirect
}
}
fn column_to_node_line<N>(column: &Column<N>) -> NodeLine {
match column {
Column::Ancestor(_) => NodeLine::Ancestor,
Column::Parent(_) => NodeLine::Parent,
_ => NodeLine::Blank,
}
}
fn column_to_link_line<N>(column: &Column<N>) -> LinkLine {
match column {
Column::Ancestor(_) => LinkLine::VERT_ANCESTOR,
Column::Parent(_) => LinkLine::VERT_PARENT,
_ => LinkLine::empty(),
}
}
fn column_to_pad_line<N>(column: &Column<N>) -> PadLine {
match column {
Column::Ancestor(_) => PadLine::Ancestor,
Column::Parent(_) => PadLine::Parent,
_ => PadLine::Blank,
}
}