Genome graph rewritten to more straightforward approach
This commit is contained in:
parent
4ed86fda31
commit
b4583ff917
4 changed files with 26 additions and 37 deletions
|
|
@ -3,15 +3,13 @@ use std::{collections::VecDeque, fmt::Display};
|
|||
use crate::genetics::gene::Gene;
|
||||
|
||||
pub struct PlantGenome {
|
||||
pub(crate) genes: Vec<Gene>,
|
||||
pub(crate) edges: Vec<(usize,usize)>
|
||||
pub(crate) graph: Vec<(Gene,Vec<usize>)>,
|
||||
}
|
||||
|
||||
impl PlantGenome {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
genes: Vec::new(),
|
||||
edges: Vec::new()
|
||||
graph: Vec::new()
|
||||
}
|
||||
}
|
||||
pub fn from_edges(nodes: Vec<Gene>,edges: Vec<(usize,usize)>) -> Result<Self,PlantCreationError> {
|
||||
|
|
@ -23,9 +21,13 @@ impl PlantGenome {
|
|||
return Err(PlantCreationError::EmptyEdges);
|
||||
}
|
||||
|
||||
let mut graph: Vec<(Gene,Vec<usize>)>= nodes.iter().map(|gene|{(gene.clone(),Vec::new())}).collect();
|
||||
for edge in edges {
|
||||
graph[edge.0].1.push(edge.1);
|
||||
}
|
||||
|
||||
return Ok(Self {
|
||||
genes: nodes,
|
||||
edges
|
||||
graph
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -33,31 +35,13 @@ impl PlantGenome {
|
|||
impl Display for PlantGenome {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let indents = {
|
||||
// Vibe code
|
||||
let n = self.genes.len();
|
||||
|
||||
// Build adjacency list
|
||||
let mut adj = vec![Vec::new(); n];
|
||||
for &(u, v) in &self.edges {
|
||||
adj[u].push(v);
|
||||
adj[v].push(u); // remove if directed
|
||||
}
|
||||
|
||||
let mut visited = vec![false; n];
|
||||
let mut queue = VecDeque::new();
|
||||
let mut result = Vec::new();
|
||||
|
||||
queue.push_back((0, 0));
|
||||
visited[0] = true;
|
||||
|
||||
while let Some((node, depth)) = queue.pop_front() {
|
||||
result.push((depth, self.genes[node].clone()));
|
||||
|
||||
for &next in &adj[node] {
|
||||
if !visited[next] {
|
||||
visited[next] = true;
|
||||
queue.push_back((next, depth + 1));
|
||||
}
|
||||
let mut result: Vec<(usize,Gene)> = vec![(0,self.graph[0].0.clone())];
|
||||
let mut tree: VecDeque<(usize,&Vec<usize>)> = VecDeque::from([(0,&self.graph[0].1)]);
|
||||
while tree.is_empty() == false {
|
||||
let (depth,children) = tree.pop_front().unwrap();
|
||||
for child in children {
|
||||
result.push((depth+1,self.graph[*child].0.clone()));
|
||||
tree.push_back((depth+1,&self.graph[*child].1));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -65,7 +49,7 @@ impl Display for PlantGenome {
|
|||
};
|
||||
|
||||
for (depth,gene) in indents {
|
||||
let indent = "____".repeat(depth);
|
||||
let indent = "\t".repeat(depth);
|
||||
writeln!(f,"{}{}",indent,gene)?;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue