# what's the best hash function?

Discussion in 'C Programming' started by vietor.liu, Nov 25, 2005.

1. ### vietor.liuGuest

is this?

int hashpjw(char* str,int size)
{
unsigned long h=0,g;
for(unsigned char *p=(unsigned char*)str;*p;++p)
{
h=(h<<4)+*p;
if(g=(h&0xf0000000))
{
h^=(g>>24);
h^=g;
}
}
return h%size;
}

vietor.liu, Nov 25, 2005

2. ### Chris McDonaldGuest

Re: what's the best hash function?

"vietor.liu" <> writes:

>is this?

>int hashpjw(char* str,int size)
>{
> unsigned long h=0,g;
> for(unsigned char *p=(unsigned char*)str;*p;++p)
> {
> h=(h<<4)+*p;
> if(g=(h&0xf0000000))
> {
> h^=(g>>24);
> h^=g;
> }
> }
> return h%size;
>}

<OT>
The answer totally depends on the type and range of inputs,
and the purpose of the hash result. Read onwards:

http://burtleburtle.net/bob/hash/evahash.html
</OT>

--
Chris.

Chris McDonald, Nov 25, 2005

3. ### Jack KleinGuest

On Fri, 25 Nov 2005 10:21:54 +0800, "vietor.liu"
<> wrote in comp.lang.c:

Define "best".

> is this?
>
> int hashpjw(char* str,int size)
> {
> unsigned long h=0,g;
> for(unsigned char *p=(unsigned char*)str;*p;++p)
> {
> h=(h<<4)+*p;
> if(g=(h&0xf0000000))
> {
> h^=(g>>24);
> h^=g;
> }
> }
> return h%size;
> }
>

If you want to know the "best" hashing algorithm, ask in
news:comp.programming. And supply a definition of what you consider
to be "best".

Once you have selected an algorithm, if you have problems coding it
correctly in standard C, then you can post your code and description
of your problem here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

Jack Klein, Nov 25, 2005
4. ### MabdenGuest

What is the best colour?

--
Mabden

Mabden, Dec 5, 2005
5. ### Joe EstockGuest

Mabden wrote:
> What is the best colour?
>

Red. No wait, Purple. On second thought, make that Green.

I have no idea what it is you are asking since both the subject line and
the body of your post are completely different and no context is
provided in either. Please be more descriptive when you post as it helps
us better help you.

Joe

Joe Estock, Dec 5, 2005
6. ### Richard HeathfieldGuest

Joe Estock said:

> Mabden wrote:
>> What is the best colour?
>>

>
> Red. No wait, Purple. On second thought, make that Green.

' . .
ea e o ee e o a
Pl s d n t f d th tr lls Th nks

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield, Dec 5, 2005
7. ### Christopher Benson-ManicaGuest

Joe Estock <> wrote:

> Red. No wait, Purple. On second thought, make that Green.

And what is the airspeed velocity of an unladen swallow?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Christopher Benson-Manica, Dec 5, 2005
8. ### SkarmanderGuest

Christopher Benson-Manica wrote:
> Joe Estock <> wrote:
>
>> Red. No wait, Purple. On second thought, make that Green.

>
> And what is the airspeed velocity of an unladen swallow?
>

Please read http://www.ungerhu.com/jxh/clc.welcome.txt. Your question is
off-topic for this group. You may have more success getting answers in
comp.lang.python -- this may be a FAQ there.

S.

Skarmander, Dec 5, 2005
9. ### peteGuest

[ot]Re: what's the best hash function?

Christopher Benson-Manica wrote:
>
> Joe Estock <> wrote:
>
> > Red. No wait, Purple. On second thought, make that Green.

>
> And what is the airspeed velocity of an unladen swallow?

http://faultgame.com/images/quest.wav

--
pete

pete, Dec 5, 2005
10. ### David ResnickGuest

Re: what's the best hash function?

Christopher Benson-Manica wrote:
> Joe Estock <> wrote:
>
> > Red. No wait, Purple. On second thought, make that Green.

>
> And what is the airspeed velocity of an unladen swallow?
>
> --
> Christopher Benson-Manica | I *should* know what I'm talking about - if I
> ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

This really sounds like homework. Besides, it is an algorithm
question, so comp.programming would be a better place.
Maybe you should try to write a C program to calculate
the average velocity of a number of swallows. If you
present your efforts, I'm sure people will try to give you a
hand. <OT> Hint -- it matters whether it is African or
European </OT>

-David

David Resnick, Dec 5, 2005
11. ### Mark McIntyreGuest

On Mon, 05 Dec 2005 15:49:26 +0100, in comp.lang.c , Skarmander
<> wrote:

>Christopher Benson-Manica wrote:
>
>> And what is the airspeed velocity of an unladen swallow?
>>

>You may have more success getting answers in
>comp.lang.python -- this may be a FAQ there.

rofl!!!

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Dec 5, 2005
12. ### Mark McIntyreGuest

On Mon, 5 Dec 2005 14:44:43 +0000 (UTC), in comp.lang.c , Christopher
Benson-Manica <> wrote:

>Joe Estock <> wrote:
>
>> Red. No wait, Purple. On second thought, make that Green.

>
>And what is the airspeed velocity of an unladen swallow?

African or European? http://www.style.org/unladenswallow/
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Dec 5, 2005
13. ### Jordan AbelGuest

On 2005-12-05, Joe Estock <> wrote:
> Mabden wrote:
>> What is the best colour?
>>

>
> Red. No wait, Purple. On second thought, make that Green.
>
> I have no idea what it is you are asking since both the subject line and
> the body of your post are completely different and no context is
> provided in either. Please be more descriptive when you post as it helps
> us better help you.

It seems to me that he's responding to the question in the subject with
a rhetorical question, rather than responding to the body of the post he
is in reply to.

Jordan Abel, Dec 5, 2005
14. ### MabdenGuest

"Richard Heathfield" <> wrote in message
news:dn1162\$jue\$-infra.bt.com...
> Joe Estock said:
>
> > Mabden wrote:
> >> What is the best colour?
> >>

> >
> > Red. No wait, Purple. On second thought, make that Green.

>
> ' . .
> ea e o ee e o a
> Pl s d n t f d th tr lls Th nks
>

Buuuuurrrrp!

Seriously, it was an answer to the post. How can one say what the best
hash function is without knowing the data? I think my answer was quite
appropos.

--
Mabden

Mabden, Dec 6, 2005
15. ### MabdenGuest

"Christopher Benson-Manica" <> wrote in
message news:dn1jor\$8no\$...
> Joe Estock <> wrote:
>
> > Red. No wait, Purple. On second thought, make that Green.

>
> And what is the airspeed velocity of an unladen swallow?
>

http://www.style.org/unladenswallow/

"the average cruising airspeed velocity of an unladen European Swallow
is roughly 11 meters per second, or 24 miles an hour. "

--
Mabden

Mabden, Dec 6, 2005
16. ### Joe EstockGuest

Mabden wrote:
> "Richard Heathfield" <> wrote in message
> news:dn1162\$jue\$-infra.bt.com...
>
>>Joe Estock said:
>>
>>
>>>Mabden wrote:
>>>
>>>>What is the best colour?
>>>>
>>>
>>>Red. No wait, Purple. On second thought, make that Green.

>>
>> ' . .
>> ea e o ee e o a
>>Pl s d n t f d th tr lls Th nks
>>

>
>
> Buuuuurrrrp!
>
> Seriously, it was an answer to the post. How can one say what the best
> hash function is without knowing the data? I think my answer was quite
> appropos.
>

If enough of the original post were left in your reply then I would be
inclined to agree, however I had nothing but the text I replied to and
misinterpreted your reply. Yet another classic example of why you should
quote enough text from the previous post so that someone looking at the
thread for the very first time has enough information to understand your
reply. For all I know you could have been the OP replying to your
original thread.

Joe

Joe Estock, Dec 6, 2005
17. ### MabdenGuest

"Joe Estock" <> wrote in message
news:5H9lf.599651\$x96.384071@attbi_s72...
> Mabden wrote:
> > "Richard Heathfield" <> wrote in message
> > news:dn1162\$jue\$-infra.bt.com...
> >>Joe Estock said:
> >>>Mabden wrote:
> >>>>What is the best colour?
> >>>Red. No wait, Purple. On second thought, make that Green.
> >> ' . .
> >> ea e o ee e o a
> >>Pl s d n t f d th tr lls Th nks

> >
> > Buuuuurrrrp!
> >
> > Seriously, it was an answer to the post. How can one say what the

best
> > hash function is without knowing the data? I think my answer was

quite
> > appropos.

>
> If enough of the original post were left in your reply then I would be
> inclined to agree, however I had nothing but the text I replied to and
> misinterpreted your reply. Yet another classic example of why you

should
> quote enough text from the previous post so that someone looking at

the
> thread for the very first time has enough information to understand

your
> reply. For all I know you could have been the OP replying to your
> original thread.
>

It really wouldn't help. The subject says it all, but here it is, in all
its 2-word glory:
======================
is this?

int hashpjw(char* str,int size)
{
unsigned long h=0,g;
for(unsigned char *p=(unsigned char*)str;*p;++p)
{
h=(h<<4)+*p;
if(g=(h&0xf0000000))
{
h^=(g>>24);
h^=g;
}
}
return h%size;
}

======================

Now, does that really need to have been included. I think not.

I await your apology with anticip...
..
..
..
..
ation

--
Mabden

Mabden, Dec 7, 2005
18. ### Joe EstockGuest

[OT] Re: what's the best hash function?

Mabden wrote:
[snip irrelevant stuff to my reply]
>>If enough of the original post were left in your reply then I would be
>>inclined to agree, however I had nothing but the text I replied to and
>>misinterpreted your reply. Yet another classic example of why you should
>>quote enough text from the previous post so that someone looking at
>>the thread for the very first time has enough information to understand
>>your reply. For all I know you could have been the OP replying to your
>>original thread.
>>

>
>
>
> It really wouldn't help. The subject says it all, but here it is, in all
> its 2-word glory:
> ======================
> is this?
>

[snip 14 lines of code for hash function]
>
> ======================
>
> Now, does that really need to have been included. I think not.

I agree that it did not need to be included, however you could have at
least included some context such as [snip useless function] so that
others who do not have access to the original thread can better
understand why you replied as you did.

>
> I await your apology with anticip...

[snip gratuitous waste of newlines]
> ation
>

None needed since I said nothing that demands an apology. I was simply
pointing out that if you had quoted (or at least noted that you trimmed)
the original post that I would not have replied as I did. You don't read
the last message in a thread without first reading the original post and
neither do I.

Joe

Joe Estock, Dec 8, 2005
19. ### Mark McIntyreGuest

Re: [OT] Re: what's the best hash function?

On Thu, 08 Dec 2005 06:19:53 GMT, in comp.lang.c , Joe Estock
<> wrote:

>Mabden wrote:
>>
>> I await your apology with anticip...

>[snip gratuitous waste of newlines]

I fear the joke went over your head.

>> ation

>
>None needed

And you shall recieve it, in abundance.

gd&r

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Dec 8, 2005
20. ### Jordan AbelGuest

Re: [OT] Re: what's the best hash function?

On 2005-12-08, Joe Estock <> wrote:
>>
>> I await your apology with anticip...

> [snip gratuitous waste of newlines]

it's not like we're killing trees, here - they probably only occupy a
byte apiece in storage and two over the wire. Your complaint was almost
certainly bigger.

>> ation
>>

Jordan Abel, Dec 8, 2005

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.