/* * Hash a func_t. */ extern hash_t hash_func(func_t f) { hash_t key = FUNC_KEY; hash_t hash = hash_word((word_t)f->atom, key); size_t aty = atom_arity(f->atom); for (size_t i = 0; i < aty; i++) hash = hash_join(i, hash, hash_term(f->args[i])); return hash; }
/* * Calculate the hash value of a constraint w.r.t. a lookup. */ extern hash_t hash_lookup(hash_t hash, lookup_t lookup, cons_t c) { while (*lookup >= 0) { size_t idx = (size_t)*lookup; term_t arg = c->args[idx]; hash = hash_join(idx, hash, hash_term(arg)); lookup++; } return hash; }
/* PUBLIC */ unsigned hash_term(Term t) { if (VARIABLE(t)) return VARNUM(t); else { int i; unsigned x = SYMNUM(t); for (i = 0; i < ARITY(t); i++) x = (x << 3) ^ hash_term(ARG(t,i)); return x; } } /* hash_term */
/* * Similar to hash_update_lookup, but for entire constraint. */ extern hash_t hash_update_cons(hash_t hash, cons_t c, hash_t xkey_old, hash_t xkey_new) { size_t aty = c->sym->arity; for (size_t i = 0; i < aty; i++) { term_t arg = c->args[i]; hash_t arg_hash = hash_term(arg); if (hash_iseq(arg_hash, xkey_old)) arg_hash = xkey_new; hash = hash_join(i, hash, arg_hash); } return hash; }
/* * Similar to above but use new hash value for term x. */ extern hash_t hash_update_lookup(hash_t hash, lookup_t lookup, cons_t c, hash_t xkey_old, hash_t xkey_new) { while (*lookup >= 0) { size_t idx = (size_t)*lookup; term_t arg = c->args[idx]; hash_t arg_hash = hash_term(arg); if (hash_iseq(arg_hash, xkey_old)) arg_hash = xkey_new; hash = hash_join(idx, hash, arg_hash); lookup++; } return hash; }