Improve hash function to fix collision problem
[satune.git] / src / Backend / constraint.cc
index 7c3a094701d3c12ffa08d5a3fba5c83dd6b3f4d8..b7cb320e2a9da41473d260e09a27940c1360f174 100644 (file)
@@ -146,12 +146,19 @@ Edge createNode(CNF *cnf, NodeType type, uint numEdges, Edge *edges) {
 }
 
 uint hashNode(NodeType type, uint numEdges, Edge *edges) {
-       uint hashvalue = type ^ numEdges;
+       uint hash = type;
+       hash += hash << 10;
+       hash ^= hash >> 6;
        for (uint i = 0; i < numEdges; i++) {
-               hashvalue ^= (uint) ((uintptr_t) edges[i].node_ptr);
-               hashvalue = (hashvalue << 3) | (hashvalue >> 29);       //rotate left by 3 bits
-       }
-       return (uint) hashvalue;
+               uint c = (uint) ((uintptr_t) edges[i].node_ptr);
+               hash += c;
+               hash += hash << 10;
+               hash += hash >> 6;
+       }
+       hash += hash << 3;
+       hash ^= hash >> 11;
+       hash += hash << 15;
+       return hash;
 }
 
 bool compareNodes(Node *node, NodeType type, uint numEdges, Edge *edges) {