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

Method remove_vertex

code/chapter08/graph_adjlist.rs:114–135  ·  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_connects()
122 .len() as u32;
123
124 // 删除到当前点的边
125 for vertex in self.vertex_keys() {
126 if let Some(vt) = self.vertices.get_mut(&vertex) {
127 if vt.adjacent_key(key) {
128 vt.connects.retain(|(k, _)| k != key);
129 self.edgenums -= 1;
130 }
131 }
132 }
133
134 old_vertex
135 }
136
137 fn add_edge(&mut self, from: &T, to: &T, wt: i32) {
138 // 若点不存在要先添加点

Callers 1

mainFunction · 0.45

Calls 6

get_connectsMethod · 0.80
removeMethod · 0.45
lenMethod · 0.45
vertex_keysMethod · 0.45
get_mutMethod · 0.45
adjacent_keyMethod · 0.45

Tested by

no test coverage detected