Skip to content

Commit 6060f70

Browse files
committed
Refactored Component::extendRealizeTopology to use for range loops
1 parent fbf0617 commit 6060f70

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

OpenSim/Common/Component.cpp

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,51 +1410,35 @@ getStateVariableNamesAddedByComponent() const
14101410
void Component::extendRealizeTopology(SimTK::State& s) const
14111411
{
14121412
const SimTK::Subsystem& subSys = getSystem().getDefaultSubsystem();
1413-
1414-
Component *mutableThis = const_cast<Component*>(this);
14151413

14161414
// Allocate Modeling Option
1417-
if(_namedModelingOptionInfo.size()>0){
1418-
std::map<std::string, ModelingOptionInfo>::iterator it;
1419-
for (it = (mutableThis->_namedModelingOptionInfo).begin();
1420-
it !=_namedModelingOptionInfo.end(); ++it)
1421-
{
1422-
ModelingOptionInfo& moi = it->second;
1423-
moi.index = subSys.allocateDiscreteVariable
1424-
(s, SimTK::Stage::Instance, new SimTK::Value<int>(0));
1425-
}
1415+
for (auto& kv : _namedModelingOptionInfo) {
1416+
ModelingOptionInfo& moi = kv.second;
1417+
moi.index = subSys.allocateDiscreteVariable(
1418+
s, SimTK::Stage::Instance, new SimTK::Value<int>(0));
14261419
}
14271420

14281421
// Allocate Continuous State Variables
1429-
if(_namedStateVariableInfo.size()>0){
1430-
SimTK::Vector zInit(1, 0.0);
1431-
std::map<std::string, StateVariableInfo>::iterator it;
1432-
for (it = (mutableThis->_namedStateVariableInfo).begin();
1433-
it != _namedStateVariableInfo.end(); ++it)
1434-
{
1435-
const StateVariable& sv = *it->second.stateVariable;
1436-
const AddedStateVariable* asv
1437-
= dynamic_cast<const AddedStateVariable *>(&sv);
1438-
1439-
if(asv){// add index information for added state variables
1440-
// make mutable just to update system allocated index ONLY!
1441-
AddedStateVariable* masv = const_cast<AddedStateVariable*>(asv);
1442-
masv->setVarIndex(subSys.allocateZ(s, zInit));
1443-
masv->setSubsystemIndex(getDefaultSubsystem().getMySubsystemIndex());
1444-
}
1422+
SimTK::Vector zInit(1, 0.0);
1423+
for (auto& kv : _namedStateVariableInfo) {
1424+
const StateVariable& sv = *kv.second.stateVariable;
1425+
const AddedStateVariable* asv =
1426+
dynamic_cast<const AddedStateVariable*>(&sv);
1427+
1428+
if (asv) { // add index information for added state variables
1429+
// make mutable just to update system allocated index ONLY!
1430+
AddedStateVariable* masv = const_cast<AddedStateVariable*>(asv);
1431+
masv->setVarIndex(subSys.allocateZ(s, zInit));
1432+
masv->setSubsystemIndex(
1433+
getDefaultSubsystem().getMySubsystemIndex());
14451434
}
14461435
}
14471436

14481437
// Allocate Discrete State Variables
1449-
if(_namedDiscreteVariableInfo.size()>0){
1450-
std::map<std::string, DiscreteVariableInfo>::iterator it;
1451-
for (it = (mutableThis->_namedDiscreteVariableInfo).begin();
1452-
it != _namedDiscreteVariableInfo.end(); ++it)
1453-
{
1454-
DiscreteVariableInfo& dvi = it->second;
1455-
dvi.index = subSys.allocateDiscreteVariable
1456-
(s, dvi.invalidatesStage, new SimTK::Value<double>(0.0));
1457-
}
1438+
for (auto& kv : _namedDiscreteVariableInfo) {
1439+
DiscreteVariableInfo& dvi = kv.second;
1440+
dvi.index = subSys.allocateDiscreteVariable(
1441+
s, dvi.invalidatesStage, new SimTK::Value<double>(0.0));
14581442
}
14591443

14601444
// allocate cache entry in the state

0 commit comments

Comments
 (0)