1from __future__ import absolute_import, print_function, division
2from theano.gof.utils import hash_from_code
3
4
5def hash_from_sparse(data):
6    # We need to hash the shapes as hash_from_code only hashes
7    # the data buffer. Otherwise, this will cause problem with shapes like:
8    # (1, 0) and (2, 0)
9    # We also need to add the dtype to make the distinction between
10    # uint32 and int32 of zeros with the same shape.
11
12    # Python hash is not strong, so use sha256 instead. To avoid having a too
13    # long hash, I call it again on the contatenation of all parts.
14    return hash_from_code(hash_from_code(data.data) +
15                          hash_from_code(data.indices) +
16                          hash_from_code(data.indptr) +
17                          hash_from_code(str(data.shape)) +
18                          hash_from_code(str(data.dtype)) +
19                          hash_from_code(data.format))
20