[Interest] Qt 5.5.0 Compile Time Hash Example

md at rpzdesign.com md at rpzdesign.com
Mon Aug 24 14:07:37 CEST 2015


Helmut:

Yes, but you are talking about a different use case
with algorithmic purity.

We do not care about every string.

We only care about the strings in our program.

For quality assurance, you can scan the source code looking for every 
instance of the fnhash("STRING") and make sure there are no collisions
project wide.

But the gains in code readability should not be overlooked.

This is for programmer sanity, run-time cleanliness, switch efficiency, etc.

Collisions in any case are VERY low probability.

Have a great day!

md

On 8/23/2015 11:10 PM, Helmut Mülner wrote:
> 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
> <mailto: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######################
>     //VALIDC++14ConstantExpressionFunction(CompilesforIOSonQt5.5.0andXCode6.3.2,butdoesnotCompileforOSXDesktopwithClang)
>     /*
>     constexprunsignedintfnhash(constchar*gpstr)
>     {
>     unsignedinthash=0;
>     size_tgidx=0;
>     while(gpstr[gidx]>0)
>     {
>     hash=65599*hash+gpstr[gidx];
>     if(gidx>15)break;
>     gidx++;
>     }
>     returnhash^(hash>>16);
>     }
>     */
>     //######################
>     //C++11ConstantExpressionFunction
>
>     //Forward declaration
>     constexprunsignedintfnhashrec(unsignedinthash,constchar*str);
>
>     constexprunsignedintfnhash(constchar*str)
>     {
>     return(!str?0:fnhashrec(5381,str));
>     }
>
>     constexprunsignedintfnhashrec(unsignedinthash,constchar*str)
>     {
>     return(!*str?hash:fnhashrec(((hash<<5)+hash)+*str,str+1));
>     }
>     //######################
>
>
>     intmain(intargc,char*argv[])
>     {
>     QGuiApplicationgapp(argc,argv);
>     QQmlApplicationEnginegeng;
>
>
>     QStringgval="JOKER";
>
>          //NOW look at the run time eval from QString -> char*
>
>     switch(fnhash(gval.toUtf8().data()))
>     {
>     casefnhash("PETE"): //Compile time optimized
>     qDebug()<<"POORPETEISNOTAJOKER"<<endl;
>     break;
>     casefnhash("JOKER"): //Compile time optimized
>     qDebug()<<"BLOODYJOKERWORKED->SUCCESS"<<endl;
>     break;
>     }
>
>     geng.load(QUrl(QStringLiteral("qrc:/main.qml")));
>     returngapp.exec();
>     }
>
>
>     Cheers,
>
>     md
>
>
>
>     _______________________________________________
>     Interest mailing list
>     Interest at qt-project.org <mailto:Interest at qt-project.org>
>     http://lists.qt-project.org/mailman/listinfo/interest
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>

-- 
No spell checkers were harmed during the creation of this message.



More information about the Interest mailing list