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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
pub mod space; pub mod state; mod tests; pub use self::space::*; pub use self::state::*; use error::*; use id::*; use petgraph::algo::astar; use petgraph::graphmap::UnGraphMap; use std::collections::HashMap; #[derive(Debug)] pub struct QDF<S> where S: State, { id: ID, graph: UnGraphMap<ID, ()>, spaces: HashMap<ID, Space<S>>, root: ID, dimensions: usize, } impl<S> QDF<S> where S: State, { pub fn new(dimensions: usize, root_state: S) -> Self { let mut graph = UnGraphMap::new(); let mut spaces = HashMap::new(); let id = ID::new(); graph.add_node(id); spaces.insert(id, Space::with_id(id, root_state)); Self { id: ID::new(), graph, spaces, root: id, dimensions, } } #[inline] pub fn id(&self) -> ID { self.id } #[inline] pub fn root(&self) -> ID { self.root } #[inline] pub fn dimensions(&self) -> usize { self.dimensions } #[inline] pub fn state(&self) -> &S { self.spaces[&self.root].state() } #[inline] pub fn space_exists(&self, id: ID) -> bool { self.spaces.contains_key(&id) } #[inline] pub fn try_get_space(&self, id: ID) -> Option<&Space<S>> { self.spaces.get(&id) } #[inline] pub fn get_space(&self, id: ID) -> Result<&Space<S>> { if let Some(space) = self.spaces.get(&id) { Ok(space) } else { Err(QDFError::SpaceDoesNotExists(id)) } } #[inline] pub fn space(&self, id: ID) -> &Space<S> { &self.spaces[&id] } #[inline] pub fn try_set_space_state(&mut self, id: ID, state: S) -> bool { self.set_space_state(id, state).is_ok() } #[inline] pub fn set_space_state(&mut self, id: ID, state: S) -> Result<()> { if self.space_exists(id) { let substate = state.subdivide(self.dimensions + 1); let mut space = self.spaces[&id].clone(); space.apply_state(state); for s in space.subspace() { self.set_space_state(*s, substate.clone())?; } let mut parent = space.parent(); self.spaces.insert(id, space); while parent.is_some() { parent = self.recalculate_state(parent.unwrap()); } Ok(()) } else { Err(QDFError::SpaceDoesNotExists(id)) } } #[inline] pub fn find_space_neighbors(&self, id: ID) -> Result<Vec<ID>> { if self.graph.contains_node(id) { Ok(self.graph.neighbors(id).collect()) } else { Err(QDFError::SpaceDoesNotExists(id)) } } pub fn find_path(&self, from: ID, to: ID) -> Result<Vec<ID>> { if !self.space_exists(from) { return Err(QDFError::SpaceDoesNotExists(from)); } if !self.space_exists(to) { return Err(QDFError::SpaceDoesNotExists(to)); } if let Some((_, spaces)) = astar(&self.graph, from, |f| f == to, |_| 0, |_| 0) { Ok(spaces) } else { Ok(vec![]) } } pub fn increase_space_density(&mut self, id: ID) -> Result<()> { if self.space_exists(id) { let mut space = self.spaces[&id].clone(); if !space.is_platonic() { for s in space.subspace() { self.increase_space_density(*s)?; } } else { let subs = self.dimensions + 1; let substate = space.state().subdivide(subs); let spaces = (0..subs) .map(|_| Space::with_id_parent_state(ID::new(), id, substate.clone())) .collect::<Vec<Space<S>>>(); let subspace = spaces.iter().map(|s| s.id()).collect::<Vec<ID>>(); for s in spaces { let id = s.id(); self.spaces.insert(id, s); self.graph.add_node(id); } for a in &subspace { for b in &subspace { if a != b { self.graph.add_edge(*a, *b, ()); } } } let neighbors = self.graph.neighbors(id).collect::<Vec<ID>>(); for (i, n) in neighbors.iter().enumerate() { self.graph.remove_edge(*n, id); self.graph.add_edge(*n, subspace[i], ()); } space.apply_subspace(subspace); self.spaces.insert(id, space); } Ok(()) } else { Err(QDFError::SpaceDoesNotExists(id)) } } pub fn decrease_space_density(&mut self, id: ID) -> Result<bool> { if self.space_exists(id) { let mut space = self.spaces[&id].clone(); if space.is_platonic() { Ok(true) } else { let merge = space .subspace() .iter() .map(|id| { if self.spaces[id].is_platonic() { Ok(true) } else { self.decrease_space_density(*id) } }).collect::<Result<Vec<bool>>>()? .iter() .all(|v| *v); if merge { let neighbors = space .subspace() .iter() .flat_map(|s| self.graph.neighbors(*s).collect::<Vec<ID>>()) .filter(|s| !space.subspace().contains(s)) .collect::<Vec<ID>>(); for n in neighbors { self.graph.add_edge(id, n, ()); } for s in space.subspace() { self.graph.remove_node(*s); self.spaces.remove(s); } space.apply_subspace(vec![]); self.spaces.insert(id, space); } Ok(false) } } else { Err(QDFError::SpaceDoesNotExists(id)) } } #[inline] pub fn decrease_space_density_level(&mut self, id: ID) -> Result<()> { while !self.decrease_space_density(id)? {} Ok(()) } fn recalculate_state(&mut self, id: ID) -> Option<ID> { let mut space = self.spaces[&id].clone(); let states = space .subspace() .iter() .map(|s| self.spaces[&s].state().clone()) .collect::<Vec<S>>(); space.apply_state(Subdividable::merge(&states)); let parent = space.parent(); self.spaces.insert(id, space); parent } }