hash() algorithm? 
Author Message
 hash() algorithm?

Does anybody know what the built-in hash function in python's
algorithm/code is?

I tried doing

hash.func_code but it doesn't work.

Please help!
-Anand



Thu, 22 May 2003 03:00:00 GMT  
 hash() algorithm?

Quote:

> Does anybody know what the built-in hash function in python's
> algorithm/code is?

Each object type implement it's own hash algorithm, via the __hash__
slot.  Integers return themselves, strings return a combination of their
characters, tuples a combination of the hash values of their members,
etc.  And it's also implementation dependent, of course.

What exactly are you trying to do?

</F>



Thu, 22 May 2003 03:00:00 GMT  
 hash() algorithm?

Quote:

> What exactly are you trying to do?

Well, i'm writing a hashing function for my Computer Science class.

I'm trying to figure out how the python one works because it seems to be
much better than the other one's i've found.

Is there a way to find this out?  
I wasn't able to find anything on the hash alg. on python.org.

-A



Thu, 22 May 2003 03:00:00 GMT  
 hash() algorithm?

Quote:
> I'm trying to figure out how the Python one works because it seems to be
> much better than the other one's i've found.

> Is there a way to find this out?

Look in the source code (don't know the file).


Fri, 23 May 2003 03:00:00 GMT  
 hash() algorithm?

Quote:

> Look in the source code (don't know the file).

.../Objects/stringobject.c, function string_hash().

--
         Carey Evans  http://home.clear.net.nz/pages/c.evans/



Fri, 23 May 2003 03:00:00 GMT  
 hash() algorithm?

Quote:

> .../Objects/stringobject.c, function string_hash().

cool!
Although I'm having trouble finding that file....

Is the path diffent for the mac distribution?  Because I don't have a
Objects directory, and stringobject.c doesn't exist anywhere.



Fri, 23 May 2003 03:00:00 GMT  
 hash() algorithm?

Quote:

> Is the path diffent for the mac distribution?  Because I don't have a
> Objects directory, and stringobject.c doesn't exist anywhere.

no, it's not different -- stringobject.c is part of the inter-
preter's SOURCE code.  if you're looking at a binary release,
you won't find it.

source code is available here:

    http://www.python.org

and here's the relevant portion of the string hash
algorithm:

static long
string_hash(PyStringObject *a)
{
 register int len;
 register unsigned char *p;
 register long x;

 len = a->ob_size;
 p = (unsigned char *) a->ob_sval;
 x = *p << 7;
 while (--len >= 0)
  x = (1000003*x) ^ *p++;
 x ^= a->ob_size;
 if (x == -1)
  x = -2;
 return x;

Quote:
}

as I mentioned before, other data types use different
hash algorithms.  check the sources for details.

</F>



Fri, 23 May 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. MD5 Hash Algorithm or class

2. Simple Hashing algorithm or description?

3. Secure Hash Algorithm 1 (SHA1) Clipper

4. Secure Hash Algorithm 1 (SHA1) Clipper

5. Hash-algorithm

6. Secure Hash Algorithm (SHA-1)

7. A decent hashing algorithm

8. A decent hashing algorithm

9. Hash Algorithm

10. Secure Hash Algorithm ("SHA-1")

11. Hashing Algorithms

12. Secure Hash Algorithm

 

 
Powered by phpBB® Forum Software