From 7dcd2610f05fc651f9f5f3e9355af264ba36f1a0 Mon Sep 17 00:00:00 2001 From: PaddleStroke Date: Wed, 11 Dec 2024 09:57:04 +0100 Subject: [PATCH] AssemblyObject: Use std::unordered_set instead of vector. --- src/Mod/Assembly/App/AssemblyObject.cpp | 33 +++++++++++-------------- src/Mod/Assembly/App/AssemblyObject.h | 6 ++--- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/src/Mod/Assembly/App/AssemblyObject.cpp b/src/Mod/Assembly/App/AssemblyObject.cpp index b98b56e42c..45905162c7 100644 --- a/src/Mod/Assembly/App/AssemblyObject.cpp +++ b/src/Mod/Assembly/App/AssemblyObject.cpp @@ -159,7 +159,7 @@ int AssemblyObject::solve(bool enableRedo, bool updateJCS) objectPartMap.clear(); motions.clear(); - std::vector groundedObjs = fixGroundedParts(); + auto groundedObjs = fixGroundedParts(); if (groundedObjs.empty()) { // If no part fixed we can't solve. return -6; @@ -202,7 +202,7 @@ int AssemblyObject::generateSimulation(App::DocumentObject* sim) motions = getMotionsFromSimulation(sim); - std::vector groundedObjs = fixGroundedParts(); + auto groundedObjs = fixGroundedParts(); if (groundedObjs.empty()) { // If no part fixed we can't solve. return -6; @@ -375,7 +375,7 @@ Base::Placement AssemblyObject::getMbdPlacement(std::shared_ptr mbdPar bool AssemblyObject::validateNewPlacements() { // First we check if a grounded object has moved. It can happen that they flip. - std::vector groundedParts = getGroundedParts(); + auto groundedParts = getGroundedParts(); for (auto* obj : groundedParts) { auto* propPlacement = dynamic_cast(obj->getPropertyByName("Placement")); @@ -775,11 +775,11 @@ std::vector AssemblyObject::getJointsOfPart(App::DocumentO return jointsOf; } -std::vector AssemblyObject::getGroundedParts() +std::unordered_set AssemblyObject::getGroundedParts() { std::vector groundedJoints = getGroundedJoints(); - std::vector groundedObjs; + std::unordered_set groundedSet; for (auto gJoint : groundedJoints) { if (!gJoint) { continue; @@ -791,10 +791,7 @@ std::vector AssemblyObject::getGroundedParts() if (propObj) { App::DocumentObject* objToGround = propObj->getValue(); if (objToGround) { - if (std::find(groundedObjs.begin(), groundedObjs.end(), objToGround) - == groundedObjs.end()) { - groundedObjs.push_back(objToGround); - } + groundedSet.insert(objToGround); } } } @@ -812,21 +809,19 @@ std::vector AssemblyObject::getGroundedParts() continue; } } - if (std::find(groundedObjs.begin(), groundedObjs.end(), obj) == groundedObjs.end()) { - groundedObjs.push_back(obj); - } + groundedSet.insert(obj); } } // Origin is not in Group so we add it separately - groundedObjs.push_back(Origin.getValue()); + groundedSet.insert(Origin.getValue()); - return groundedObjs; + return groundedSet; } -std::vector AssemblyObject::fixGroundedParts() +std::unordered_set AssemblyObject::fixGroundedParts() { - std::vector groundedParts = getGroundedParts(); + auto groundedParts = getGroundedParts(); for (auto obj : groundedParts) { if (!obj) { @@ -948,7 +943,7 @@ bool AssemblyObject::isObjInSetOfObjRefs(App::DocumentObject* obj, const std::ve } void AssemblyObject::removeUnconnectedJoints(std::vector& joints, - std::vector groundedObjs) + std::unordered_set groundedObjs) { std::vector connectedParts; @@ -1040,7 +1035,7 @@ bool AssemblyObject::isPartGrounded(App::DocumentObject* obj) return false; } - std::vector groundedObjs = getGroundedParts(); + auto groundedObjs = getGroundedParts(); for (auto* groundedObj : groundedObjs) { if (groundedObj->getFullName() == obj->getFullName()) { @@ -1057,7 +1052,7 @@ bool AssemblyObject::isPartConnected(App::DocumentObject* obj) return false; } - std::vector groundedObjs = getGroundedParts(); + auto groundedObjs = getGroundedParts(); std::vector joints = getJoints(false); std::vector connectedParts; diff --git a/src/Mod/Assembly/App/AssemblyObject.h b/src/Mod/Assembly/App/AssemblyObject.h index f4c1c2589d..b8ea59049c 100644 --- a/src/Mod/Assembly/App/AssemblyObject.h +++ b/src/Mod/Assembly/App/AssemblyObject.h @@ -157,8 +157,8 @@ public: std::vector getJointsOfPart(App::DocumentObject* part); App::DocumentObject* getJointOfPartConnectingToGround(App::DocumentObject* part, std::string& name); - std::vector getGroundedParts(); - std::vector fixGroundedParts(); + std::unordered_set getGroundedParts(); + std::unordered_set fixGroundedParts(); void fixGroundedPart(App::DocumentObject* obj, Base::Placement& plc, std::string& jointName); bool isJointConnectingPartToGround(App::DocumentObject* joint, const char* partPropName); @@ -166,7 +166,7 @@ public: bool isObjInSetOfObjRefs(App::DocumentObject* obj, const std::vector& pairs); void removeUnconnectedJoints(std::vector& joints, - std::vector groundedObjs); + std::unordered_set groundedObjs); void traverseAndMarkConnectedParts(App::DocumentObject* currentPart, std::vector& connectedParts, const std::vector& joints);