[Interest] Qt 5.5.0 Compile Time Hash Example

Helmut Mülner helmut.muelner at gmail.com
Mon Aug 24 07:10:45 CEST 2015


This will not work in every case because you cannot guarantee that your
hash function generates a unique value for every string. In hash based
containers you still have to compare against the valueof the string.
Am 24.08.2015 01:27 schrieb "mark diener" <rpzrpzrpz at gmail.com>:

> Hello List:
>
> Have you ever wanted an efficient switch statement on QString values?
>
> There are many references to the subject with various algorithms, but they
> all have some major flaw in the implementation.
>
> For those looking for a way to have compile and run time hash on their
> strings, I spent a little while doing some research on the subject and
> wanted to share back to Qt community:
>
> The tests ran successfully with the C++11 example for OSX/IOS Xcode 6.3.2
> and Android NDK 10 r10e,
>
> I went ahead and tested the function on MSVC 2013 with CTP November and it
> failed.  MSVC 2015 worked, but I could not build the QT 5.5.0 source code
> successfully, so I just built a console app in MSVC2015 and the C++11
> function worked.
>
> The C++14 example did not work on OSX or Android, but it did work on IOS.
> With 2 out of 3 failures, I did not feel motivated to test for MSVC 2015 on
> Windows 10.
>
> So for those wanting to verify and make use of it, here are the required
> code snippets:
>
> First, in your .PRO file:
>
> #CONFIG += c++14
>
> CONFIG += c++11
>
>
> Now for a source sample:
>
>
> //main.cpp ######################//VALID C++14 Constant Expression Function (Compiles for IOS on Qt5.5.0 and XCode 6.3.2, but does not Compile for OSX Desktop with Clang)/*constexpr unsigned int fnhash(const char *gpstr){    unsigned int hash = 0;    size_t gidx = 0 ;    while (gpstr[gidx] > 0)    {        hash = 65599 * hash + gpstr[gidx];        if (gidx > 15) break ;        gidx++ ;    }    return hash ^ (hash >> 16);}*/// ######################//C++11 Constant Expression Function
> //Forward declarationconstexpr unsigned int fnhashrec(unsigned int hash, const char* str) ;
> constexpr unsigned int fnhash(const char* str){   return ( !str ? 0 : fnhashrec(5381, str) );}
> constexpr unsigned int fnhashrec(unsigned int hash, const char* str){   return ( !*str ? hash : fnhashrec(((hash << 5) + hash) + *str, str + 1));}// ######################
> int main(int argc, char *argv[]){    QGuiApplication gapp(argc, argv);    QQmlApplicationEngine geng;
>     QString gval = "JOKER" ;
>
>     //NOW look at the run time eval from QString -> char*
>
>     switch (fnhash(gval.toUtf8().data()))    {    case fnhash("PETE"):  //Compile time optimized        qDebug() << "POOR PETE IS NOT A JOKER" << endl ;        break ;    case fnhash("JOKER"): //Compile time optimized        qDebug() << "BLOODY JOKER WORKED -> SUCCESS" << endl ;        break ;    }
>     geng.load(QUrl(QStringLiteral("qrc:/main.qml")));    return gapp.exec();}
>
> Cheers,
>
> md
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20150824/69f23b6d/attachment.html>


More information about the Interest mailing list