Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pretty fast hash map

Name: Anonymous 2014-09-22 13:01

Hey /frog/

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;
}

Name: Anonymous 2014-09-23 11:59

>>11
wats wrong wif picolisp?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List