Commit 88313407 authored by peastman's avatar peastman
Browse files

Use C++11 style loops

parent d314e695
...@@ -56,16 +56,16 @@ vector<SerializationNode>& SerializationNode::getChildren() { ...@@ -56,16 +56,16 @@ vector<SerializationNode>& SerializationNode::getChildren() {
} }
const SerializationNode& SerializationNode::getChildNode(const std::string& name) const { const SerializationNode& SerializationNode::getChildNode(const std::string& name) const {
for (int i = 0; i < (int) children.size(); i++) for (auto& child : children)
if (children[i].name == name) if (child.name == name)
return children[i]; return child;
throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'"); throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'");
} }
SerializationNode& SerializationNode::getChildNode(const std::string& name) { SerializationNode& SerializationNode::getChildNode(const std::string& name) {
for (int i = 0; i < (int) children.size(); i++) for (auto& child : children)
if (children[i].name == name) if (child.name == name)
return children[i]; return child;
throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'"); throw OpenMMException("Unknown child '"+name+"' for node '"+getName()+"'");
} }
......
...@@ -56,11 +56,8 @@ void StateProxy::serialize(const void* object, SerializationNode& node) const { ...@@ -56,11 +56,8 @@ void StateProxy::serialize(const void* object, SerializationNode& node) const {
if ((s.getDataTypes()&State::Parameters) != 0) { if ((s.getDataTypes()&State::Parameters) != 0) {
s.getParameters(); s.getParameters();
SerializationNode& parametersNode = node.createChildNode("Parameters"); SerializationNode& parametersNode = node.createChildNode("Parameters");
map<string, double> stateParams = s.getParameters(); for (auto& param : s.getParameters())
map<string, double>::const_iterator it; parametersNode.setDoubleProperty(param.first, param.second);
for (it = stateParams.begin(); it!=stateParams.end();it++) {
parametersNode.setDoubleProperty(it->first, it->second);
}
} }
if ((s.getDataTypes()&State::Energy) != 0) { if ((s.getDataTypes()&State::Energy) != 0) {
s.getPotentialEnergy(); s.getPotentialEnergy();
...@@ -108,17 +105,11 @@ void* StateProxy::deserialize(const SerializationNode& node) const { ...@@ -108,17 +105,11 @@ void* StateProxy::deserialize(const SerializationNode& node) const {
int types = 0; int types = 0;
vector<int> arraySizes; vector<int> arraySizes;
State::StateBuilder builder(outTime); State::StateBuilder builder(outTime);
const vector<SerializationNode>& children = node.getChildren(); for (auto& child : node.getChildren()) {
for (int j = 0; j < (int) children.size(); j++) {
const SerializationNode& child = children[j];
if (child.getName() == "Parameters") { if (child.getName() == "Parameters") {
map<string, double> outStateParams; map<string, double> outStateParams;
// inStateParams is really a <string,double> pair, where string is the name and double is the value for (auto& param : child.getProperties())
// but we want to avoid casting a string to a double and instead use the built in routines, outStateParams[param.first] = child.getDoubleProperty(param.first);
map<string, string> inStateParams = child.getProperties();
for (map<string, string>::const_iterator pit = inStateParams.begin(); pit != inStateParams.end(); pit++) {
outStateParams[pit->first] = child.getDoubleProperty(pit->first);
}
builder.setParameters(outStateParams); builder.setParameters(outStateParams);
} }
else if (child.getName() == "Energies") { else if (child.getName() == "Energies") {
...@@ -128,28 +119,22 @@ void* StateProxy::deserialize(const SerializationNode& node) const { ...@@ -128,28 +119,22 @@ void* StateProxy::deserialize(const SerializationNode& node) const {
} }
else if (child.getName() == "Positions") { else if (child.getName() == "Positions") {
vector<Vec3> outPositions; vector<Vec3> outPositions;
for (int i = 0; i < (int) child.getChildren().size(); i++) { for (auto& particle : child.getChildren())
const SerializationNode& particle = child.getChildren()[i];
outPositions.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z"))); outPositions.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
}
builder.setPositions(outPositions); builder.setPositions(outPositions);
arraySizes.push_back(outPositions.size()); arraySizes.push_back(outPositions.size());
} }
else if (child.getName() == "Velocities") { else if (child.getName() == "Velocities") {
vector<Vec3> outVelocities; vector<Vec3> outVelocities;
for (int i = 0; i < (int) child.getChildren().size(); i++) { for (auto& particle : child.getChildren())
const SerializationNode& particle = child.getChildren()[i];
outVelocities.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z"))); outVelocities.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
}
builder.setVelocities(outVelocities); builder.setVelocities(outVelocities);
arraySizes.push_back(outVelocities.size()); arraySizes.push_back(outVelocities.size());
} }
else if (child.getName() == "Forces") { else if (child.getName() == "Forces") {
vector<Vec3> outForces; vector<Vec3> outForces;
for (int i = 0; i < (int) child.getChildren().size(); i++) { for (auto& particle : child.getChildren())
const SerializationNode& particle = child.getChildren()[i];
outForces.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z"))); outForces.push_back(Vec3(particle.getDoubleProperty("x"),particle.getDoubleProperty("y"),particle.getDoubleProperty("z")));
}
builder.setForces(outForces); builder.setForces(outForces);
arraySizes.push_back(outForces.size()); arraySizes.push_back(outForces.size());
} }
......
...@@ -129,14 +129,11 @@ void* SystemProxy::deserialize(const SerializationNode& node) const { ...@@ -129,14 +129,11 @@ void* SystemProxy::deserialize(const SerializationNode& node) const {
} }
} }
const SerializationNode& constraints = node.getChildNode("Constraints"); const SerializationNode& constraints = node.getChildNode("Constraints");
for (int i = 0; i < (int) constraints.getChildren().size(); i++) { for (auto& constraint : constraints.getChildren())
const SerializationNode& constraint = constraints.getChildren()[i];
system->addConstraint(constraint.getIntProperty("p1"), constraint.getIntProperty("p2"), constraint.getDoubleProperty("d")); system->addConstraint(constraint.getIntProperty("p1"), constraint.getIntProperty("p2"), constraint.getDoubleProperty("d"));
}
const SerializationNode& forces = node.getChildNode("Forces"); const SerializationNode& forces = node.getChildNode("Forces");
for (int i = 0; i < (int) forces.getChildren().size(); i++) { for (auto& force : forces.getChildren())
system->addForce(forces.getChildren()[i].decodeObject<Force>()); system->addForce(force.decodeObject<Force>());
}
} }
catch (...) { catch (...) {
delete system; delete system;
......
...@@ -49,8 +49,8 @@ void Continuous1DFunctionProxy::serialize(const void* object, SerializationNode& ...@@ -49,8 +49,8 @@ void Continuous1DFunctionProxy::serialize(const void* object, SerializationNode&
node.setDoubleProperty("min", min); node.setDoubleProperty("min", min);
node.setDoubleProperty("max", max); node.setDoubleProperty("max", max);
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
} }
void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const { void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) const {
...@@ -58,8 +58,8 @@ void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) cons ...@@ -58,8 +58,8 @@ void* Continuous1DFunctionProxy::deserialize(const SerializationNode& node) cons
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) for (auto& child : valuesNode.getChildren())
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max")); return new Continuous1DFunction(values, node.getDoubleProperty("min"), node.getDoubleProperty("max"));
} }
...@@ -80,8 +80,8 @@ void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode& ...@@ -80,8 +80,8 @@ void Continuous2DFunctionProxy::serialize(const void* object, SerializationNode&
node.setDoubleProperty("ymin", ymin); node.setDoubleProperty("ymin", ymin);
node.setDoubleProperty("ymax", ymax); node.setDoubleProperty("ymax", ymax);
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
} }
void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) const { void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) const {
...@@ -89,8 +89,8 @@ void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) cons ...@@ -89,8 +89,8 @@ void* Continuous2DFunctionProxy::deserialize(const SerializationNode& node) cons
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) for (auto& child : valuesNode.getChildren())
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
return new Continuous2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values, return new Continuous2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values,
node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax")); node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"));
} }
...@@ -115,8 +115,8 @@ void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode& ...@@ -115,8 +115,8 @@ void Continuous3DFunctionProxy::serialize(const void* object, SerializationNode&
node.setDoubleProperty("zmin", zmin); node.setDoubleProperty("zmin", zmin);
node.setDoubleProperty("zmax", zmax); node.setDoubleProperty("zmax", zmax);
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
} }
void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) const { void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) const {
...@@ -124,8 +124,8 @@ void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) cons ...@@ -124,8 +124,8 @@ void* Continuous3DFunctionProxy::deserialize(const SerializationNode& node) cons
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) for (auto& child : valuesNode.getChildren())
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
return new Continuous3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values, return new Continuous3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values,
node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"), node.getDoubleProperty("xmin"), node.getDoubleProperty("xmax"), node.getDoubleProperty("ymin"), node.getDoubleProperty("ymax"),
node.getDoubleProperty("zmin"), node.getDoubleProperty("zmax")); node.getDoubleProperty("zmin"), node.getDoubleProperty("zmax"));
...@@ -140,8 +140,8 @@ void Discrete1DFunctionProxy::serialize(const void* object, SerializationNode& n ...@@ -140,8 +140,8 @@ void Discrete1DFunctionProxy::serialize(const void* object, SerializationNode& n
vector<double> values; vector<double> values;
function.getFunctionParameters(values); function.getFunctionParameters(values);
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
} }
void* Discrete1DFunctionProxy::deserialize(const SerializationNode& node) const { void* Discrete1DFunctionProxy::deserialize(const SerializationNode& node) const {
...@@ -149,8 +149,8 @@ void* Discrete1DFunctionProxy::deserialize(const SerializationNode& node) const ...@@ -149,8 +149,8 @@ void* Discrete1DFunctionProxy::deserialize(const SerializationNode& node) const
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) for (auto& child : valuesNode.getChildren())
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
return new Discrete1DFunction(values); return new Discrete1DFunction(values);
} }
...@@ -166,8 +166,8 @@ void Discrete2DFunctionProxy::serialize(const void* object, SerializationNode& n ...@@ -166,8 +166,8 @@ void Discrete2DFunctionProxy::serialize(const void* object, SerializationNode& n
node.setDoubleProperty("xsize", xsize); node.setDoubleProperty("xsize", xsize);
node.setDoubleProperty("ysize", ysize); node.setDoubleProperty("ysize", ysize);
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
} }
void* Discrete2DFunctionProxy::deserialize(const SerializationNode& node) const { void* Discrete2DFunctionProxy::deserialize(const SerializationNode& node) const {
...@@ -175,8 +175,8 @@ void* Discrete2DFunctionProxy::deserialize(const SerializationNode& node) const ...@@ -175,8 +175,8 @@ void* Discrete2DFunctionProxy::deserialize(const SerializationNode& node) const
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) for (auto& child : valuesNode.getChildren())
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
return new Discrete2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values); return new Discrete2DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), values);
} }
...@@ -193,8 +193,8 @@ void Discrete3DFunctionProxy::serialize(const void* object, SerializationNode& n ...@@ -193,8 +193,8 @@ void Discrete3DFunctionProxy::serialize(const void* object, SerializationNode& n
node.setDoubleProperty("ysize", ysize); node.setDoubleProperty("ysize", ysize);
node.setDoubleProperty("zsize", zsize); node.setDoubleProperty("zsize", zsize);
SerializationNode& valuesNode = node.createChildNode("Values"); SerializationNode& valuesNode = node.createChildNode("Values");
for (int j = 0; j < (int) values.size(); j++) for (auto v : values)
valuesNode.createChildNode("Value").setDoubleProperty("v", values[j]); valuesNode.createChildNode("Value").setDoubleProperty("v", v);
} }
void* Discrete3DFunctionProxy::deserialize(const SerializationNode& node) const { void* Discrete3DFunctionProxy::deserialize(const SerializationNode& node) const {
...@@ -202,7 +202,7 @@ void* Discrete3DFunctionProxy::deserialize(const SerializationNode& node) const ...@@ -202,7 +202,7 @@ void* Discrete3DFunctionProxy::deserialize(const SerializationNode& node) const
throw OpenMMException("Unsupported version number"); throw OpenMMException("Unsupported version number");
const SerializationNode& valuesNode = node.getChildNode("Values"); const SerializationNode& valuesNode = node.getChildNode("Values");
vector<double> values; vector<double> values;
for (int j = 0; j < (int) valuesNode.getChildren().size(); j++) for (auto& child : valuesNode.getChildren())
values.push_back(valuesNode.getChildren()[j].getDoubleProperty("v")); values.push_back(child.getDoubleProperty("v"));
return new Discrete3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values); return new Discrete3DFunction(node.getIntProperty("xsize"), node.getIntProperty("ysize"), node.getIntProperty("zsize"), values);
} }
...@@ -115,11 +115,10 @@ void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stre ...@@ -115,11 +115,10 @@ void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stre
for (int i = 0; i < depth; i++) for (int i = 0; i < depth; i++)
stream << '\t'; stream << '\t';
stream << '<' << node.getName(); stream << '<' << node.getName();
const map<string, string>& properties = node.getProperties(); for (auto& prop : node.getProperties()) {
for (map<string, string>::const_iterator iter = properties.begin(); iter != properties.end(); ++iter) {
string name, value; string name, value;
encodeString(iter->first, &name); encodeString(prop.first, &name);
encodeString(iter->second, &value); encodeString(prop.second, &value);
stream << ' ' << name << "=\"" << value << '\"'; stream << ' ' << name << "=\"" << value << '\"';
} }
const vector<SerializationNode>& children = node.getChildren(); const vector<SerializationNode>& children = node.getChildren();
...@@ -127,8 +126,8 @@ void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stre ...@@ -127,8 +126,8 @@ void XmlSerializer::encodeNode(const SerializationNode& node, std::ostream& stre
stream << "/>\n"; stream << "/>\n";
else { else {
stream << ">\n"; stream << ">\n";
for (int i = 0; i < (int) children.size(); i++) for (auto& child : children)
encodeNode(children[i], stream, depth+1); encodeNode(child, stream, depth+1);
for (int i = 0; i < depth; i++) for (int i = 0; i < depth; i++)
stream << '\t'; stream << '\t';
stream << "</" << node.getName() << ">\n"; stream << "</" << node.getName() << ">\n";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment