1#[derive(Debug)]
2struct Child<T> {
3 id: T,
4 children: Vec<usize>,
5}
6
7impl<T> Child<T> {
8 fn new(id: T) -> Self {
9 Child {
10 id,
11 children: vec![],
12 }
13 }
14}
15
16#[derive(Debug)]
17pub(crate) struct ChildGraph<T>(Vec<Child<T>>);
18
19impl<T> ChildGraph<T>
20where
21 T: Sized + PartialEq + Clone,
22{
23 pub(crate) fn with_capacity(s: usize) -> Self {
24 ChildGraph(Vec::with_capacity(s))
25 }
26
27 pub(crate) fn insert(&mut self, req: T) -> usize {
28 self.0.iter().position(|e| e.id == req).unwrap_or_else(|| {
29 let idx = self.0.len();
30 self.0.push(Child::new(req));
31 idx
32 })
33 }
34
35 pub(crate) fn insert_child(&mut self, parent: usize, child: T) -> usize {
36 let c_idx = self.0.len();
37 self.0.push(Child::new(child));
38 self.0[parent].children.push(c_idx);
39 c_idx
40 }
41
42 pub(crate) fn iter(&self) -> impl Iterator<Item = &T> {
43 self.0.iter().map(|r| &r.id)
44 }
45
46 pub(crate) fn contains(&self, req: &T) -> bool {
47 self.0.iter().any(|r| r.id == *req)
48 }
49}
50