1 #[derive(Debug)] 2 struct Child<T> { 3 id: T, 4 children: Vec<usize>, 5 } 6 7 impl<T> Child<T> { new(id: T) -> Self8 fn new(id: T) -> Self { 9 Child { 10 id, 11 children: vec![], 12 } 13 } 14 } 15 16 #[derive(Debug)] 17 pub(crate) struct ChildGraph<T>(Vec<Child<T>>); 18 19 impl<T> ChildGraph<T> 20 where 21 T: Sized + PartialEq + Clone, 22 { with_capacity(s: usize) -> Self23 pub(crate) fn with_capacity(s: usize) -> Self { 24 ChildGraph(Vec::with_capacity(s)) 25 } 26 insert(&mut self, req: T) -> usize27 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 insert_child(&mut self, parent: usize, child: T) -> usize35 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 iter(&self) -> impl Iterator<Item = &T>42 pub(crate) fn iter(&self) -> impl Iterator<Item = &T> { 43 self.0.iter().map(|r| &r.id) 44 } 45 contains(&self, req: &T) -> bool46 pub(crate) fn contains(&self, req: &T) -> bool { 47 self.0.iter().any(|r| r.id == *req) 48 } 49 } 50