Skip to content

Instantly share code, notes, and snippets.

@Kudo
Created June 29, 2012 00:16
Show Gist options
  • Save Kudo/3014898 to your computer and use it in GitHub Desktop.
Save Kudo/3014898 to your computer and use it in GitHub Desktop.
Hashing two integer to one key
static int _HashingSourceInfo(in_addr_t sourceIp, uint32_t sourceId)
{
// [0] Cantor pairing function
unsigned long long key = (sourceIp + sourceId) * (sourceIp + sourceId + 1) / 2 + sourceId;
// Ref: http://elliottback.com/wp/hashmap-implementation-in-c/
// [1] Robert Jenkins 32 bit Mix Function
key += (key << 12);
key ^= (key >> 22);
key += (key << 4);
key ^= (key >> 9);
key += (key << 10);
key ^= (key >> 2);
key += (key << 7);
key ^= (key >> 12);
// [2] Knuth's Multiplicative Hash
key = (key >> 3) * 2654435761UL;
return key % NF_V9_MAX_SOURCE_ENTRIES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment