Skip to content

libclist is a shared/static library for the c programming language with list and dictionary implementations

License

Notifications You must be signed in to change notification settings

jpaffrath/libclist

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Memcheck Badge Test Badge License Badge

Introduction

libclist is a shared/static library for the c programming language with list and dictionary implementations. libclist is very fast, yet memory saving and customizable. It is able to store any kind of data you like!

Compile

Invoke make in the project root. This will build libclist as a shared (libclist.so) and as a static (libclist.a) library.

Install

Invoke sudo make install in the project root. This will install the required header files along with the library to /usr/local/.

Testing

The test/ directory contains unit tests build with CUnit. Invoke make in the test/ directory will build a test binary. To check code coverage with gcov, run gcov_setup.sh.

List API

Provides a generic interface for a very fast linked list data structure. libclist supports storing any kind of data. Just

#include <libclist/list.h>

in your project and off you go! Want a simple example? Take this:

int myVal = 5;

// creates a new list holding your integer variable
element* list = create_list(&myVal, sizeof(int));

// adds an elements to your list
add_element(list, &myVal, sizeof(int));

// gets an element. You have to cast the result
int result = *(int*)get_element_at_index(list, 1);

Apart from the generic list interface, libclist provides a data type specialized API

#include <libclist/list_int.h>
#include <libclist/list_char.h>
#include <libclist/list_double.h>

so you can use easier functions:

element* myIntegerList = create_int_list(100);

printf("My list contains %d at index %d!", get_int_at_index(myIntegerList, 0), 0);

If you are done using your list, do not forget to delete it!

delete_list(&list);

A special case: structs with pointers!

If you want to store structs with allocated pointers, libclist provides specialized create and delete functions:

test_struct myStruct;
myStruct.str = (char*)malloc(100);

element* list = create_list_alloc(&myStruct, alloc_callback);
delete_list_alloc(&list, free_callback);

These functions are taking function pointers for allocating and freeing pointers in your struct. You just have to define these functions by yourself. See the following code for an easy example:

static void* alloc_callback(const void* e) {
	test_struct* s = (test_struct*)e;
	test_struct* copy = (test_struct*)malloc(sizeof(test_struct));
	copy->str = (char*)malloc(100);
	strcpy(copy->str, s->str);
	return copy;
}

static void free_callback(const void* e) {
	test_struct* ptr = (test_struct*)e;
	free(ptr->str);
}

List supports a lot of useful functions, just like removing or swapping elements, removing elements in a range, creating list from arrays or strings, or cloning lists. See list.h for all implemented functions.

About

libclist is a shared/static library for the c programming language with list and dictionary implementations

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published