Avatar of ambuli
ambuli
Flag for United States of America

asked on 

Calculating hash of an object

I have the following function that calculates the has of on object.  There are four integer member variables and 2 String variables. The two member variables have been changed to integers now. so, how would I calculate the hash now.  Is it simply doing the following is enough.  Why string is handled especially.  I am not familiar with how this works, can someone explain it.  Thank you very much.

        hash = hash * 17 + intMember1;
      hash = hash * 17 + intMember2;
      hash = hash * 17 + intMember3;
      hash = hash * 17 + intMember4;
      hash = hash * 17 +strMember1; <-  This is integer now
      hash = hash * 17 + strMember2;<-  This is integer now
                  
The code is actually in AS3.  But, I modified here.
public int hashValueOf()
{
	int  hash = 1;  
	hash = hash * 17 + intMember1;
	hash = hash * 17 + intMember2;
	hash = hash * 17 + intMember3;
	hash = hash * 17 + intMember4;
	hash = hash * 31 + calculateHash(strMember1);
	hash = hash * 31 + calculateHash(strMember2);
			
	return hash;
}


public int calculateHash(String input)
{
        int h = 0;
        int len = input.length;
	for (var i:int = 0; i < len; i++) 
        {
		h = 31 * h + input.charCodeAt(i);
	}
	return h;
}

Open in new window

JavaC++C

Avatar of undefined
Last Comment
for_yan

8/22/2022 - Mon