Performance optimization: empty() instead of size()>0

empty() is guaranteed to be constant complexity for both vectors and lists, while size() has linear complexity for lists.
This commit is contained in:
Tom Paoletti 2013-03-21 19:27:30 -07:00 committed by James Turner
parent ee403fd83a
commit 15e3e92ec2
12 changed files with 24 additions and 24 deletions

View File

@ -629,7 +629,7 @@ bool SGMetar::scanWeather()
weather = pre + weather + post; weather = pre + weather + post;
weather.erase(weather.length() - 1); weather.erase(weather.length() - 1);
_weather.push_back(weather); _weather.push_back(weather);
if( w.phenomena.size() > 0 ) if( ! w.phenomena.empty() )
_weather2.push_back( w ); _weather2.push_back( w );
_grpcount++; _grpcount++;
return true; return true;

View File

@ -934,7 +934,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
fprintf(fp, "\n"); fprintf(fp, "\n");
// dump individual triangles if they exist // dump individual triangles if they exist
if ( tris_v.size() > 0 ) { if ( ! tris_v.empty() ) {
fprintf(fp, "# triangle groups\n"); fprintf(fp, "# triangle groups\n");
int start = 0; int start = 0;
@ -982,7 +982,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name,
} }
// dump triangle groups // dump triangle groups
if ( strips_v.size() > 0 ) { if ( ! strips_v.empty() ) {
fprintf(fp, "# triangle strips\n"); fprintf(fp, "# triangle strips\n");
int start = 0; int start = 0;

View File

@ -144,7 +144,7 @@ void SGPath::set_cached(bool cached)
// append another piece to the existing path // append another piece to the existing path
void SGPath::append( const string& p ) { void SGPath::append( const string& p ) {
if ( path.size() == 0 ) { if ( path.empty() ) {
path = p; path = p;
} else { } else {
if ( p[0] != sgDirPathSep ) { if ( p[0] != sgDirPathSep ) {
@ -173,7 +173,7 @@ void SGPath::add( const string& p ) {
// concatenate a string to the end of the path without inserting a // concatenate a string to the end of the path without inserting a
// path separator // path separator
void SGPath::concat( const string& p ) { void SGPath::concat( const string& p ) {
if ( path.size() == 0 ) { if ( path.empty() ) {
path = p; path = p;
} else { } else {
path += p; path += p;
@ -378,7 +378,7 @@ int SGPath::create_dir( mode_t mode ) {
string_list sgPathBranchSplit( const string &dirpath ) { string_list sgPathBranchSplit( const string &dirpath ) {
string_list path_elements; string_list path_elements;
string element, path = dirpath; string element, path = dirpath;
while ( path.size() ) { while ( ! path.empty() ) {
size_t p = path.find( sgDirPathSep ); size_t p = path.find( sgDirPathSep );
if ( p != string::npos ) { if ( p != string::npos ) {
element = path.substr( 0, p ); element = path.substr( 0, p );
@ -387,7 +387,7 @@ string_list sgPathBranchSplit( const string &dirpath ) {
element = path; element = path;
path = ""; path = "";
} }
if ( element.size() ) if ( ! element.empty() )
path_elements.push_back( element ); path_elements.push_back( element );
} }
return path_elements; return path_elements;

View File

@ -37,11 +37,11 @@ public:
virtual ~TiedPropertyList() virtual ~TiedPropertyList()
{ {
_root = 0; _root = 0;
if (size()>0) if (! empty())
{ {
SG_LOG(SG_GENERAL, SG_ALERT, "Detected properties with dangling ties. Use 'Untie' before removing a TiedPropertyList."); SG_LOG(SG_GENERAL, SG_ALERT, "Detected properties with dangling ties. Use 'Untie' before removing a TiedPropertyList.");
// running debug mode: go, fix it! // running debug mode: go, fix it!
assert(size() == 0); assert(empty());
} }
} }
@ -124,7 +124,7 @@ public:
} }
void Untie() { void Untie() {
while( size() > 0 ) { while( ! empty() ) {
SG_LOG( SG_GENERAL, SG_DEBUG, "untie of " << back()->getPath() ); SG_LOG( SG_GENERAL, SG_DEBUG, "untie of " << back()->getPath() );
back()->untie(); back()->untie();
pop_back(); pop_back();

View File

@ -316,7 +316,7 @@ getVectorProperties(const SGPropertyNode* prop,
PropertyList useProps = prop->getChildren("use"); PropertyList useProps = prop->getChildren("use");
if (useProps.size() == 1) { if (useProps.size() == 1) {
string parentName = useProps[0]->getStringValue(); string parentName = useProps[0]->getStringValue();
if (parentName.size() == 0 || parentName[0] != '/') if (parentName.empty() || parentName[0] != '/')
parentName = options->getPropertyNode()->getPath() + "/" + parentName; parentName = options->getPropertyNode()->getPath() + "/" + parentName;
if (parentName[parentName.size() - 1] != '/') if (parentName[parentName.size() - 1] != '/')
parentName.append("/"); parentName.append("/");
@ -331,7 +331,7 @@ getVectorProperties(const SGPropertyNode* prop,
itr != end; itr != end;
++itr) { ++itr) {
string childName = (*itr)->getStringValue(); string childName = (*itr)->getStringValue();
if (childName.size() == 0 || childName[0] != '/') if (childName.empty() || childName[0] != '/')
result.push_back(parentName + childName); result.push_back(parentName + childName);
else else
result.push_back(childName); result.push_back(childName);

View File

@ -167,12 +167,12 @@ Technique::processDrawables(const EffectGeode::DrawablesIterator& begin,
BOOST_FOREACH(ref_ptr<Pass>& pass, passes) BOOST_FOREACH(ref_ptr<Pass>& pass, passes)
{ {
osg::ref_ptr<osg::StateSet> ss = pass; osg::ref_ptr<osg::StateSet> ss = pass;
if (ecv && ( pass->getBufferUnitList().size() != 0 || pass->getPositionedUniformMap().size() != 0 ) ) { if (ecv && ( ! pass->getBufferUnitList().empty() || ! pass->getPositionedUniformMap().empty() ) ) {
ss = static_cast<osg::StateSet*>( ss = static_cast<osg::StateSet*>(
pass->clone( osg::CopyOp( ( pass->getBufferUnitList().size() != 0 ? pass->clone( osg::CopyOp( ( ! pass->getBufferUnitList().empty() ?
osg::CopyOp::DEEP_COPY_TEXTURES : osg::CopyOp::DEEP_COPY_TEXTURES :
osg::CopyOp::SHALLOW_COPY ) | osg::CopyOp::SHALLOW_COPY ) |
( pass->getPositionedUniformMap().size() != 0 ? ( ! pass->getPositionedUniformMap().empty() ?
osg::CopyOp::DEEP_COPY_UNIFORMS : osg::CopyOp::DEEP_COPY_UNIFORMS :
osg::CopyOp::SHALLOW_COPY ) ) osg::CopyOp::SHALLOW_COPY ) )
) )

View File

@ -202,7 +202,7 @@ SGMaterial::read_properties(const SGReaderWriterOptions* options,
} }
} }
if (textures.size() == 0 && texturesets.size() == 0) { if (textures.empty() && texturesets.empty()) {
SGPath tpath("Textures"); SGPath tpath("Textures");
tpath.append("Terrain"); tpath.append("Terrain");
tpath.append("unknown.rgb"); tpath.append("unknown.rgb");
@ -455,7 +455,7 @@ Effect* SGMaterial::get_effect(int i)
Effect* SGMaterial::get_effect(const SGTexturedTriangleBin& triangleBin) Effect* SGMaterial::get_effect(const SGTexturedTriangleBin& triangleBin)
{ {
if (_status.size() == 0) { if (_status.empty()) {
SG_LOG( SG_GENERAL, SG_WARN, "No effect available."); SG_LOG( SG_GENERAL, SG_WARN, "No effect available.");
return 0; return 0;
} }
@ -472,7 +472,7 @@ Effect* SGMaterial::get_effect()
osg::Texture2D* SGMaterial::get_object_mask(const SGTexturedTriangleBin& triangleBin) osg::Texture2D* SGMaterial::get_object_mask(const SGTexturedTriangleBin& triangleBin)
{ {
if (_status.size() == 0) { if (_status.empty()) {
SG_LOG( SG_GENERAL, SG_WARN, "No mask available."); SG_LOG( SG_GENERAL, SG_WARN, "No mask available.");
return 0; return 0;
} }

View File

@ -45,7 +45,7 @@ public:
numeric( aNumeric ), numeric( aNumeric ),
format( aFormat ) format( aFormat )
{ {
if( format.size() == 0 ) { if( format.empty() ) {
if( numeric ) format = "%f"; if( numeric ) format = "%f";
else format = "%s"; else format = "%s";
} }

View File

@ -48,7 +48,7 @@ namespace simgear
{ {
void CloudShaderGeometry::drawImplementation(RenderInfo& renderInfo) const void CloudShaderGeometry::drawImplementation(RenderInfo& renderInfo) const
{ {
if (!_cloudsprites.size()) return; if (_cloudsprites.empty()) return;
osg::State& state = *renderInfo.getState(); osg::State& state = *renderInfo.getState();

View File

@ -395,7 +395,7 @@ bool SGCloudField::repositionCloud(int identifier, float lon, float lat, float a
} }
bool SGCloudField::isDefined3D(void) { bool SGCloudField::isDefined3D(void) {
return (cloud_hash.size() > 0); return (! cloud_hash.empty());
} }
SGCloudField::CloudFog::CloudFog() { SGCloudField::CloudFog::CloudFog() {

View File

@ -429,7 +429,7 @@ void AirportSignBuilder::addSign(const SGGeod& pos, double heading, const std::s
} }
} }
if (newmat.size()) { if (! newmat.empty()) {
material = d->materials->find(newmat); material = d->materials->find(newmat);
newmat.clear(); newmat.clear();
} }

View File

@ -1015,7 +1015,7 @@ SGLoadBTG(const std::string& path, const simgear::SGReaderWriterOptions* options
randomObjects->setName("Random objects"); randomObjects->setName("Random objects");
} }
if (tileGeometryBin.randomBuildings.size() > 0) { if (! tileGeometryBin.randomBuildings.empty()) {
buildingNode = createRandomBuildings(tileGeometryBin.randomBuildings, osg::Matrix::identity(), buildingNode = createRandomBuildings(tileGeometryBin.randomBuildings, osg::Matrix::identity(),
options); options);
buildingNode->setName("Random buildings"); buildingNode->setName("Random buildings");
@ -1025,7 +1025,7 @@ SGLoadBTG(const std::string& path, const simgear::SGReaderWriterOptions* options
// Now add some random forest. // Now add some random forest.
tileGeometryBin.computeRandomForest(matlib, vegetation_density); tileGeometryBin.computeRandomForest(matlib, vegetation_density);
if (tileGeometryBin.randomForest.size() > 0) { if (! tileGeometryBin.randomForest.empty()) {
forestNode = createForest(tileGeometryBin.randomForest, osg::Matrix::identity(), forestNode = createForest(tileGeometryBin.randomForest, osg::Matrix::identity(),
options); options);
forestNode->setName("Random trees"); forestNode->setName("Random trees");