[sugar] python hash function limited to specific table size?

Dan Williams dcbw at redhat.com
Thu Mar 15 09:49:16 EDT 2007


On Thu, 2007-03-15 at 13:31 +0100, Bert Freudenberg wrote:
> On Mar 15, 2007, at 13:09 , Dan Williams wrote:
> 
> > Hi,
> >
> > Does anyone know if python has a hash function that one can pass a  
> > size
> > to, to limit the returned hash value to a specific range?
> >
> > I've got an array with ~520 color pairs, and I'd like to take an
> > arbitrary string of bytes, hash that, and use the hash as an index  
> > into
> > that table to pull out the same color pair for the same bytes every
> > time.
> >
> > As I understand it, Python's built-in hash function for the base types
> > is locked and optimizied to a table size of 2^32 or something like  
> > that.
> 
> Err, wouldn't you normally just take that hash modulo your table size?

Hmm; right.  I was thinking that would reduce the spread/effectiveness
of the hash algorithm, rather than allowing the algorithm to understand
the size required and possibly incorporate that in a better manner.  I
appear to be overthinking.  Bad Dan.

Dan




More information about the Sugar mailing list