dict.c is a simple implementation of a dictionary in C. It's basically just a linked list of key-value pairs.
Here's a usage example:
#include <stdio.h>
#include <stdlib.h>
#include "dict.h"
int main (int argc, char *argv[])
{
int n = 12;
dict *my_dict = dict_new();
dict_set(my_dict, "hello", "world");
dict_set(my_dict, "foo", &n);
char *world = dict_get(my_dict, "hello");
printf("hello: %s\n", world); // => "hello: world"
free(world);
char *bar = dict_get(my_dict, "foo");
printf("foo: %d\n", *bar); // => "foo: 12"
free(bar);
dict_set(my_dict, "hello", "people"); // now 'hello' is mapped to 'people'
n = 5; // we don't need to dict_set since 'foo' points to n
dict_free(my_dict);
}
Take a look at ./test.c
for a longer example.
Installing with clib is recommended:
$ clib install AjayMT/dict.c
But you can make
it and use the dict.o
file:
$ git clone http://github.com/AjayMT/dict.c.git
$ cd dict.c
$ make
Allocate and initialize a new dictionary. This function returns a pointer to the dictionary that was created.
Get the value of key
in d
and return it. You should free
the value that is returned.
Set key
to value
in d
. This function will create a new key-value pair if necessary.
Delete key
in d
.
Return 1 if key
is in d
, 0 otherwise.
Return the number of key-value pairs in d
.
Return an array containing all the keys in d
. You should free
this.
Return an array containing all the values in d
. You should free
this.
Properly free d
.
Clone the thing, cd
into it and then do this:
$ make test
MIT License. See ./LICENSE
for details.