Skip to main content

blossom/
forest.rs

1use std::collections::HashMap;
2
3use crate::Vertex;
4
5pub struct Forest {
6    trees: HashMap<Vertex, Vec<Vertex>>,
7}
8
9impl Forest {
10    pub fn new() -> Forest {
11        Forest {
12            trees: HashMap::new(),
13        }
14    }
15
16    pub fn from_singletons(vertices: &[Vertex]) -> Forest {
17        Forest {
18            trees: vertices.iter().map(|&x| (x, vec![x])).collect(),
19        }
20    }
21
22    pub fn depth(&self, vertex: Vertex) -> Option<usize> {
23        self.trees.get(&vertex).map(|p| p.len())
24    }
25
26    pub fn find_path(&self, vertex: Vertex) -> Option<&[Vertex]> {
27        self.trees.get(&vertex).map(|p| &p[..])
28    }
29
30    pub fn push(&mut self, v: Vertex, p: Vec<Vertex>) {
31        self.trees.insert(v, p);
32    }
33
34    pub fn append(&mut self, other: &mut Forest) {
35        for (v, p) in other.trees.drain() {
36            self.trees.insert(v, p);
37        }
38    }
39}