Ticket #3724: m4ri_hash.patch

File m4ri_hash.patch, 2.1 kB (added by malb, 5 months ago)

implements faster hashing

  • a/c_lib/include/pb_wrap.h

    old new  
    66 
    77// M4RI  
    88#define PACKED 1 
    9 #include "M4RI/packedmatrix.h" 
    10 #include "M4RI/grayflex.h" 
     9#include "M4RI/m4ri.h" 
    1110 
    1211#include <sstream> 
    1312#include <vector> 
  • a/sage/matrix/matrix_mod2_dense.pyx

    old new  
    281281            {[0 1 0] 
    282282            [0 1 1] 
    283283            [0 0 0]: 0} 
     284            sage: A = matrix(GF(2),2,2) 
     285            sage: A.set_immutable() 
     286            sage: hex(hash(A)) 
     287            '0xdeadbeed' 
    284288 
    285289        TEST: 
    286290            sage: A = matrix(GF(2),2,0) 
     
    288292            sage: hash(A) 
    289293            0 
    290294        """ 
    291         return self._hash() 
     295        cdef unsigned long _hash = 0xDEADBEEF 
     296        cdef unsigned long counter = 0 
     297        cdef unsigned long i, j, truerow 
     298        cdef word mask = 1 
     299        mask = ~((mask<<(RADIX - self._ncols%RADIX))-1) 
     300 
     301        if self._nrows == 0 or self._ncols == 0: 
     302            return 0 
     303 
     304        for i from 0 <= i < self._entries.nrows: 
     305            truerow = self._entries.rowswap[i] 
     306            for j from 0 <= j < self._entries.width - 1: 
     307                _hash ^= self._entries.values[truerow + j] 
     308                counter += 1 
     309            _hash ^= self._entries.values[truerow + j] & mask 
     310            counter += 1 
     311                 
     312        _hash = _hash ^ counter 
     313 
     314        if _hash == -1: 
     315            return -2 
     316        return _hash 
    292317     
    293318    cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value): 
    294319        mzd_write_bit(self._entries, i, j, int(value))