From: Sylvia! [mailto:[EMAIL PROTECTED]

>Hi,
>
>I want to implement a hash routine for IP addresses. Basically
>if f(x) is my hash formula then i would want an efficient f(x)
>such that f(x) on most of the IP addresses gives me unique
>values so that i dont have collisions. I can have a limited
>array size (say 255) and i have to use this. Any clues.
>
>The formula that I have in my mind is simple. I will calculate
>the hash as (IP Address value as unsigned int) % HASH_SIZE,
>where HASH_SIZE is 255.
>
>Anybody aware of any better formula?

Not necessarily better. Depends on the variety of IP addresses you're
expecting, for example are they all likely to be in one of the 'local'
ranges? (10.0.0.1 .. 10.255.255.254, 172.16.0.1 .. 172.31.255.254 or
192.168.0.1 .. 192.168.255.254) or global?


Anyway, assuming a prototype of

unsigned char f(unsigned long ipaddress){

There are a few ways you could do it:


            return ipaddress % 255; /* your method */


            return ipaddress & 0xff; /* last octect of IP address -
slightly different, one of 256*/


            return (ipaddress &0xff) ^
                        ((ipaddress >> 8) &0xff) ^
                        ((ipaddress >> 16) &0xff) ^
                        ((ipaddress >> 24) &0xff); /* XOR all
bytes of address - again, 0-255*/


            srand(ipaddress);
            return rand() / (RAND_MAX / 256 + 1); /* plain weird */


--
PJH

"Consistently separating words by spaces became a general custom about
the tenth century A.D., and lasted until about 1957, when FORTRAN
abandoned the practice." -- Sun FORTRAN Reference Manual



Alderley plc, Arnolds Field Estate, The Downs, Wickwar, Gloucestershire, GL12 8JD, UK
Tel: +44(0)1454 294556 Fax: +44 (0)1454 299272

Website : www.alderley.com  Sales : [EMAIL PROTECTED] Service : [EMAIL PROTECTED]

This email and its contents are confidential and are solely for the use of the intended recipient. If you are not the original recipient you have received it in error and any use, dissemination, forwarding, printing or copying of this email is strictly prohibited. Should you receive this email in error please immediately notify [EMAIL PROTECTED]

This email has been scanned for viruses, however you should always scan emails with your own systems prior to opening.






To unsubscribe, send a blank message to <mailto:[EMAIL PROTECTED]>.


Yahoo! Groups Sponsor
ADVERTISEMENT
click here
Web Bug from http://us.adserver.yahoo.com/l?M=315388.5526708.6599542.3001176/D=groups/S=:HM/A=2372354/rand=793961375


Yahoo! Groups Links

Reply via email to