Skip to content

AjayMT/dict.c

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dict.c

Build Status

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.

Installation

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

API

dict *dict_new()

Allocate and initialize a new dictionary. This function returns a pointer to the dictionary that was created.

char *dict_get(dict *d, char *key)

Get the value of key in d and return it. You should free the value that is returned.

void dict_set(dict *d, char *key, char *value)

Set key to value in d. This function will create a new key-value pair if necessary.

void dict_del(dict *d, char *key)

Delete key in d.

int dict_has(dict *d, char *key)

Return 1 if key is in d, 0 otherwise.

int dict_len(dict *d)

Return the number of key-value pairs in d.

char **keys(dict *d)

Return an array containing all the keys in d. You should free this.

char **values(dict *d)

Return an array containing all the values in d. You should free this.

void dict_free(dict *d)

Properly free d.

Running tests

Clone the thing, cd into it and then do this:

$ make test

License

MIT License. See ./LICENSE for details.

About

simple dictionary implementation in c

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages