Home

Awesome

rsht (really simple hash table)

rsht is a really basic LPMult hash table with a simple Bloom-like optimization that sometimes prevents unsuccessful reads from causing O(n) scans. Its small code size and lack of use of linked lists make it ideal for usecases with small n and low load factors.

rsht avoids copying data, allowing the caller to manage memory as they wish. This leads to just slightly more complex calling code, with the benefit of (probably?) better performance.

limitations

building

rsht conforms to the C99 standard. To build it,

cd build
cmake ..
make
make test # to test, if you want

or use it as part of an existing CMake build with add_subdirectory

TODO: allow people to build shared, somehow (CMake CACHE var?)

usage

initialization:

rsht_ht *ht = malloc(sizeof(rsht_ht)); // can also be stack allocated
rsht_create(ht, num_buckets, initial_capacity);

insertion:

char *key = strdup("my key");
char *val = strdup("my val");

// if a caller knows the value doesn't already exist
// BEWARE: using it this way can cause memory leaks!
if (!rsht_put(ht, key, val, NULL)) {
  // handle out of memory error
}

// if a caller wants to know the previous value and free it
void *old_val;
if (!rsht_put(ht, key, val, &old_val)) {
  // handle out of memory error
  free(key);
}
if (old_val) {
  free(key); // if the value existed, we can free the key we just used
  free(old_val);
}

retrieval:

rsht_entry *ret = rsht_get(ht, "key");
if (ret) {
  // use the item that was found
} else {
  // handle the lack of an item
}

cleanup:

bool entry_destroyer(rsht_entry *e, void *userdata) {
  // however one chooses to destroy entries.
  // this example assumes `val` might be `NULL` and everything heap allocated.
  free(e->key);
  if (e->val)
    free(e->val);
  return true; // if this were false, the loop would stop
}

rsht_foreach(ht, entry_destroyer, NULL);
rsht_destroy(ht);

testing

The current tests can be run via make test when rsht is configured as a standalone project. They're not very comprehensive and need some work.

alternative names

The "RS" of rsht could really stand for any number of things. Consider:

license

rsht.h and src/rsht.c are provided under the terms of the MIT license.

Everything else is dual-licensed MIT/Public Domain.