Name: Anonymous 2014-09-22 13:01
Hey /frog/
check out my hash map implementation
in C of course
check out my hash map implementation
in C of course
struct entry {
int key;
int hash;
int value;
};
static int hash(int key) { return key % 256; }
struct HashMap {
struct entry entries[999];
};
/* The API is below */
/* initialize new hash map */
void hashmap_init(HashMap* h) {
for (int i=0; i<999;i++) { h->entries[i].key = -1; }
}
/* find key from hashmap, return pointer to value
returned pointer is valid as long as HashMap is alive
*/
int* hashmap_find(HashMap* h, int k) {
for (int i=0; i<999;i++) {
if (hash(k) == h->entries[i].hash) {
if (k == h->entries[i].key) {
return &h->entries[i].value;
}
}
}
for (int i=0;i<999; i++) {
if (h->entries[i] < 0) {
h->entries[i].key = k;
h->entries[i].hash =hash(k);
return &h->entries[i].value
}
}
return 0;
}