dsa_c is a small header-first C library with macro-based data structures.
It currently includes:
- dynamic arrays in
include/d_array.h - doubly linked lists in
include/d_list.h - pairs in
include/pairs.h - binary heap priority queues in
include/priority_queue.h - unordered maps and sets in
include/ptr_hash.h - an ordered map experiment in
include/ordered_hash.h
The data structures which currently work are:
d_array.hd_list.hpriority_queue.hptr_hash.hpairs.h
ordered_hash.h looks incomplete and should be treated as work in progress.
- a C compiler with GNU extensions, such as
gccorclangin GNU mode - standard C library headers such as
stdlib.handstdint.h
Recommended compile flags:
gcc -std=gnu11 -Wall -Wextra your_file.c -IincludeThis library is header-only in its current form, so include the headers you need and compile with -Iinclude.
Example:
#include "d_array.h"
#include <stdio.h>
int main(void) {
int *arr;
vec_init(arr, 4);
vec_push_back(arr, 10);
vec_push_back(arr, 20);
vec_push_back(arr, 30);
printf("front=%d back=%d\n", front(arr), back(arr));
return 0;
}Build:
gcc -std=gnu11 -Wall -Wextra example.c -Iinclude -o exampleHeader: include/d_array.h
Macros:
vec_init(ptr, capacity)vec_push_back(ptr, val)vec_pop_back(ptr)front(ptr)back(ptr)
Notes:
- capacity and size metadata are stored just before the returned pointer
vec_push_backmay reallocate, so the pointer variable itself must be used directlyvec_pop_backfrees storage when the array becomes empty
Example:
int *arr;
vec_init(arr, 2);
vec_push_back(arr, 1);
vec_push_back(arr, 2);
vec_push_back(arr, 3);
printf("%d\n", back(arr));Header: include/d_list.h
Macros:
list_create(type, name)list_init(ptr)insert_list(node, value)find_list(t, val, iter)erase_list(t)
Example:
#include "d_list.h"
list_create(int, int_node)
int_node *head;
list_init(head);
insert_list(head, 42);
insert_list(head, 99);Header: include/priority_queue.h
Macros:
p_queue_init(ptr, maxer)p_push(ptr, val)p_pop(ptr)p_top(ptr)
Behavior:
maxer == 0creates a min-heapmaxer == 1creates a max-heap
Example:
#include "priority_queue.h"
#include <stdio.h>
int *pq;
p_queue_init(pq, 0);
p_push(pq, 7);
p_push(pq, 3);
p_push(pq, 10);
printf("%d\n", p_top(pq));Header: include/ptr_hash.h
Map macros:
umap_create(name_map, type_k, type_v)umap_init(ptr)umap_insert(u, k, v)umap_get(u, k)umap_erase(u, k)umap_size(u)
Set macros:
uset_create(name_set, type_k)uset_init(ptr)uset_insert(u, k)uset_get(u, k)uset_erase(u, k)uset_size(u)
Important note:
- the current hashing path is written specifically for
intkeys
Example:
#include "ptr_hash.h"
#include <stdio.h>
umap_create(int_map, int, int)
int main(void) {
int_map *map;
umap_init(map);
umap_insert(map, 1, 100);
umap_insert(map, 2, 200);
printf("%d\n", umap_get(map, 2));
return 0;
}Header: include/pairs.h
Macro:
pair_create(name, type1, type2)
Example:
#include "pairs.h"
pair_create(point, int, int);- This library relies on macros rather than functions, so arguments with side effects should be used carefully.
- Several macros assume direct pointer variables instead of temporary expressions.
- Memory management is manual.
- The public API is not yet polished and may change.
ordered_hash.his not ready for production use.test_arr.ccurrently references a header name that does not match the files ininclude/, so treat it as an old experiment rather than an authoritative example.
include/
d_array.h
d_list.h
ordered_hash.h
pairs.h
priority_queue.h
ptr_hash.h
No license file is currently present in this repository. If you plan to publish or distribute this library, add a license first.