]> git.mxchange.org Git - flightgear.git/commitdiff
Performance optimization: empty() instead of size()>0
authorTom Paoletti <zommaso@gmail.com>
Fri, 22 Mar 2013 02:43:03 +0000 (19:43 -0700)
committerJames Turner <zakalawe@mac.com>
Mon, 19 Aug 2013 08:02:06 +0000 (09:02 +0100)
empty() is guaranteed to be constant complexity for both vectors and lists, while size() has linear complexity for lists.

src/FDM/YASim/Rotor.cpp
src/FDM/YASim/Vector.hpp
src/FDM/YASim/Wing.cpp

index ab1775af50594a5dccefb08a5bdfbc48941adb9c..46207027cd06e0d4965ab70028415b6d6dfdfc32 100644 (file)
@@ -1159,7 +1159,7 @@ void Rotor::updateDirectionsAndPositions(float *rot)
 void Rotor::compile()
 {
     // Have we already been compiled?
-    if(_rotorparts.size() != 0) return;
+    if(! _rotorparts.empty()) return;
 
     //rotor is divided into _number_of_parts parts
     //each part is calcualted at _number_of_segments points
@@ -1498,7 +1498,7 @@ void Rotorgear::initRotorIteration(float *lrot,float dt)
 {
     int i;
     float omegarel;
-    if (!_rotors.size()) return;
+    if (_rotors.empty()) return;
     Rotor* r0 = (Rotor*)_rotors.get(0);
     omegarel= r0->getOmegaRelNeu();
     for(i=0; i<_rotors.size(); i++) {
@@ -1514,7 +1514,7 @@ void Rotorgear::calcForces(float* torqueOut)
     // check,<if the engine can handle the torque of the rotors. 
     // If not reduce the torque to the fueselage and change rotational 
     // speed of the rotors instead
-    if (_rotors.size())
+    if (! _rotors.empty())
     {
         float omegarel,omegan;
         Rotor* r0 = (Rotor*)_rotors.get(0);
@@ -1556,7 +1556,7 @@ void Rotorgear::calcForces(float* torqueOut)
 
         //add the rotor brake and the gear fritcion
         float dt=0.1f;
-        if (r0->_rotorparts.size()) dt=((Rotorpart*)r0->_rotorparts.get(0))->getDt();
+        if (! r0->_rotorparts.empty()) dt=((Rotorpart*)r0->_rotorparts.get(0))->getDt();
 
         float rotor_brake_torque;
         rotor_brake_torque=_rotorbrake*_max_power_rotor_brake+_rotorgear_friction;
index ea647a30d1356e743ffc8fef195edfcddb2927ea..7d56956434eda5e4956f30ae74a207a440667ff9 100644 (file)
@@ -15,6 +15,7 @@ public:
     void* get(int i);
     void  set(int i, void* p);
     int   size();
+    bool  empty();
 private:
     void realloc();
 
@@ -58,6 +59,11 @@ inline int Vector::size()
     return _sz;
 }
 
+inline bool Vector::empty()
+{
+    return _sz == 0;
+}
+
 inline void Vector::realloc()
 {
     _nelem = 2*_nelem + 1;
index ad15410e17a7a7831e1280849a69253f4635552b..1a68244962bd91a54815c94c8229bc779f18588e 100644 (file)
@@ -254,7 +254,7 @@ bool Wing::isMirrored()
 void Wing::compile()
 {
     // Have we already been compiled?
-    if(_surfs.size() != 0) return;
+    if(! _surfs.empty()) return;
 
     // Assemble the start/end coordinates of all control surfaces
     // and the wing itself into an array, sort them,