Browse Source Download (without any required ccan dependencies)
idtree
id allocation tree
Rusty Russell <rusty@rustcorp.com.au>
Jim Houston <jim.houston@ccur.com>
There are often cases where you want to provide an integer handle for some data, and easily map it back to another structure.
idtree is an efficient implementation of an int->void * mapping, with assignment of the lowest available id number. It is from the Linux kernel via the Samba project.
#include <ccan/idtree/idtree.h>
#include <ccan/tal/tal.h>
#include <stdlib.h>
#include <stdio.h>
// Silly example which puts args in the idtree and retreives them
int main(int argc, char *argv[])
{
struct idtree *idtree = idtree_new(NULL);
unsigned int i;
// This will return consecutive id numbers.
for (i = 0; i < argc; i++) {
printf("idtree_add('%s') -> id %i\n",
argv[i], idtree_add(idtree, argv[i], -1));
}
for (i = 0; i < argc; i++) {
printf("id %i -> '%s'\n",
i, (char *)idtree_lookup(idtree, i));
}
return 0;
}
GPL (v2 or any later version)