I usually take my school code as a playground to mess around with things. I was given a bunch of premade hash functions and had to test their output and find when they reach a specific lower range. Since the functions all have the same structure, I made a check function that uses a function pointer parameter(I think this is the most efficient way to deal with this problem?). Which I'm iterating through the potential keys to feed to all of the functions it I wanted it to stop at the first one of each, but couldn't think of a way to do so without using 11 individual flags or for loops. I've never messed with bitmasks (I specifically only remember the gist of them because there was a couple commands in quake3 that used them!), but I did that instead and it turned out to not be as pretty/elegant as I thought it would in practice.
in short:
How would you approach the assignment "Using the given 11 hash functions, find each of their first 'low' output?"
void findLowHash();
bool foundLow(unsigned int (*hash)(const std::string &key), std::string key);
void findLowHash() {
string key;
key.push_back(0);
int mask = 2047;
for (int i = 0; i < 100000; ++i) {
incrStr(key);
carryStr(key);
if (mask == 0)
break;
if (mask >> 0 & 1) {
if (foundLow(RSHash, key)) {
mask -= 1;
cout << "RSHash"
<< " " << mask << endl;
}
}
if (mask >> 1 & 1) {
//cout<<JSHash(key)<<" "<<key<<endl;
if (foundLow(JSHash, key)) {
mask -= 2;
cout << "JSHash"
<< " " << mask << endl;
}
}
if (mask >> 2 & 1) {
if (foundLow(PJWHash, key)) {
mask -= 4;
cout << "PJWHash"
<< " " << mask << endl;
}
}
if (mask >> 3 & 1) {
if (foundLow(ELFHash, key)) {
mask -= 8;
cout << "ELFHash"
<< " " << mask << endl;
}
}
if (mask >> 4 & 1) {
if (foundLow(BKDRHash, key)) {
mask -= 16;
cout << "BKDRHash"
<< " " << mask << endl;
}
}
if (mask >> 5 & 1) {
if (foundLow(SDBMHash, key)) {
mask -= 32;
cout << "SDBMHash"
<< " " << mask << endl;
}
}
if (mask >> 6 & 1) {
if (foundLow(DJBHash, key)) {
mask -= 64;
cout << "DJBHash"
<< " " << mask << endl;
}
}
if (mask >> 7 & 1) {
if (foundLow(DEKHash, key)) {
mask -= 128;
cout << "DEKHash"
<< " " << mask << endl;
}
}
if (mask >> 8 & 1) {
if (foundLow(FNVHash, key)) {
mask -= 256;
cout << "FNVHash"
<< " " << mask << endl;
}
}
if (mask >> 9 & 1) {
if (foundLow(BPHash, key)) {
mask -= 512;
cout << "BPHash"
<< " " << mask << endl;
}
}
if (mask >> 10 & 1) {
if (foundLow(APHash, key)) {
mask -= 1024;
cout << "APHash"
<< " " << mask << endl;
}
}
}
}
bool foundLow(unsigned int (*hash)(const std::string &key), std::string key) {
if ((*hash)(key) < 10000) {
std::cout << "key: " << key << " Output:" << ((*hash)(key)) << " ";
return true;
}
return false;
}