Hash Function

F

flipdog

Hello all,
I didn't know there is a thread on hash function started in this newsgroup
so reposted my posting from another group.
Hope I can have some feedbacks. I am new to hash table.
I came across a very well presented tutorial web page wrt hash table. In
its content, it listed a number of hash functions which the web master(?)
quoted from other web sites. So no explainations for these hash functions
and I failed to understand a couple of them. They are quite similar so I
ask help for one. Wonder if someone can shed some light for me?


/*Peter Weinberger's*/
int hashpjw(char *s)
{
char *p;
unsigned int h,g;
h=0;
for (p=s; *p != '\0'; p++)
{
h = (h<<4) + *p;
if (g = h & 0xF0000000)
{
h ^= g>>24;
h ^= g;
}
}
return h%211;
}

Within the for loop, what exactly does the second line do? Bit shifting by
4 units and added to the particular char entry of the string?
The third line in the for loop, what is the purpose of bitwise and h and
0xF0000000? Why 0xF0000000 in the first place?
I failed to understand the logic of the next two lines within the
conditional statement as well?

Thanks in advance,
FD
 
A

Arthur J. O'Dwyer

Hello all,
I didn't know there is a thread on hash function started in this newsgroup
so reposted my posting from another group.

For future reference, it would be nice of you to mention *which*
other group contained the original post. And both for courtesy's
sake and the avoidance of unnecessary discussion [like this], it
would be nice of you to summarize what you've learned from that
other thread and why you still want more information.
Hope I can have some feedbacks. I am new to hash table.
I came across a very well presented tutorial web page wrt hash table.

...which can be found at the following URL: and so on.
Don't forget the common sense, please. [All I find is a
Spanish-language Postscript document, which may or may not be
the document to which you're referring.]
In its content, it listed a number of hash functions which the web master(?)
quoted from other web sites. So no explainations for these hash functions
and I failed to understand a couple of them. They are quite similar so I
ask help for one. Wonder if someone can shed some light for me?

If you want real information as to *why* the hash function is
designed the way it is, or information on how the various lines
interact, I recommend you try comp.programming or sci.crypt.
Hash functions, to me at least, are entirely "black magic," and
I don't expect any useful answers you might get would be on
topic for comp.lang.c, which only discusses the C programming
language (and not the higher math and statistical methods
necessary [IMLE] to fully understand hashing).
Here is what the function does, in "plain" English, since
it sounds like you're unfamiliar with C operators and syntax.

/*Peter Weinberger's*/
int hashpjw(char *s)
{
char *p;
unsigned int h,g;
h=0;
for (p=s; *p != '\0'; p++)
{

Loop over the text string 's', using the variable 'p' as
a sort of index into the string. This is not the way I'd
have written this function today, which implies to me that
this function is taken from an old (1980s or early 90s?)
source. [And a quick Google implies I'm pretty much right.
I hadn't recognized PJW as the W in "AWK"!]
h = (h<<4) + *p;

For each character in the string, add its numerical value
to the quantity obtained by shifting h left by four bits
(multiplying h by 16), and store the result back in h.
h is an "accumulator" or "state" variable (which probably
has another buzzword in crypto/hash jargon) -- it starts out
at zero, and then changes in some hopefully-unpredictable
fashion each time through the loop.
if (g = h & 0xF0000000)

Compute the bitwise AND of h and the hexadecimal constant
0xF0000000 (the high-order four bits of h) and store the
result in g.
{
h ^= g>>24;
h ^= g;
}

Bitwise XOR the value in h with the value of g shifted
right by 24 bits, and then XOR that result with the
original (unshifted) value of g. Store the result back
in h.
Note that the 'if ( ) { }' is unnecessary here; it merely
skips the last two steps if the value of g is zero (in
which case they would have no effect). In other words,
it's an optimization -- one that might not really matter
on modern computers anyway, but *that's* a topic for
comp.arch or some such newsgroup. :)

Continue looping over the characters in 's'.
return h%211;

Return the value of the state variable h, modulo 211,
which is simply a small prime. This discards a lot of
information out of the high-order digits of h, but that's
okay because h has been quite well mixed-up by the loop
operations.
}

Within the for loop, what exactly does the second line do? Bit
shifting by 4 units and added to the particular char entry of the
string?

Yes. Characters in C are treated as small integers; most
[but not all] systems these days use [a superset of] ASCII encoding,
which has, for instance, 'A'=65, 'd'=100, and so on. The exact
encoding doesn't matter here, of course -- you could just as easily
think of 'hashpjw' as operating on a list of byte values.
The third line in the for loop, what is the purpose of bitwise and h and
0xF0000000? Why 0xF0000000 in the first place?
I failed to understand the logic of the next two lines within the
conditional statement as well?

Black magic. Consult sci.crypt for the details. [Maybe
someone else can suggest a better group; I know sci.crypt
covers secure hash functions, but I don't know if there might
be a more beginner-oriented, hash- or math-related group out
there.]

HTH,
-Arthur
 
C

CBFalconer

flipdog said:
I came across a very well presented tutorial web page wrt hash
table. In its content, it listed a number of hash functions
which the web master(?) quoted from other web sites. So no
explainations for these hash functions and I failed to
understand a couple of them. They are quite similar so I ask
help for one. Wonder if someone can shed some light for me?

/*Peter Weinberger's*/
int hashpjw(char *s)
{
char *p;
unsigned int h,g;
h=0;
for (p=s; *p != '\0'; p++)
{
h = (h<<4) + *p;
if (g = h & 0xF0000000)
{
h ^= g>>24;
h ^= g;
}
}
return h%211;
}

Within the for loop, what exactly does the second line do? Bit shifting by
4 units and added to the particular char entry of the string?
The third line in the for loop, what is the purpose of bitwise and h and
0xF0000000? Why 0xF0000000 in the first place?
I failed to understand the logic of the next two lines within the
conditional statement as well?

That is picking off the high 4 bits of the (assumed) 32 bit
integer. The function is not portable, because it depends on the
size of an int. The following are the generic functions I
referred you to in my earlier post, which are portable.

/* ============= Useful generic functions ============= */

/* NOTE: hash is called once per operation, while rehash is
called _no more_ than once per operation. Thus it
is preferable that hash be the more efficient.
*/

/* Hash a string quantity */
unsigned long hshstrhash(const char * string)
{
unsigned long h;

h = 0;
while (*string) {
h = h * 31UL + (unsigned char) *string++;
}
return h;
} /* hshstrhash */

/* 1------------------1 */

/* ReHash a string quantity */
unsigned long hshstrehash(const char * string)
{
unsigned long h;

h = 0;
while (*string) {
h = h * 37UL + (unsigned char) *string++;
}
return h;
} /* hshstrehash */
 
T

Toni Uusitalo

flipdog said:
Hello all,
I didn't know there is a thread on hash function started in this newsgroup
so reposted my posting from another group.
Hope I can have some feedbacks. I am new to hash table.

Have you got a working hashtable code? Did the page yout mentioned
provide source for hashtable? Hashing function is of course part of that
but usually you have to trust to "black magic" of tried and tested hashing
algorithm (mr. O'Dryer covered that black magic
part already).

You just have to rely on benchmarks like
http://www.cs.usyd.edu.au/~scilect/se/2000/lectures/lect02-2.htm
to choose appropriate hash function/algorithm for your table. Of course
benchmarking
yourself and/or running some "hashtable_inserts" with debugger would be very
educational.

here's one hashtable implementation (google for more yourself)
http://c.snippets.org/snip_lister.php?fname=hash.c
http://c.snippets.org/snip_lister.php?fname=hash.h

with respect,
Toni Uusitalo
 
A

Andrew Slade

"Toni Uusitalo"
Have you got a working hashtable code? Did the page yout mentioned
provide source for hashtable? Hashing function is of course part of that
but usually you have to trust to "black magic" of tried and tested hashing
algorithm (mr. O'Dryer covered that black magic
part already).

You just have to rely on benchmarks like
http://www.cs.usyd.edu.au/~scilect/se/2000/lectures/lect02-2.htm
to choose appropriate hash function/algorithm for your table. Of course
benchmarking
yourself and/or running some "hashtable_inserts" with debugger would be very
educational.

here's one hashtable implementation (google for more yourself)
http://c.snippets.org/snip_lister.php?fname=hash.c
http://c.snippets.org/snip_lister.php?fname=hash.h

with respect,
Toni Uusitalo

Thank you for this post. I have been searching for information like this
with google on my own and getting gazillions of pages concerning java and "c
sharp" with a few C++ pages thrown in. Perhaps my choice of search strings
was bad....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top