Python-like Dictionary Library
Dictionary library similar to Python dict functionality
Loading...
Searching...
No Matches
Python-like dictionary datatype written in C

Dictionary is a key-value storage useful for different purposes.

The key is always of type char * (string) and the value can be any type.

Since we don't know the type of the value in advance, (it can be a pointer to char or int or a custom structure), We have to know how to copy the value and how to free the allocated memory for the value.

Therefore, the cdict_init() function will accept two parameters:

  1. cdict_free_func: A pointer to the user provided function to free the memory for the value.
  2. cdict_copy_func: A pointer to the user provided function to copy the value.

Other things are easy to use and understand.

There are currently four functions available for the dictionary structure:

  1. Adding a key-value pair to the dictionary
  2. Removing a key-value pair from a dictionary
  3. Getting a value from the dictionary by providing the key
  4. Lists all the keys

Example

Check the examples in test directory.

Here is a small example:

Compile this with:

# inside the test directory
gcc -O2 ./test_1.c ../src/cdict.c ../include/cdict.h -o test
# and then run ./test
#include <cdict.h>
#include <cunittest.h>
#include <stdlib.h>
void dict_free_func(void * value){
free(value);
}
void* dict_copy_func(void * value){
return (void*) strdup((char*)value);
}
int main(int argc, char ** argv){
// this is only used to create random keys and values
srand(1);
ABORT_ON_FAIL(1);
// creating a new context
cdict_ctx* new_dict = NULL;
// number of key-values to insert
int COUNT = 500000;
// we store strings as values in the dictionary. So the standard 'free()'
// function and 'strdup()' for copying and freeing operations are enough.
// creating a context with free and strdup functions as params.
new_dict = cdict_init(free, (void*)strdup);
// add random key, values to the dictionary
int random_val = 0;
char key[50] = {0};
for (int i=0; i< COUNT; ++i){
random_val = rand();
sprintf(key, "key_%d", random_val);
// before insertion check if the key exists
if (cdict_has_key(new_dict, key)){
fprintf(stdout, "We already have this key: %s\n", key);
continue;
}
cdict_set(new_dict, key, (void *)"John Doe");
}
cdict_keylist * klst = NULL;
klst = cdict_keys(new_dict, 0);
fprintf(stdout, "We have %d keys\n", klst->len);
for (unsigned long int i=0; i< klst->len; ++i){
//fprintf(stdout, "%s -> %s\n", klst->lst[i], (char*)cdict_get(new_dict, klst->lst[i]));
}
cdict_free(new_dict);
return 0;
}
void cdict_free_keylist(cdict_keylist *klst, int clone_keys)
This function free the keylist structure returned by cdict_keys()
Definition cdict.c:509
void cdict_free(cdict_ctx *ctx)
Frees the memory allocated for the dictionary.
Definition cdict.c:403
int cdict_has_key(cdict_ctx *ctx, char *key)
Checks if the key exists in the dictionary.
Definition cdict.c:439
cdict_ctx * cdict_init(void(*free_func)(void *), void *(*copy_func)(void *))
Initializes the Dictionary in the memory.
Definition cdict.c:109
int cdict_set(cdict_ctx *ctx, char *key, void *value)
Sets the new value for a given key.
Definition cdict.c:339
cdict_keylist * cdict_keys(cdict_ctx *ctx, int clone_keys)
Get the list of keys from the dictionary.
Definition cdict.c:533
Definition cdict.h:40
  1. test_1.c: shows how to insert key-value pairs to the dictionary and check if a key exists.

if values are of type char *, then I use

cdict_ctx * dict = cdict_init(free, (char*) strdup);

to initialize the dictionary.

However, if I want to store a custom structure in the dictionary, I have to provide two custom functions to free and copy the structure for me.

Look at test_2.c in the test directory for more info.

  1. test_remove.c in test directory shows how to remove the key in a for-loop.
  2. test_2.c in test directory shows how to store custom structure as values in the dictionary.

Documentation

Use Doxygen to generate the documents