DRY: simpler removal of element from middle of std::vector

This commit is contained in:
Andrew Meadows 2018-04-03 18:28:07 -07:00
parent 0a2b4a8d1d
commit bd2bfb6807
2 changed files with 17 additions and 18 deletions

View file

@ -79,20 +79,14 @@ void PhysicalEntitySimulation::removeOwnershipData(EntityMotionState* motionStat
for (uint32_t i = 0; i < _owned.size(); ++i) { for (uint32_t i = 0; i < _owned.size(); ++i) {
if (_owned[i] == motionState) { if (_owned[i] == motionState) {
_owned[i]->clearOwnershipState(); _owned[i]->clearOwnershipState();
if (i < _owned.size() - 1) { _owned.remove(i);
_owned[i] = _owned.back();
}
_owned.pop_back();
} }
} }
} else if (motionState->getOwnershipState() == EntityMotionState::OwnershipState::PendingBid) { } else if (motionState->getOwnershipState() == EntityMotionState::OwnershipState::PendingBid) {
for (uint32_t i = 0; i < _bids.size(); ++i) { for (uint32_t i = 0; i < _bids.size(); ++i) {
if (_bids[i] == motionState) { if (_bids[i] == motionState) {
_bids[i]->clearOwnershipState(); _bids[i]->clearOwnershipState();
if (i < _bids.size() - 1) { _bids.remove(i);
_bids[i] = _bids.back();
}
_bids.pop_back();
} }
} }
} }
@ -368,10 +362,7 @@ void PhysicalEntitySimulation::sendOwnershipBids(uint32_t numSubsteps) {
_bids[i]->clearOwnershipState(); _bids[i]->clearOwnershipState();
} }
if (removeBid) { if (removeBid) {
if (i < _bids.size() - 1) { _bids.remove(i);
_bids[i] = _bids.back();
}
_bids.pop_back();
} else { } else {
if (now > _bids[i]->getNextBidExpiry()) { if (now > _bids[i]->getNextBidExpiry()) {
_bids[i]->sendBid(_entityPacketSender, numSubsteps); _bids[i]->sendBid(_entityPacketSender, numSubsteps);
@ -393,10 +384,7 @@ void PhysicalEntitySimulation::sendOwnedUpdates(uint32_t numSubsteps) {
} else { } else {
_owned[i]->clearOwnershipState(); _owned[i]->clearOwnershipState();
} }
if (i < _owned.size() - 1) { _owned.remove(i);
_owned[i] = _owned.back();
}
_owned.pop_back();
} else { } else {
if (_owned[i]->shouldSendUpdate(numSubsteps)) { if (_owned[i]->shouldSendUpdate(numSubsteps)) {
_owned[i]->sendUpdate(_entityPacketSender, numSubsteps); _owned[i]->sendUpdate(_entityPacketSender, numSubsteps);

View file

@ -27,6 +27,17 @@ class PhysicalEntitySimulation;
using PhysicalEntitySimulationPointer = std::shared_ptr<PhysicalEntitySimulation>; using PhysicalEntitySimulationPointer = std::shared_ptr<PhysicalEntitySimulation>;
using SetOfEntityMotionStates = QSet<EntityMotionState*>; using SetOfEntityMotionStates = QSet<EntityMotionState*>;
class VectorOfEntityMotionStates: public std::vector<EntityMotionState*> {
public:
void remove(uint32_t index) {
assert(index < size());
if (index < size() - 1) {
(*this)[index] = back();
}
pop_back();
}
};
class PhysicalEntitySimulation : public EntitySimulation { class PhysicalEntitySimulation : public EntitySimulation {
Q_OBJECT Q_OBJECT
public: public:
@ -89,8 +100,8 @@ private:
PhysicsEnginePointer _physicsEngine = nullptr; PhysicsEnginePointer _physicsEngine = nullptr;
EntityEditPacketSender* _entityPacketSender = nullptr; EntityEditPacketSender* _entityPacketSender = nullptr;
std::vector<EntityMotionState*> _owned; VectorOfEntityMotionStates _owned;
std::vector<EntityMotionState*> _bids; VectorOfEntityMotionStates _bids;
uint64_t _nextBidExpiry; uint64_t _nextBidExpiry;
uint32_t _lastStepSendPackets { 0 }; uint32_t _lastStepSendPackets { 0 };
}; };