44 lines
1013 B
C
44 lines
1013 B
C
/*
|
|
* SPDX-FileCopyrightText: 2025 William Bell
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*/
|
|
|
|
#ifndef DARRAY_H
|
|
#define DARRAY_H
|
|
|
|
#include <stdbool.h>
|
|
#include <stddef.h> // for size_t
|
|
|
|
#define CHUNK_SIZE 4096
|
|
|
|
typedef struct {
|
|
void *data;
|
|
size_t element_size;
|
|
size_t size;
|
|
size_t capacity;
|
|
bool resizable;
|
|
} DArray;
|
|
|
|
// Initializes the dynamic_array
|
|
void darray_init(DArray *arr, size_t element_size);
|
|
|
|
// Pushes an element onto the array
|
|
void darray_push(DArray *arr, void *element);
|
|
|
|
// Pops the last element, calling `free_data` if provided
|
|
void darray_pop(DArray *arr, void (*free_data)(void *));
|
|
|
|
// Gets a pointer to an element at index
|
|
void *darray_get(DArray *arr, size_t index);
|
|
|
|
// Frees the entire array and optionally each element
|
|
void darray_free(DArray *arr, void (*free_data)(void *));
|
|
|
|
// Resizes the array to a new size (internal use, but exposed)
|
|
void darray_resize(DArray *arr, size_t new_size);
|
|
|
|
DArray darray_slice(DArray *arr, size_t start, size_t end);
|
|
|
|
#endif // DARRAY_H
|