diff options
author | Campbell Barton <ideasman42@gmail.com> | 2015-02-15 02:46:14 +0300 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2015-02-15 06:02:08 +0300 |
commit | fa2f7c69acc55fd1fe3db0610ad319d08d77d79e (patch) | |
tree | 004780e19ea70ec8ee5eb6b26cde8ace90558a70 /source/blender/python/mathutils/mathutils_Matrix.c | |
parent | a9d979c8ef2b6de25c1953da341dd5e207416540 (diff) |
mathutils: Implement __hash__() functions
- all mathutils types
- only works on frozen data (so vectors can be used in sets/dict keys)
- uses same method as CPython, (matches hashing a tuple)
D1104 by @juicyfruit with own modifications
Diffstat (limited to 'source/blender/python/mathutils/mathutils_Matrix.c')
-rw-r--r-- | source/blender/python/mathutils/mathutils_Matrix.c | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/source/blender/python/mathutils/mathutils_Matrix.c b/source/blender/python/mathutils/mathutils_Matrix.c index 7b72ae5cebe..ce907091f65 100644 --- a/source/blender/python/mathutils/mathutils_Matrix.c +++ b/source/blender/python/mathutils/mathutils_Matrix.c @@ -895,6 +895,19 @@ static void matrix_copy(MatrixObject *mat_dst, const MatrixObject *mat_src) memcpy(mat_dst->matrix, mat_src->matrix, sizeof(float) * (mat_dst->num_col * mat_dst->num_row)); } +/* transposes memory layout, rol/col's don't have to match */ +static void matrix_transpose_internal(float mat_dst_fl[], const MatrixObject *mat_src) +{ + unsigned short col, row; + unsigned int i = 0; + + for (row = 0; row < mat_src->num_row; row++) { + for (col = 0; col < mat_src->num_col; col++) { + mat_dst_fl[i++] = MATRIX_ITEM(mat_src, row, col); + } + } +} + /* assumes rowsize == colsize is checked and the read callback has run */ static float matrix_determinant_internal(const MatrixObject *self) { @@ -2040,6 +2053,21 @@ static PyObject *Matrix_richcmpr(PyObject *a, PyObject *b, int op) return Py_INCREF_RET(res); } +static Py_hash_t Matrix_hash(MatrixObject *self) +{ + float mat[SQUARE(MATRIX_MAX_DIM)]; + + if (BaseMath_ReadCallback(self) == -1) + return -1; + + if (BaseMathObject_Prepare_ForHash(self) == -1) + return -1; + + matrix_transpose_internal(mat, self); + + return mathutils_array_hash(mat, self->num_row * self->num_col); +} + /*---------------------SEQUENCE PROTOCOLS------------------------ * ----------------------------len(object)------------------------ * sequence length */ @@ -2745,7 +2773,7 @@ PyTypeObject matrix_Type = { &Matrix_NumMethods, /*tp_as_number*/ &Matrix_SeqMethods, /*tp_as_sequence*/ &Matrix_AsMapping, /*tp_as_mapping*/ - NULL, /*tp_hash*/ + (hashfunc)Matrix_hash, /*tp_hash*/ NULL, /*tp_call*/ #ifndef MATH_STANDALONE (reprfunc) Matrix_str, /*tp_str*/ |