Allelic manipulations
This commit is contained in:
parent
aa27328286
commit
6be5d415ab
6 changed files with 439 additions and 0 deletions
|
|
@ -1,3 +1,5 @@
|
|||
use std::{collections::VecDeque, fmt::Display};
|
||||
|
||||
use crate::genetics::gene::Gene;
|
||||
|
||||
pub struct PlantGenome {
|
||||
|
|
@ -33,6 +35,49 @@ 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].place.clone()));
|
||||
|
||||
for &next in &adj[node] {
|
||||
if !visited[next] {
|
||||
visited[next] = true;
|
||||
queue.push_back((next, depth + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result
|
||||
};
|
||||
|
||||
for (depth,name) in indents {
|
||||
let indent = "____".repeat(depth);
|
||||
writeln!(f,"{}{}",indent,name)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum PlantCreationError {
|
||||
EmptyNodes,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue