[Kst] extragear/graphics/kst/kst

George Staikos staikos at kde.org
Wed Oct 26 05:56:58 CEST 2005


SVN commit 474329 by staikos:

a big simplification and refactor.  gets rid of extra map lookups and reduces
branching and other operations in hotspots


 M  +28 -61    kstalignment.cpp  
 M  +2 -3      kstalignment.h  


--- trunk/extragear/graphics/kst/kst/kstalignment.cpp #474328:474329
@@ -19,18 +19,8 @@
 
 KstAlignment KST::alignment;
 
-bool operator<( const QPoint &p1, const QPoint &p2 ) {
-  bool bRetVal = false;
-
-  if (p1.x() < p2.x()) {
-    bRetVal = true;
-  } else if (p1.x() == p2.x()) {
-    if (p1.y() < p2.y()) {
-      bRetVal = true;
-    }
-  }
-
-  return bRetVal;
+int operator<(const QPoint &p1, const QPoint &p2) {
+  return p1.x() < p2.x() || (p1.x() == p2.x() && p1.y() < p2.y());
 }
 
 
@@ -45,25 +35,13 @@
 
 
 void KstAlignment::setPosition(const QRect& geometry, const QRect& plotRegion) {
-  QPoint xAllRegion;
-  QPoint yAllRegion;
-  QPoint xPlotRegion;
-  QPoint yPlotRegion;
-  QPoint xPlotRegionCurrent;
-  QPoint yPlotRegionCurrent;
+  QPoint xAllRegion(geometry.left(), geometry.right());
+  QPoint yAllRegion(geometry.top(), geometry.bottom());
+  QPoint xPlotRegion(plotRegion.left(), plotRegion.right());
+  QPoint yPlotRegion(plotRegion.top(), plotRegion.bottom());
 
-  xAllRegion.setX(geometry.left());
-  xAllRegion.setY(geometry.right());
-  yAllRegion.setX(geometry.top());
-  yAllRegion.setY(geometry.bottom());
-
-  xPlotRegion.setX(plotRegion.left());
-  xPlotRegion.setY(plotRegion.right());
-  yPlotRegion.setX(plotRegion.top());
-  yPlotRegion.setY(plotRegion.bottom());  
-
   if (_xAlignments.contains(xAllRegion)) {
-    xPlotRegionCurrent = _xAlignments[xAllRegion];
+    QPoint xPlotRegionCurrent = _xAlignments[xAllRegion];
 
     if (xPlotRegion.x() > xPlotRegionCurrent.x()) {
       xPlotRegionCurrent.setX(xPlotRegion.x());
@@ -77,7 +55,7 @@
   }
 
   if (_yAlignments.contains(yAllRegion)) {
-    yPlotRegionCurrent = _yAlignments[yAllRegion];
+    QPoint yPlotRegionCurrent = _yAlignments[yAllRegion];
 
     if (yPlotRegion.x() > yPlotRegionCurrent.x()) {
       yPlotRegionCurrent.setX(yPlotRegion.x());
@@ -93,58 +71,47 @@
 
 
 void KstAlignment::limits(const QRect& geometry, double& xleft, double& xright, double& ytop, double& ybottom, double dFactor) {
-  QPoint xAllRegion;
-  QPoint yAllRegion;
+  QPoint xAllRegion(geometry.left(), geometry.right());
+  QPoint yAllRegion(geometry.top(), geometry.bottom());
 
-  xAllRegion.setX(geometry.left());
-  xAllRegion.setY(geometry.right());
-  yAllRegion.setX(geometry.top());
-  yAllRegion.setY(geometry.bottom());
-
-  if (_xAlignments.contains(xAllRegion)) {
-    xleft = (double)_xAlignments[xAllRegion].x();
-    xright = (double)_xAlignments[xAllRegion].y();
+  alignmentMap::ConstIterator allRef = _xAlignments.find(xAllRegion);
+  if (allRef != _xAlignments.end()) {
+    xleft = double((*allRef).x()) * dFactor;
+    xright = double((*allRef).y()) * dFactor;
   } else {
     xleft = 0.0;
     xright = 0.0;
   }
 
-  if (_yAlignments.contains(yAllRegion)) {
-    ytop = (double)_yAlignments[yAllRegion].x();
-    ybottom = (double)_yAlignments[yAllRegion].y();
+  allRef = _yAlignments.find(yAllRegion);
+  if (allRef != _yAlignments.end()) {
+    ytop = double((*allRef).x()) * dFactor;
+    ybottom = double((*allRef).y()) * dFactor;
   } else {
     ytop = 0.0;
     ybottom = 0.0;
   }
-
-  xleft   *= dFactor;
-  xright  *= dFactor;
-  ytop    *= dFactor;
-  ybottom *= dFactor;
 }
 
 
 QRect KstAlignment::limits(const QRect& geometry) {
-  QPoint xAllRegion;
-  QPoint yAllRegion;
+  QPoint xAllRegion(geometry.left(), geometry.right());
+  QPoint yAllRegion(geometry.top(), geometry.bottom());
   QRect plotRegion;
 
-  xAllRegion.setX(geometry.left());
-  xAllRegion.setY(geometry.right());
-  yAllRegion.setX(geometry.top());
-  yAllRegion.setY(geometry.bottom());
-
-  if (_xAlignments.contains(xAllRegion)) {
-    plotRegion.setLeft(_xAlignments[xAllRegion].x());
-    plotRegion.setRight(_xAlignments[xAllRegion].y());
+  alignmentMap::ConstIterator allRef = _xAlignments.find(xAllRegion);
+  if (allRef != _xAlignments.end()) {
+    plotRegion.setLeft((*allRef).x());
+    plotRegion.setRight((*allRef).y());
   } else {
     plotRegion.setLeft(0);
     plotRegion.setRight(0);
   }
 
-  if (_yAlignments.contains(yAllRegion)) {
-    plotRegion.setTop(_yAlignments[yAllRegion].x());
-    plotRegion.setBottom(_yAlignments[yAllRegion].y());
+  allRef = _yAlignments.find(yAllRegion);
+  if (allRef != _yAlignments.end()) {
+    plotRegion.setTop((*allRef).x());
+    plotRegion.setBottom((*allRef).y());
   } else {
     plotRegion.setTop(0);
     plotRegion.setBottom(0);
--- trunk/extragear/graphics/kst/kst/kstalignment.h #474328:474329
@@ -23,10 +23,8 @@
 #include <qpoint.h>
 #include <qrect.h>
 
-Q_EXPORT bool operator<(const QPoint &p1, const QPoint &p2);
+Q_EXPORT int operator<(const QPoint &p1, const QPoint &p2);
 
-typedef QMap<QPoint, QPoint> alignmentMap;
-
 // this class keeps track of the plot line alignment (eg, xaxis lineup, etc)
 class KstAlignment {
  public:
@@ -37,6 +35,7 @@
   QRect limits(const QRect& geometry);
 
 private:
+  typedef QMap<QPoint, QPoint> alignmentMap;
   alignmentMap  _xAlignments;
   alignmentMap  _yAlignments;
 };


More information about the Kst mailing list