From: Tom Paoletti Date: Fri, 22 Mar 2013 02:27:30 +0000 (-0700) Subject: Performance optimization: empty() instead of size()>0 X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=15e3e92ec20b1c90dee217fe5e4eba5cbd44011d;p=simgear.git 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. --- diff --git a/simgear/environment/metar.cxx b/simgear/environment/metar.cxx index 75109b75..ec4375ab 100644 --- a/simgear/environment/metar.cxx +++ b/simgear/environment/metar.cxx @@ -629,7 +629,7 @@ bool SGMetar::scanWeather() weather = pre + weather + post; weather.erase(weather.length() - 1); _weather.push_back(weather); - if( w.phenomena.size() > 0 ) + if( ! w.phenomena.empty() ) _weather2.push_back( w ); _grpcount++; return true; diff --git a/simgear/io/sg_binobj.cxx b/simgear/io/sg_binobj.cxx index 994f3dbe..704d9e3a 100644 --- a/simgear/io/sg_binobj.cxx +++ b/simgear/io/sg_binobj.cxx @@ -934,7 +934,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name, fprintf(fp, "\n"); // dump individual triangles if they exist - if ( tris_v.size() > 0 ) { + if ( ! tris_v.empty() ) { fprintf(fp, "# triangle groups\n"); int start = 0; @@ -982,7 +982,7 @@ bool SGBinObject::write_ascii( const string& base, const string& name, } // dump triangle groups - if ( strips_v.size() > 0 ) { + if ( ! strips_v.empty() ) { fprintf(fp, "# triangle strips\n"); int start = 0; diff --git a/simgear/misc/sg_path.cxx b/simgear/misc/sg_path.cxx index 69d1ac44..4e11de3c 100644 --- a/simgear/misc/sg_path.cxx +++ b/simgear/misc/sg_path.cxx @@ -144,7 +144,7 @@ void SGPath::set_cached(bool cached) // append another piece to the existing path void SGPath::append( const string& p ) { - if ( path.size() == 0 ) { + if ( path.empty() ) { path = p; } else { 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 // path separator void SGPath::concat( const string& p ) { - if ( path.size() == 0 ) { + if ( path.empty() ) { path = p; } else { path += p; @@ -378,7 +378,7 @@ int SGPath::create_dir( mode_t mode ) { string_list sgPathBranchSplit( const string &dirpath ) { string_list path_elements; string element, path = dirpath; - while ( path.size() ) { + while ( ! path.empty() ) { size_t p = path.find( sgDirPathSep ); if ( p != string::npos ) { element = path.substr( 0, p ); @@ -387,7 +387,7 @@ string_list sgPathBranchSplit( const string &dirpath ) { element = path; path = ""; } - if ( element.size() ) + if ( ! element.empty() ) path_elements.push_back( element ); } return path_elements; diff --git a/simgear/props/tiedpropertylist.hxx b/simgear/props/tiedpropertylist.hxx index 647b8d56..39461afd 100644 --- a/simgear/props/tiedpropertylist.hxx +++ b/simgear/props/tiedpropertylist.hxx @@ -37,11 +37,11 @@ public: virtual ~TiedPropertyList() { _root = 0; - if (size()>0) + if (! empty()) { SG_LOG(SG_GENERAL, SG_ALERT, "Detected properties with dangling ties. Use 'Untie' before removing a TiedPropertyList."); // running debug mode: go, fix it! - assert(size() == 0); + assert(empty()); } } @@ -124,7 +124,7 @@ public: } void Untie() { - while( size() > 0 ) { + while( ! empty() ) { SG_LOG( SG_GENERAL, SG_DEBUG, "untie of " << back()->getPath() ); back()->untie(); pop_back(); diff --git a/simgear/scene/material/EffectBuilder.hxx b/simgear/scene/material/EffectBuilder.hxx index 5624aaa2..06b8ba9f 100644 --- a/simgear/scene/material/EffectBuilder.hxx +++ b/simgear/scene/material/EffectBuilder.hxx @@ -316,7 +316,7 @@ getVectorProperties(const SGPropertyNode* prop, PropertyList useProps = prop->getChildren("use"); if (useProps.size() == 1) { string parentName = useProps[0]->getStringValue(); - if (parentName.size() == 0 || parentName[0] != '/') + if (parentName.empty() || parentName[0] != '/') parentName = options->getPropertyNode()->getPath() + "/" + parentName; if (parentName[parentName.size() - 1] != '/') parentName.append("/"); @@ -331,7 +331,7 @@ getVectorProperties(const SGPropertyNode* prop, itr != end; ++itr) { string childName = (*itr)->getStringValue(); - if (childName.size() == 0 || childName[0] != '/') + if (childName.empty() || childName[0] != '/') result.push_back(parentName + childName); else result.push_back(childName); diff --git a/simgear/scene/material/Technique.cxx b/simgear/scene/material/Technique.cxx index a3c5e34e..4caa9d0c 100644 --- a/simgear/scene/material/Technique.cxx +++ b/simgear/scene/material/Technique.cxx @@ -167,12 +167,12 @@ Technique::processDrawables(const EffectGeode::DrawablesIterator& begin, BOOST_FOREACH(ref_ptr& pass, passes) { osg::ref_ptr ss = pass; - if (ecv && ( pass->getBufferUnitList().size() != 0 || pass->getPositionedUniformMap().size() != 0 ) ) { + if (ecv && ( ! pass->getBufferUnitList().empty() || ! pass->getPositionedUniformMap().empty() ) ) { ss = static_cast( - pass->clone( osg::CopyOp( ( pass->getBufferUnitList().size() != 0 ? + pass->clone( osg::CopyOp( ( ! pass->getBufferUnitList().empty() ? osg::CopyOp::DEEP_COPY_TEXTURES : osg::CopyOp::SHALLOW_COPY ) | - ( pass->getPositionedUniformMap().size() != 0 ? + ( ! pass->getPositionedUniformMap().empty() ? osg::CopyOp::DEEP_COPY_UNIFORMS : osg::CopyOp::SHALLOW_COPY ) ) ) diff --git a/simgear/scene/material/mat.cxx b/simgear/scene/material/mat.cxx index 65d1401d..46af662b 100644 --- a/simgear/scene/material/mat.cxx +++ b/simgear/scene/material/mat.cxx @@ -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"); tpath.append("Terrain"); tpath.append("unknown.rgb"); @@ -455,7 +455,7 @@ Effect* SGMaterial::get_effect(int i) Effect* SGMaterial::get_effect(const SGTexturedTriangleBin& triangleBin) { - if (_status.size() == 0) { + if (_status.empty()) { SG_LOG( SG_GENERAL, SG_WARN, "No effect available."); return 0; } @@ -472,7 +472,7 @@ Effect* SGMaterial::get_effect() 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."); return 0; } diff --git a/simgear/scene/model/SGText.cxx b/simgear/scene/model/SGText.cxx index 21638793..747e4098 100644 --- a/simgear/scene/model/SGText.cxx +++ b/simgear/scene/model/SGText.cxx @@ -45,7 +45,7 @@ public: numeric( aNumeric ), format( aFormat ) { - if( format.size() == 0 ) { + if( format.empty() ) { if( numeric ) format = "%f"; else format = "%s"; } diff --git a/simgear/scene/sky/CloudShaderGeometry.cxx b/simgear/scene/sky/CloudShaderGeometry.cxx index d8f44022..3a41ceb6 100644 --- a/simgear/scene/sky/CloudShaderGeometry.cxx +++ b/simgear/scene/sky/CloudShaderGeometry.cxx @@ -48,7 +48,7 @@ namespace simgear { void CloudShaderGeometry::drawImplementation(RenderInfo& renderInfo) const { - if (!_cloudsprites.size()) return; + if (_cloudsprites.empty()) return; osg::State& state = *renderInfo.getState(); diff --git a/simgear/scene/sky/cloudfield.cxx b/simgear/scene/sky/cloudfield.cxx index 18c7e8ef..45ae15f8 100644 --- a/simgear/scene/sky/cloudfield.cxx +++ b/simgear/scene/sky/cloudfield.cxx @@ -395,7 +395,7 @@ bool SGCloudField::repositionCloud(int identifier, float lon, float lat, float a } bool SGCloudField::isDefined3D(void) { - return (cloud_hash.size() > 0); + return (! cloud_hash.empty()); } SGCloudField::CloudFog::CloudFog() { diff --git a/simgear/scene/tgdb/apt_signs.cxx b/simgear/scene/tgdb/apt_signs.cxx index ab60c232..6a3ce73d 100644 --- a/simgear/scene/tgdb/apt_signs.cxx +++ b/simgear/scene/tgdb/apt_signs.cxx @@ -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); newmat.clear(); } diff --git a/simgear/scene/tgdb/obj.cxx b/simgear/scene/tgdb/obj.cxx index 21427c90..b388d4e7 100644 --- a/simgear/scene/tgdb/obj.cxx +++ b/simgear/scene/tgdb/obj.cxx @@ -1015,7 +1015,7 @@ SGLoadBTG(const std::string& path, const simgear::SGReaderWriterOptions* options randomObjects->setName("Random objects"); } - if (tileGeometryBin.randomBuildings.size() > 0) { + if (! tileGeometryBin.randomBuildings.empty()) { buildingNode = createRandomBuildings(tileGeometryBin.randomBuildings, osg::Matrix::identity(), options); buildingNode->setName("Random buildings"); @@ -1025,7 +1025,7 @@ SGLoadBTG(const std::string& path, const simgear::SGReaderWriterOptions* options // Now add some random forest. tileGeometryBin.computeRandomForest(matlib, vegetation_density); - if (tileGeometryBin.randomForest.size() > 0) { + if (! tileGeometryBin.randomForest.empty()) { forestNode = createForest(tileGeometryBin.randomForest, osg::Matrix::identity(), options); forestNode->setName("Random trees");