model_physics: use entry or_insert_with pattern

This commit is contained in:
Quaternions 2025-01-15 08:45:25 -08:00
parent 870cb56dac
commit 6fa0f1c83e

View File

@ -279,14 +279,11 @@ struct EdgePool{
} }
impl EdgePool{ impl EdgePool{
fn push(&mut self,edge_ref_verts:EdgeRefVerts)->(&mut EdgeRefFaces,SubmeshEdgeId){ fn push(&mut self,edge_ref_verts:EdgeRefVerts)->(&mut EdgeRefFaces,SubmeshEdgeId){
let edge_id=if let Some(&edge_id)=self.edge_id_from_guy.get(&edge_ref_verts){ let edge_id=*self.edge_id_from_guy.entry(edge_ref_verts.clone()).or_insert_with(||{
edge_id
}else{
let edge_id=SubmeshEdgeId::new(self.edge_guys.len() as u32); let edge_id=SubmeshEdgeId::new(self.edge_guys.len() as u32);
self.edge_guys.push((edge_ref_verts.clone(),EdgeRefFaces::new())); self.edge_guys.push((edge_ref_verts,EdgeRefFaces::new()));
self.edge_id_from_guy.insert(edge_ref_verts,edge_id);
edge_id edge_id
}; });
(&mut self.edge_guys[edge_id.get() as usize].1,edge_id) (&mut self.edge_guys[edge_id.get() as usize].1,edge_id)
} }
} }
@ -322,16 +319,11 @@ impl TryFrom<&model::Mesh> for PhysicsMesh{
let mut submesh_verts=Vec::new(); let mut submesh_verts=Vec::new();
let mut submesh_vert_id_from_mesh_vert_id=HashMap::<MeshVertId,SubmeshVertId>::new(); let mut submesh_vert_id_from_mesh_vert_id=HashMap::<MeshVertId,SubmeshVertId>::new();
//lazy closure //lazy closure
let mut get_submesh_vert_id=|vert_id:MeshVertId|{ let mut get_submesh_vert_id=|vert_id:MeshVertId|*submesh_vert_id_from_mesh_vert_id.entry(vert_id).or_insert_with(||{
if let Some(&submesh_vert_id)=submesh_vert_id_from_mesh_vert_id.get(&vert_id){
submesh_vert_id
}else{
let submesh_vert_id=SubmeshVertId::new(submesh_verts.len() as u32); let submesh_vert_id=SubmeshVertId::new(submesh_verts.len() as u32);
submesh_verts.push(vert_id); submesh_verts.push(vert_id);
submesh_vert_id_from_mesh_vert_id.insert(vert_id,submesh_vert_id);
submesh_vert_id submesh_vert_id
} });
};
let mut edge_pool=EdgePool::default(); let mut edge_pool=EdgePool::default();
let mut vert_ref_guys=vec![VertRefGuy::default();mesh.unique_pos.len()]; let mut vert_ref_guys=vec![VertRefGuy::default();mesh.unique_pos.len()];
let mut face_ref_guys=Vec::new(); let mut face_ref_guys=Vec::new();