libds-c is a header-only library for C projects that implements a wide variety of data structures alongside strings.
| Data Structure | Status |
|---|---|
| Dynamic Array | Finished |
| Stack | Finished |
| String | In Progress |
| Hashmap | Not started |
| Hashset | Not started |
Other data structures will be added in the future as well.
Copy the include directory into your project, then you can use all functions as normal.
Add this snippet to your CMakeLists.txt:
include(FetchContent)
FetchContent_Declare(libds-c
GIT_REPOSITORY https://github.com/Villy-P/libds-c.git
GIT_TAG main # consider pinning to a release tag or commit SHA for reproducible builds
)
FetchContent_MakeAvailable(libds-c)
target_link_libraries(your_target PRIVATE libds-c::libds-c)Replace your_target with the name of your project. All libds-c headers will be available to it.
By default, libds-c only includes function declarations.
To include the function implementations, define DS_C_IMPLEMENTATION in exactly one C file:
#define DS_C_IMPLEMENTATION
#include <libds-c.h>Additionally, if you would like to have stack functions, define the DS_DEFINE_STACK_IMPLEMENTATION macro.
Alternatively, you can add these with CMake using add_definitions:
add_definitions(-DDS_DEFINE_STACK_IMPLEMENTATION -DDS_C_IMPLEMENTATION)By default, libds-c data structures are type-anonymous, so any type can be inserted into any data structure.
To add type-safety to data structures, use the DS_DEFINE_ macro in a header file:
DS_DEFINE_ARRAY(int, ds_int_array, NULL, NULL)
// Usage
ds_int_array* arr = ds_int_array_create(10);
ds_int_array_push(arr, 2); // Works
ds_int_array_push(arr, 'c'); // Compiler error: incorrect typeBy default, libds-c will return error codes as found in DS_STATUS if a runtime error occurs. By defining the DS_THROWS_ERROR macro, all critical errors will instead stop the program.
libds-c is licensed under the MIT License.