libstd v0.1.0
Loading...
Searching...
No Matches
libstd

String + generic data structures library in C.

Appendix

This is library written in C that provides easy-to-use implementations of fundamental data structures and a custom string type. It aims to reduce boilerplate and simplify development for C projects that require dynamic arrays, stacks, queues, or hash tables.

Features

  • A custom string type with functions to create, destroy, and query size/capacity.
  • Dynamic arrays (vectors)
  • Stacks
  • Queues
  • Hash tables (hashmaps)

Tech Stack

Languages: C

Build: Make and Docker

Installation

Clone the project

git clone https://codeberg.org/luizvilasboas/libstd.git

Go to the project directory

cd libstd

Compile the library

make

Install the library

sudo make install

Usage/Examples

#include <libstd/data_structures/vector.h>
#include <stdio.h>
int compare_ints(void *a, void *b) {
return (*(int *)a - *(int *)b);
}
int main() {
Vector vector;
vector_init(&vector, sizeof(int));
int data_1 = 5, data_2 = 2, data_3 = 8;
vector_insert(&vector, &data_1);
vector_insert(&vector, &data_2);
vector_insert(&vector, &data_3);
printf("Vector size: %d\n", vector_size(&vector));
vector_sort(&vector, compare_ints);
for (size_t i = 0; i < vector_size(&vector); i++) {
printf("Element %zu: %d\n", i, *(int *)vector_get(&vector, i));
}
vector_destroy(&vector);
return 0;
}
Structure representing a generic linked-list-backed Vector.
Definition vector.h:18
void vector_destroy(Vector *vector)
Destroys the Vector and frees all allocated nodes and their associated data.
Definition vector.c:39
void * vector_get(Vector *vector, size_t index)
Retrieves a pointer to the element at a specific index.
Definition vector.c:128
int vector_size(const Vector *vector)
Retrieves the total number of elements currently stored in the vector.
Definition vector.c:162
bool vector_insert(Vector *vector, const void *data)
Appends (inserts) a new element to the end of the vector.
Definition vector.c:61
bool vector_sort(Vector *vector, int(*cmp_fn)(void *, void *))
Sorts the elements in the vector using a comparison function.
Definition vector.c:170
void vector_init(Vector *vector, size_t element_size)
Initializes an empty Vector.
Definition vector.c:29

Documentation

"" "Documentation"

Running Tests

To run tests, run the following command

make test

Contributing

If you have a suggestion that would make this better, please fork the repo and create a pull request.

License

LGPLv3