[SciPy-dev] are arrays hashable?

Robert Kern rkern at ucsd.edu
Tue Sep 20 15:19:56 CDT 2005

Travis Oliphant wrote:

> Yes, the new array scalars should be hashable (because they are
> immutable).  I've probably overlooked something.  I'll look into it.

You need to implement tp_hash functions for all of the scalar types. It
looks like it shouldn't be too bad. I think that except for the
extra-long types, you can just piggyback on the implementations for
int(), float(), and complex() objects. For the unsigned integer types,
just cast to a long. For the signed, cast to a long (and if the casted
version == -1, return -2 since -1 signals an error). For the float
types, I think it's going to be acceptable to cast to a double and use
_Py_HashDouble(). I have not the slightest idea what void objects should
do. Possibly just return the hash of the pointer itself.

For the extra long types ("long long", "unsigned long long", "long
double", "complex long double"), you have to check if they are within
the range of the regular types ("long", "unsigned long", etc.) and be
sure to return the same hash value. I'm not sure what the right answer
is for the other values, though.

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

More information about the Scipy-dev mailing list