59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
//
|
|
// Created by Lennart on 02/12/2023.
|
|
//
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#ifndef LIST_NAME
|
|
#define LIST_NAME List
|
|
#endif //LIST_NAME
|
|
|
|
#ifndef LIST_PREFIX
|
|
#define LIST_PREFIX list
|
|
#endif //LIST_PREFIX
|
|
|
|
#ifndef ELEMENT_TYPE
|
|
#define ELEMENT_TYPE void*
|
|
#endif //ELEMENT_TYPE
|
|
|
|
#define LIST_CONCAT(x, y) x ## _ ## y
|
|
#define LIST_CONCAT1(x, y) LIST_CONCAT(x, y)
|
|
#define LIST_FN(name) LIST_CONCAT1(LIST_PREFIX, name)
|
|
|
|
typedef struct {
|
|
ELEMENT_TYPE *data;
|
|
int capacity;
|
|
int length;
|
|
} LIST_NAME;
|
|
|
|
static __inline LIST_NAME* LIST_FN(create)(int capacity) {
|
|
LIST_NAME *alist = malloc(sizeof (LIST_NAME));
|
|
alist->data = malloc(sizeof (ELEMENT_TYPE) * capacity);
|
|
alist->capacity = capacity;
|
|
alist->length = 0;
|
|
return alist;
|
|
}
|
|
|
|
static __inline void LIST_FN(add)(LIST_NAME *alist, ELEMENT_TYPE value) {
|
|
if(alist->length >= alist->capacity) {
|
|
alist->data = realloc(alist->data, sizeof (ELEMENT_TYPE) * (alist->capacity *= 2));
|
|
}
|
|
|
|
alist->data[alist->length++] = value;
|
|
}
|
|
|
|
static __inline void LIST_FN(remove)(LIST_NAME *alist, int index) {
|
|
#ifdef LIST_UNORDERED
|
|
alist->data[index] = alist->data[--alist->length];
|
|
#else
|
|
memmove(&alist->data[index], &alist->data[index + 1], sizeof(ELEMENT_TYPE) * (alist->capacity - index - 1));
|
|
alist->length--;
|
|
#endif //LIST_UNORDERED
|
|
}
|
|
|
|
static __inline void LIST_FN(free)(LIST_NAME *alist) {
|
|
free(alist->data);
|
|
free(alist);
|
|
}
|