However, the supplied array is not retained. This means that the implementation cannot necessarily confirm that a string is not in the supplied array. Where this implementation cannot distinguish that a string is not in the array, a 'valid' hash value may be returned. Under no circumstances will a hash value be returned that is greater than or equal to n.
IMPORTANT NOTE: The array of strings supplied to the constructor will be mutated: it is re-ordered so that hash(a[i]) == i. Application code must generally use this information to map hash values back onto the appropriate string value.
NOTE: Good performance of this algorithm is predicated on string hash values being cached by the String class. Experience indicates that is is a good assumption.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Comparator<String> Comparator used to order the supplied string array.private final int[]The hashcodes of the supplied strings.private final int[]Stores two ints for every string, an offset into the pivot array (-1 if not necessary) and the depth of the decision tree that is rooted there.private final int[]Stores two ints for every decision, the index at which a character comparison needs to be made, followed by the character value to be compared against; or -1 to indicate a length comparison, followed by the length to be compared against.private final HashRangeCache a range object which indicates the range of hash values generated.Constructor Summary
ConstructorsConstructorDescriptionPerfectStringHash(String[] values) Constructs a minimal perfect string hashing over the supplied strings.Method Summary
Modifier and TypeMethodDescriptionprivate static voidgeneratePivots(String[] values, int start, int length, int[] pivots, int pivotIndex) Builds a (typically v.getRange()Returns the hash range for this hash function.private intGenerates a hashcode for the supplied string.hashAsBigInt(String value) The hash value as aBigInteger.intThe hash value as an int.longhashAsLong(String value) The hash value as a long.
Field Details
comparator
Comparator used to order the supplied string array. Hashcodes take priority, we will do a binary search on those. Otherwise, lengths take priority over character ordering because the hash algorithm prefers to compare lengths, it's faster.hashes
private final int[] hashesThe hashcodes of the supplied strings.offsets
private final int[] offsetsStores two ints for every string, an offset into the pivot array (-1 if not necessary) and the depth of the decision tree that is rooted there.pivots
private final int[] pivotsStores two ints for every decision, the index at which a character comparison needs to be made, followed by the character value to be compared against; or -1 to indicate a length comparison, followed by the length to be compared against.range
Cache a range object which indicates the range of hash values generated.
Constructor Details
PerfectStringHash
Constructs a minimal perfect string hashing over the supplied strings.- Parameters:
values- an array of unique non-null strings that will be reordered such thathash(values[i]) == i.
Method Details
generatePivots
private static void generatePivots(String[] values, int start, int length, int[] pivots, int pivotIndex) Builds a (typically v. small) decision tree for distinguishing strings that share the same hash value.- Parameters:
values- the string values to distinguishstart- the index from which the values should be readlength- the number of string values that need to be distinguishedpivots- the array that will hold our decision nodespivotIndex- the index at which the tree should be written
getRange
hashAsBigInt
Description copied from interface:HashThe hash value as aBigInteger. This method may be useful in circumstances where the generated hash is too large to be accommodated in a single primitive value, eg. if cryptographic hashes are being used.- Specified by:
hashAsBigIntin interfaceHash<String>- Parameters:
value- the object to be hashed- Returns:
- the object's hash code, never null
hashAsInt
hashAsLong
Description copied from interface:HashThe hash value as a long. This method should provide better performance for long-ranged hashes. This value is not guaranteed to lie within the indicatedHashRange.- Specified by:
hashAsLongin interfaceHash<String>- Parameters:
value- the object to be hashed- Returns:
- the object's hash code
hash
Generates a hashcode for the supplied string.- Parameters:
value- any string, not null- Returns:
- a minimal hashcode for the supplied string, or -1