...
This compliant solution calls a method that returns a modulus remainder that is always positive.
| Code Block | ||
|---|---|---|
| ||
// method imod() gives non-negative result
private int SIZE = 16;
public int[] hash = new int[SIZE];
private int imod(int i, int j) {
int temp = i % j;
return (temp < 0) ? -temp : temp; // unary - will succeed without overflow
// because temp cannot be Integer.MIN_VALUE
}
public int lookup(int hashKey) {
return hash[imod(hashKey, size)];
}
|
...