1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use std::collections::HashMap; use information::InformationAssociator; use information::InformationProvider; use information::InformationOrganizer; pub struct HashMapGraphInformation<TInformation> { information: HashMap<usize, TInformation>, } impl <TInformation> HashMapGraphInformation<TInformation> { pub fn new() -> Self { HashMapGraphInformation{ information: HashMap::<usize, TInformation>::new() } } fn take_information_option(&mut self, vertex_id: usize) -> Option<TInformation> { self.information.remove(&vertex_id) } } impl <TInformation> InformationAssociator<TInformation> for HashMapGraphInformation<TInformation> { fn add_information(&mut self, vertex_id: usize, information: TInformation) { self.information.insert(vertex_id, information); } fn remove_information(&mut self, vertex_id: usize) { self.information.remove(&vertex_id); } } impl <TInformation> InformationProvider<TInformation> for HashMapGraphInformation<TInformation> { fn get_all_information(&self) -> Vec<&TInformation> { let information = &self.information; information .values() .collect() } fn get_all_information_mut<'a>(&'a mut self) -> Vec<&'a mut TInformation> { let information = &mut self.information; information .iter_mut() .map( |item| { let (_, value) = item; value } ) .collect() } fn get_information(&self, vertex_id: usize) -> &TInformation { self.information.get(&vertex_id).unwrap() } fn get_information_mut<'a>(&'a mut self, vertex_id: usize) -> &'a mut TInformation { self.information.get_mut(&vertex_id).unwrap() } } impl <TInformation> InformationOrganizer for HashMapGraphInformation<TInformation> { fn reorganize_information(&mut self, new_vertex_id_layout: &Vec<usize>) { let mut new_information = HashMap::<usize, TInformation>::new(); let old_vertex_ids: Vec<usize> = self.information.keys().cloned().collect(); for old_vertex_id in old_vertex_ids { let information_option = self.take_information_option(old_vertex_id); if let Some(information) = information_option { let new_vertex_id = new_vertex_id_layout[old_vertex_id]; new_information.insert(new_vertex_id, information); } } self.information = new_information } }