MCPcopy Create free account
hub / github.com/QMHTMY/RustBook / remove_vertex

Method remove_vertex

publication/code/chapter09/graph_adjlist.rs:114–134  ·  view source on GitHub ↗

删除点 (同时要删除边)

(&mut self, key: &T)

Source from the content-addressed store, hash-verified

112
113 // 删除点 (同时要删除边)
114 fn remove_vertex(&mut self, key: &T) -> Option<Vertex<T>> {
115 let old_vertex = self.vertices.remove(key);
116 self.vertnums -= 1;
117
118 // 删除从当前点出发的边
119 self.edgenums -= old_vertex.clone()
120 .unwrap()
121 .get_neighbors()
122 .len() as u32;
123 // 删除到当前点的边
124 for vertex in self.vertex_keys() {
125 if let Some(vt) = self.vertices.get_mut(&vertex) {
126 if vt.adjacent_key(key) {
127 vt.neighbors.retain(|(k, _)| k != key);
128 self.edgenums -= 1;
129 }
130 }
131 }
132
133 old_vertex
134 }
135
136 fn add_edge(&mut self, from: &T, to: &T, wt: i32) {
137 // 若点不存在要先添加点

Callers 1

mainFunction · 0.45

Calls 6

removeMethod · 0.45
lenMethod · 0.45
get_neighborsMethod · 0.45
vertex_keysMethod · 0.45
get_mutMethod · 0.45
adjacent_keyMethod · 0.45

Tested by

no test coverage detected