list.h (1311B) download
1#pragma once
2
3#include <stddef.h>
4#include <stdio.h>
5
6#define LIST_DEFAULT_CAPACITY 5
7#define LIST_GROW 2 // double the capacity if exceed, half the capacity if half empty
8
9
10typedef struct list list_t;
11typedef struct list_iter list_iter_t;
12
13typedef void* (*list_malloc_t)(size_t);
14typedef void* (*list_realloc_t)(void*, size_t);
15typedef void (*list_free_t)(void*);
16
17list_t* list(size_t element_size, list_malloc_t list_malloc, list_realloc_t list_realloc, list_free_t list_free);
18list_t* list_default_alloc(size_t element_size);
19void list_free(list_t* this);
20int list_grow(list_t* this, size_t new_length);
21int list_add(list_t* this, void* element);
22void* list_get(list_t* this, ssize_t index);
23void* list_get_copy(list_t* this, ssize_t index);
24int list_shrink(list_t* this, ssize_t new_length);
25int list_remove(list_t* this, ssize_t index);
26void* list_remove_copy(list_t* this, ssize_t index);
27size_t list_length(list_t* this);
28size_t list_capacity(list_t* this);
29list_iter_t* list_iter(list_t* this);
30void list_iter_free(list_iter_t* iter);
31int list_iter_has_next(list_iter_t* iter);
32void* list_iter_next(list_iter_t* iter);
33void* list_iter_next_copy(list_iter_t* iter);