2023-01-07 19:27:16 +01:00
|
|
|
#include "array_list.h"
|
|
|
|
|
|
|
|
struct array_list* array_list_initialization() {
|
2024-09-25 14:34:33 +02:00
|
|
|
return array_list_initialization_with_capacity(ARRAY_LIST_INITIAL_CAPACITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct array_list* array_list_initialization_with_capacity(size_t capacity) {
|
2023-01-07 19:27:16 +01:00
|
|
|
struct array_list* list = malloc(sizeof(struct array_list));
|
2024-09-25 14:26:15 +02:00
|
|
|
if (list == NULL) {
|
|
|
|
perror("Error (array_list_initialization)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2024-09-25 14:34:33 +02:00
|
|
|
list->size = 0;
|
|
|
|
list->capacity = capacity;
|
|
|
|
list->capacity_step = capacity;
|
|
|
|
list->data = malloc(sizeof(void*) * list->capacity);
|
2024-09-25 14:26:15 +02:00
|
|
|
if (list->data == NULL) {
|
|
|
|
perror("Error (array_list_initialization)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2023-01-07 19:27:16 +01:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void array_list_add(struct array_list* list, void* element) {
|
|
|
|
if (list->size >= list->capacity) {
|
|
|
|
size_t previous_capacity = list->capacity;
|
2024-09-25 14:34:33 +02:00
|
|
|
list->capacity += list->capacity_step;
|
2023-01-07 19:27:16 +01:00
|
|
|
list->data = realloc(list->data, sizeof(void*) * list->capacity);
|
2024-09-25 14:26:15 +02:00
|
|
|
if (list->data == NULL) {
|
|
|
|
perror("Error (array_list_add)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2023-01-07 19:27:16 +01:00
|
|
|
for (size_t index = previous_capacity; index < list->capacity; index++) {
|
|
|
|
list->data[index] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list->data[list->size] = element;
|
|
|
|
list->size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void array_list_remove(struct array_list* list, size_t index) {
|
|
|
|
if (index >= list->size) {
|
|
|
|
return;
|
|
|
|
}
|
2023-10-13 11:04:38 +02:00
|
|
|
for (size_t i = index; i < list->size - 1; i++) {
|
|
|
|
list->data[i] = list->data[i + 1];
|
2023-01-07 19:27:16 +01:00
|
|
|
}
|
|
|
|
list->size--;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* array_list_get(struct array_list* list, size_t index) {
|
|
|
|
if (index >= list->size) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return list->data[index];
|
|
|
|
}
|
2023-08-03 19:42:50 +02:00
|
|
|
|
|
|
|
void array_list_free(struct array_list* list) {
|
|
|
|
free(list->data);
|
|
|
|
free(list);
|
|
|
|
}
|