summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKonstantin Ritt <ritt.ks@gmail.com>2011-01-11 13:12:01 (GMT)
committerOlivier Goffart <olivier.goffart@nokia.com>2011-01-11 13:12:01 (GMT)
commitb500b9f35fbeaa5f0fe8bfaf7b225518bf6e636f (patch)
treeddc7bc7e013343374c8ec3ee8260821c95624c22 /src
parent540fbb2aec0e9187acd75598a050edf8929e1d68 (diff)
downloadQt-b500b9f35fbeaa5f0fe8bfaf7b225518bf6e636f.zip
Qt-b500b9f35fbeaa5f0fe8bfaf7b225518bf6e636f.tar.gz
Qt-b500b9f35fbeaa5f0fe8bfaf7b225518bf6e636f.tar.bz2
use qBinaryFind instead of bsearch
depending on platform, qBinaryFind might be up to 3 times faster than bsearch or a bit bit slower at the worst case; so why don't simply use qBinaryFind instead of bsearch and be an optimist? ;) also we know that all color names are in the lower case already and thus we can avoid lowercasing of them again and again, and again; the name to find needs to be lowercased separately instead. some color-by-name-lookup numbers: before: 0.000737 msecs per iteration (total: 3,684, iterations: 5000000) after: 0.000154 msecs per iteration (total: 771, iterations: 5000000) Merge-request: 906 Reviewed-by: Olivier Goffart <olivier.goffart@nokia.com>
Diffstat (limited to 'src')
-rw-r--r--src/gui/image/qxpmhandler.cpp32
-rw-r--r--src/gui/painting/qcolor_p.cpp36
2 files changed, 15 insertions, 53 deletions
diff --git a/src/gui/image/qxpmhandler.cpp b/src/gui/image/qxpmhandler.cpp
index 453100c..6afb305 100644
--- a/src/gui/image/qxpmhandler.cpp
+++ b/src/gui/image/qxpmhandler.cpp
@@ -54,12 +54,6 @@
#include "qplatformdefs.h"
#endif
-#include <stdlib.h>
-
-#if defined(Q_OS_WINCE)
-#include "qguifunctions_wince.h"
-#endif
-
QT_BEGIN_NAMESPACE
static quint64 xpmHash(const QString &str)
@@ -751,27 +745,15 @@ static const struct XPMRGBData {
{ QRGB(139,139, 0), "yellow4" },
{ QRGB(154,205, 50), "yellowgreen" } };
-#if defined(Q_C_CALLBACKS)
-extern "C" {
-#endif
-static int rgb_cmp(const void *d1, const void *d2)
-{
- return qstricmp(((XPMRGBData *)d1)->name, ((XPMRGBData *)d2)->name);
-}
-#if defined(Q_C_CALLBACKS)
-}
-#endif
+inline bool operator<(const char *name, const XPMRGBData &data)
+{ return qstrcmp(name, data.name) < 0; }
+inline bool operator<(const XPMRGBData &data, const char *name)
+{ return qstrcmp(data.name, name) < 0; }
-static bool qt_get_named_xpm_rgb(const char *name_no_space, QRgb *rgb)
+static inline bool qt_get_named_xpm_rgb(const char *name_no_space, QRgb *rgb)
{
- XPMRGBData x;
- x.name = name_no_space;
- // Function bsearch() is supposed to be
- // void *bsearch(const void *key, const void *base, ...
- // So why (char*)? Are there broken bsearch() declarations out there?
- XPMRGBData *r = (XPMRGBData *)bsearch((char *)&x, (char *)xpmRgbTbl, xpmRgbTblSize,
- sizeof(XPMRGBData), rgb_cmp);
- if (r) {
+ const XPMRGBData *r = qBinaryFind(xpmRgbTbl, xpmRgbTbl + xpmRgbTblSize, name_no_space);
+ if (r != xpmRgbTbl + xpmRgbTblSize) {
*rgb = r->value;
return true;
} else {
diff --git a/src/gui/painting/qcolor_p.cpp b/src/gui/painting/qcolor_p.cpp
index b1adf9f..d66990d 100644
--- a/src/gui/painting/qcolor_p.cpp
+++ b/src/gui/painting/qcolor_p.cpp
@@ -49,9 +49,6 @@
#include "qrgb.h"
#include "qstringlist.h"
-#if defined(Q_WS_WINCE)
-#include "qguifunctions_wince.h"
-#endif
QT_BEGIN_NAMESPACE
static inline int h2i(char hex)
@@ -290,33 +287,16 @@ static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData);
#undef rgb
-QT_BEGIN_INCLUDE_NAMESPACE
-#include <stdlib.h>
-QT_END_INCLUDE_NAMESPACE
-
-#if defined(Q_C_CALLBACKS)
-extern "C" {
-#endif
-
-#ifdef Q_OS_WINCE
-static int __cdecl rgb_cmp(const void *d1, const void *d2)
-#else
-static int rgb_cmp(const void *d1, const void *d2)
-#endif
-{
- return qstricmp(((RGBData *)d1)->name, ((RGBData *)d2)->name);
-}
-
-#if defined(Q_C_CALLBACKS)
-}
-#endif
+inline bool operator<(const char *name, const RGBData &data)
+{ return qstrcmp(name, data.name) < 0; }
+inline bool operator<(const RGBData &data, const char *name)
+{ return qstrcmp(data.name, name) < 0; }
-static bool get_named_rgb(const char *name, QRgb *rgb)
+static bool get_named_rgb(const char *name_no_space, QRgb *rgb)
{
- RGBData x;
- x.name = name;
- RGBData *r = (RGBData*)bsearch(&x, rgbTbl, rgbTblSize, sizeof(RGBData), rgb_cmp);
- if (r) {
+ QByteArray name = QByteArray(name_no_space).toLower();
+ const RGBData *r = qBinaryFind(rgbTbl, rgbTbl + rgbTblSize, name.constData());
+ if (r != rgbTbl + rgbTblSize) {
*rgb = r->value;
return true;
}