Hash Functions/Cyclic Permutation
From charlesreid1
Cyclic permutation is a method for generating hash functions from data.
The basic idea is to generate a binary representation of the data (typically stored in a 32-bit integer, so wrapped into 32 bits) with a series of 32 0s and 1s, then to cyclically shift the digits of this binary number. If the data are the same, they should result in the same binary number, which will be shifted into the same resulting number when the hash code is computed. Collisions will happen if two objects end up having the same binary representation.
For example, to cyclically shift the digits of 10001111 2 digits to the right, the number would become 111000011.
A simple implementation of the cyclic permutation hash code calculation in Java is given as a static method below:
public static int hashCode(String s) {
int shift = 5;
int h = 0;
for(int i=0; i<s.length(); i++) {
h = (h<<(shift)) | (h>>>(32-shift));
h += (int)(s.charAt(i));
}
return h;
}
| Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|