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 4 7257
On Sat, 20 Dec 2003, flipdog wrote: 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.programmin g 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 "accumulato r" 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
flipdog wrote: 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(cons t 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(con st char * string)
{
unsigned long h;
h = 0;
while (*string) {
h = h * 37UL + (unsigned char) *string++;
}
return h;
} /* hshstrehash */
--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
"flipdog" <jo******@world net.att.net> wrote in message
news:Pq******** *************** @bgtnsc04-news.ops.worldn et.att.net... 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/s...s/lect02-2.htm
to choose appropriate hash function/algorithm for your table. Of course
benchmarking
yourself and/or running some "hashtable_inse rts" 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
"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/s...s/lect02-2.htm to choose appropriate hash function/algorithm for your table. Of course benchmarking yourself and/or running some "hashtable_inse rts" 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.... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Markus Dehmann |
last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have
millions of inserts and many more lookups, and my profiler found that they
cost a lot of runtime.
Therefore, I want to substitute the set<Data*> with a hash_set<Data*>:
typedef hash_set<const Data*, hash<const Data*>, eqData> DataPointerHashSet;
// typedef set<Data*> DataPointerHashSet; // (see complete code below)
But it doesn't work! Everything is fine...
|
by: Bryan Olson |
last post by:
The current Python standard library provides two cryptographic
hash functions: MD5 and SHA-1 . The authors of MD5 originally
stated:
It is conjectured that it is computationally infeasible to
produce two messages having the same message digest.
That conjecture is false, as demonstrated by Wang, Feng, Lai and
Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short-
cut solution for finding collisions in SHA-1 . Their result
|
by: barcaroller |
last post by:
I'm looking for a hash function (in C) that will convert a string of
arbitrary length (but less than 1024 chars) to a reasonably-unique 16-bit
short integer. Can anyone point me to such a hash function?
|
by: thecodemachine |
last post by:
Hi,
I'm looking for a fast and simple one to one hash function, suitable
for longer strings (up to 2048 in length). I'd like keys to be
relatively short, I doubt I'd be creating more than 256 keys..
I could use md5 but even that is more than I need, and I assume not the
fastest algorithm (?), so I'm reluctant to use it.
I've been looking around a lot, not finding much anything come to mind?
|
by: Arash Partow |
last post by:
Hi all,
I've ported various hash functions to python if anyone is interested:
def RSHash(key):
a = 378551
b = 63689
hash = 0
| |
by: Johan Tibell |
last post by:
I would be grateful if someone had a minute or two to review my hash
table implementation. It's not yet commented but hopefully it's short
and idiomatic enough to be readable. Some of the code (i.e. the
get_hash function) is borrowed from various snippets I found on the
net. Thee free function could probably need some love. I have been
thinking about having a second linked list of all entries so that the
cost of freeing is in proportion to...
|
by: Hallvard B Furuseth |
last post by:
Is the code below valid? Generally a value must be accessed
through the same type it was stored as, but there is an exception
for data stored through a character type. I'm not sure if that
applies in this case though:
#include <limits.h>
unsigned foo(void) {
static const union {
unsigned char str;
|
by: Douglas Dude |
last post by:
how much time does it take to lok for a value - key in a hash_map ?
Thanks
|
by: ravi |
last post by:
Hi
can anybody tell me that which ds will be best suited to implement a
hash table in C/C++
thanx. in advanced
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |