libdsc is a free and open-source C library that provides generic implementations of various containers from the C++ Standard Library. It brings modern container functionality to the C programming language with well-tested, memory-safe, and high-performance implementations.
libdsc currently implements the following containers:
-
dsc_vector: dynamic array equivalent tostd::vector -
dsc_forward_list: singly-linked list equivalent tostd::forward_list -
dsc_list: doubly-linked list equivalent tostd::list
-
dsc_unordered_map: hash table with key-value pairs equivalent tostd::unordered_map -
dsc_unordered_set: hash table for unique elements equivalent tostd::unordered_set
-
stack: LIFO container adapter equivalent tostd::stack -
queue: FIFO container adapter equivalent tostd::queue
- Generic: Can store any data type using
void*and element size - Memory-safe: Comprehensive error handling and bounds checking
- High-performance: Benchmarked against C++ STL equivalents
- Well-tested: Extensive unit tests with Google Test
- Professional: Follows industry best practices for C libraries
See ROADMAP.md for containers planned for future releases.
#include <libdsc/vector.h>
#include <stdio.h>
int main() {
// Create a vector of integers
dsc_vector *vec = vector_create(sizeof(int));
if (!vec) return 1;
// Add some elements
int values[] = {1, 2, 3, 4, 5};
for (size_t i = 0; i < 5; i++) {
vector_push_back(vec, &values[i]);
}
// Access elements
printf("Vector size: %zu\n", vector_size(vec));
int *first = (int*)vector_at(vec, 0);
printf("First element: %d\n", *first);
// Clean up
vector_destroy(vec);
return 0;
}More complete examples can be found in the examples/ directory.
-
CMake 3.14 or higher
-
C11-compatible compiler (GCC, Clang, MSVC)
-
Git
# Clone the repository
git clone https://github.com/cm-jones/libdsc.git
cd libdsc
# Create build directory
mkdir -p build && cd build
# Configure and build
cmake .. -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=OFF -DBUILD_BENCHMARKS=OFF -DBUILD_EXAMPLES=OFF
make -j$(nproc)
# Install system-wide (optional)
sudo make installThis installs:
- Library files to
/usr/local/lib - Header files to
/usr/local/include/libdsc - CMake configuration files to
/usr/local/lib/cmake/libdsc
sudo dpkg -i libdsc-*.debsudo rpm -i libdsc-*.rpmsudo pacman -U libdsc-*.pkg.tar.zstAdd to your CMakeLists.txt:
find_package(libdsc REQUIRED)
target_link_libraries(your_target libdsc::dsc)-
API Reference: Generated with Doxygen in
docs/ -
Examples: Complete usage examples in
examples/ -
Contributing: See CONTRIBUTING.md
-
Security: See SECURITY.md
-
Code of Conduct: See CODE_OF_CONDUCT.md
mkdir build && cd build
cmake .. -DBUILD_TESTS=ON
make -j$(nproc)
ctest --output-on-failuremkdir build && cd build
cmake .. -DBUILD_BENCHMARKS=ON
make -j$(nproc)
cd benchmarks
./benchmark_vector
./benchmark_list
# ... other benchmarks# Format code
./scripts/format.sh
# Run linter
./scripts/lint.sh
# Generate documentation
./scripts/docs.shlibdsc containers are regularly benchmarked against their C++ STL equivalents. Performance results are available in our CI benchmarks.
Key performance characteristics:
- Vector: Amortized O(1) push_back, O(1) random access
- List: O(1) insertion/deletion, O(n) search
- Hash containers: Average O(1) insertion/lookup, O(n) worst case
Please read CONTRIBUTING.md carefully before attempting to make any contributions.
This project is licensed under the GNU General Public License v3.0. See LICENSE for details.
