Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
Hello.
What does this code do?
And what is the purpose of the "for" while?
int hash(const char *str) 
{
    if (str != 0)
    {
        unsigned h = str [0];
        for (int i = 1; str [i] != 0; ++i)
            h = (h << 5) - h + str [i];
        return h % HASH_SIZE; // remainder
    }
    else
    {
        return -1;
    }
}
Posted 31-Aug-12 13:57pm
smss IR399
Comments
Sandeep Mewara at 31-Aug-12 23:58pm
   
Assignment?
 
Where from you picked the code that you have no idea on what is happening?
Member 8238193 at 1-Sep-12 17:17pm
   
I picked this code from WrightEagleBASE-3.0.0 (www.wrighteagle.org/2d) and in the ParamEngine.cpp:116
:D
Sumal.V at 3-Sep-12 8:57am
   
Hey if you want to learn, then pick up a book and start. Learn the concept and look for examples based on that concept. like examples for threads, functions, arrays and so on. That way you can at least understand what is going on in there, rather than picking up some random code and breaking your head.
Mohibur Rashid at 1-Sep-12 0:46am
   
If you do not understand this code then learn programming
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

This seems to be the java string hashing algorithm[^]. It is a variant of the djb2[^] with different constants[^]. Here[^] you can find some explanation about the constants of djb2.
  Permalink  
Comments
Member 8238193 at 25-Sep-12 11:25am
   
Thanks my friend, but the djb2 algorithm is different from this. I wanna to know the this hash algorithm.
Member 8238193 at 25-Sep-12 11:25am
   
The djb2 formula is : hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
but in this algorithm the code is different : h = (h << 5) - h + str [i];
pasztorpisti at 25-Sep-12 11:44am
   
Yes. Because it uses 31 instead of 33 thats why I told you its java.
(h<<5)-h == h*31
I think the above equation clarifies some things, the original shifted version is "manual optimization" for older C compilers.
Member 8238193 at 25-Sep-12 18:44pm
   
Thank you my friend, but I still do not know the mean of the "hash", I think "hash" point to the count of the arguments in a string, is this true?
pasztorpisti at 25-Sep-12 19:32pm
   
Please read these:
http://en.wikipedia.org/wiki/Hash_function
http://en.wikipedia.org/wiki/Hash_table
If you still have concenrs than search for hash-related tutorials with google, sooner or later you will get it. If you don't succeed then go on learning easier material until you reach a level to understand hashing.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

See here[^], and preceding and succeeding pages.
  Permalink  
Comments
Member 8238193 at 1-Sep-12 17:21pm
   
I know "for" and "while", but I do not know the "hash" function target's ...
cariolihome at 1-Sep-12 19:45pm
   
Hash function is http://en.wikipedia.org/wiki/Hash_function
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try giving different strings as input and seeing what the output is. And debugging through the code to inspect the different values of h as the for loop progresses; and, of course, the final value of h. You need to define HASH_SIZE towards the beginning in the program.
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 275
1 OriginalGriff 230
2 BillWoodruff 173
3 CPallini 155
4 Kamal Rocks 144
0 OriginalGriff 5,655
1 DamithSL 4,506
2 Maciej Los 3,997
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 1 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100