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