/*************************************************** NAME : HashExpression DESCRIPTION : Assigns a deterministic number to an expression INPUTS : The expression RETURNS : The "value" of the expression SIDE EFFECTS : None NOTES : None ***************************************************/ static unsigned HashExpression( EXPRESSION *exp) { unsigned long tally = PRIME_THREE; if (exp->argList != NULL) tally += HashExpression(exp->argList) * PRIME_ONE; while (exp != NULL) { tally += (unsigned long) (exp->type * PRIME_TWO); tally += (unsigned long) exp->value; exp = exp->nextArg; } return((unsigned) (tally % EXPRESSION_HASH_SIZE)); }
/*************************************************** NAME : FindHashedExpression DESCRIPTION : Determines if a given expression is in the expression hash table INPUTS : 1) The expression 2) A buffer to hold the hash value 3) A buffer to hold the previous node in the hash chain RETURNS : The expression hash table entry (NULL if not found) SIDE EFFECTS : None NOTES : None ***************************************************/ static EXPRESSION_HN *FindHashedExpression( EXPRESSION *exp, unsigned *hashval, EXPRESSION_HN **prv) { EXPRESSION_HN *exphash; if (exp == NULL) return(NULL); *hashval = HashExpression(exp); *prv = NULL; exphash = ExpressionHashTable[*hashval]; while (exphash != NULL) { if (IdenticalExpression(exphash->exp,exp)) return(exphash); *prv = exphash; exphash = exphash->nxt; } return(NULL); }
/*************************************************** NAME : HashExpression DESCRIPTION : Assigns a deterministic number to an expression INPUTS : The expression RETURNS : The "value" of the expression SIDE EFFECTS : None NOTES : None ***************************************************/ static unsigned HashExpression( EXPRESSION *theExp) { unsigned long tally = PRIME_THREE; union { void *vv; unsigned long uv; } fis; if (theExp->argList != NULL) tally += HashExpression(theExp->argList) * PRIME_ONE; while (theExp != NULL) { tally += (unsigned long) (theExp->type * PRIME_TWO); fis.uv = 0; fis.vv = theExp->value; tally += fis.uv; theExp = theExp->nextArg; } return((unsigned) (tally % EXPRESSION_HASH_SIZE)); }