Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a quick a dirty C hash I just wrote. Compiles, but untested locally. Still, the idea is there for you to run with it as needed. The performance of this is completely dependant upon the keyToHash function. My version will not be high performance, but again demonstrates how to do it.</p> <pre><code> static const int kMaxKeyLength = 31; static const int kMaxKeyStringLength = kMaxKeyLength + 1; struct HashEntry { int value; char key[kMaxKeyLength]; }; static const char kEmptyHash[2] = ""; static const int kHashPowerofTwo = 10; static const int kHashSize = 1 &lt&lt kHashPowerofTwo; static const int kHashMask = kHashSize - 1; static const int kSmallPrimeNumber = 7; static HashEntry hashTable[kHashSize]; int keyToHash(const char key[]) { assert(strlen(key) &lt kMaxKeyLength); int hashValue = 0; for(int i=0; &lt strlen(key); i++) { hashValue += key[i]; } return hashValue; } bool hashAdd(const char key[], const int value) { int hashValue = keyToHash(key); int hashFullSentinal = 0; while(strcmp(hashTable[hashValue & kHashMask].key, kEmptyHash)) { hashValue += kSmallPrimeNumber; if(hashFullSentinal++ &gt= (kHashSize - 1)) { return false; } } strcpy(hashTable[hashValue & kHashMask].key, key); hashTable[hashValue & kHashMask].value = value; return true; } bool hashFind(const char key[], int *value) { int hashValue = keyToHash(key); while(strcmp(hashTable[hashValue & kHashMask].key, kEmptyHash)) { if(!strcmp(hashTable[hashValue & kHashMask].key, key)) { *value = hashTable[hashValue & kHashMask].value; return true; } } return false; } bool hashRemove(const char key[]) { int hashValue = keyToHash(key); while(strcmp(hashTable[hashValue & kHashMask].key, kEmptyHash)) { if(!strcmp(hashTable[hashValue & kHashMask].key, key)) { hashTable[hashValue & kHashMask].value = 0; hashTable[hashValue & kHashMask].key[0] = 0; return true; } } return false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload