Skip to content

Latest commit

 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

dsa_c

dsa_c is a small header-first C library with macro-based data structures.

It currently includes:

Status

The data structures which currently work are:

  • d_array.h
  • d_list.h
  • priority_queue.h
  • ptr_hash.h
  • pairs.h

ordered_hash.h looks incomplete and should be treated as work in progress.

Requirements

  • a C compiler with GNU extensions, such as gcc or clang in GNU mode
  • standard C library headers such as stdlib.h and stdint.h

Recommended compile flags:

gcc -std=gnu11 -Wall -Wextra your_file.c -Iinclude

Quick Start

This 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 example

Available Data Structures

Dynamic Array

Header: 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_back may reallocate, so the pointer variable itself must be used directly
  • vec_pop_back frees 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));

Doubly Linked List

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);

Priority Queue

Header: include/priority_queue.h

Macros:

  • p_queue_init(ptr, maxer)
  • p_push(ptr, val)
  • p_pop(ptr)
  • p_top(ptr)

Behavior:

  • maxer == 0 creates a min-heap
  • maxer == 1 creates 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));

Unordered Map and Set

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 int keys

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;
}

Pair Helper

Header: include/pairs.h

Macro:

  • pair_create(name, type1, type2)

Example:

#include "pairs.h"

pair_create(point, int, int);

Caveats

  • 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.h is not ready for production use.
  • test_arr.c currently references a header name that does not match the files in include/, so treat it as an old experiment rather than an authoritative example.

Layout

include/
  d_array.h
  d_list.h
  ordered_hash.h
  pairs.h
  priority_queue.h
  ptr_hash.h

License

No license file is currently present in this repository. If you plan to publish or distribute this library, add a license first.

About

Implementation of common datastructures like dynamic arrays,stack and hashmaps in C

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages