Skip to content

Latest commit

 

History

History
56 lines (41 loc) · 1.33 KB

README.md

File metadata and controls

56 lines (41 loc) · 1.33 KB

hamt - hash array mapped trie

hamt is a simple implementation of a Hash Array Mapped Trie. The use case it was built for was to handle storing api routes as the key with a function pointer/struct as the value.

Some examples have been provided in main.c, along with a dictionary of 466550 words. To build them:

$ mkdir build
$ make
$ ./hamt-testing.out

Usage

Insert

Any arbitrary data void * can be stored against a char * key.

#include "hamt.h"

int main(void) {
  struct hamt_t *hamt = create_hamt();

  hamt = hamt_set(hamt, "hello", "world");
  hamt = hamt_set(hamt, "hey", "hows it going");
  hamt = hamt_set(hamt, "hey2", "wow so doge!");
}

Retrieval

To get a value, pass the hamt and key to hamt_get. The return value is either void * or NULL. Cast the type to whatever the data type it is you gave inserted. Follow from aboves example we cast the result to char * and then print to stdout.

#include "hamt.h"

int main(void) {
  struct hamt_t *hamt = create_hamt();

  // Assuming the hamt has been made as above ^
  char *found = (char *)hamt_get(hamt, "Hello");
  if (found != NULL) {
    printf("%s\n", found);
  }
}

Removal

To remove a node:

#include "hamt.h"

hamt = hamt_remove(hamt, "hey");

char *value = hamt_get(hamt, "hey");
printf("%s\n", value); // prints NULL