Browse Source Download (without any required ccan dependencies)
strmap
an ordered map of strings to values
Rusty Russell <[email protected]>
This code implements an ordered map of strings as a critbit tree. See:
http://cr.yp.to/critbit.html http://github.com/agl/critbit (which this code is based on)
#include <ccan/compiler/compiler.h>
#include <ccan/strmap/strmap.h>
#include <stdio.h>
static bool dump(const char *member, size_t value, void *unused UNNEEDED)
{
printf("%s at %zu. ", member, value);
// true means keep going with iteration.
return true;
}
int main(int argc, char *argv[])
{
size_t i;
STRMAP(size_t) map;
strmap_init(&map);
for (i = 1; i < (size_t)argc; i++)
// This only adds the first time for this arg.
strmap_add(&map, argv[i], i);
strmap_iterate(&map, dump, NULL);
printf("\n");
return 0;
}
// Given "foo" outputs "foo at 1. \n"
// Given "foo bar" outputs "bar at 2. foo at 1. \n"
// Given "foo foo bar zebra" outputs "bar at 3. foo at 1. zebra at 4. \n"
CC0 (but some dependencies are LGPL!)