From f84268c14d8d8170018da52d4f8829c31c09a2db Mon Sep 17 00:00:00 2001 From: MeirGavish Date: Thu, 7 Aug 2025 19:47:55 +0300 Subject: [PATCH] Added int list for joker pooling --- include/list.h | 19 +++++++++++++++ source/int_list.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 include/list.h create mode 100644 source/int_list.c diff --git a/include/list.h b/include/list.h new file mode 100644 index 0000000..b3ee749 --- /dev/null +++ b/include/list.h @@ -0,0 +1,19 @@ +#ifndef LIST_H +#define LIST_H + +#include + +typedef struct IntList +{ + int* _array; + int size; + int allocated_size; +} IntList; + +IntList *int_list_new(int init_size); +void int_list_destroy(IntList **list); +bool int_list_add(IntList *list, int value); +bool int_list_remove(IntList *list, int index); +int int_list_get(IntList *list, int index); + +#endif diff --git a/source/int_list.c b/source/int_list.c new file mode 100644 index 0000000..458b564 --- /dev/null +++ b/source/int_list.c @@ -0,0 +1,61 @@ +#include +#include +#include "list.h" + +IntList *int_list_new(int init_size) { + IntList *list = (IntList *)malloc(sizeof(IntList)); + if (list == NULL) return NULL; + list->_array = (int *)malloc(sizeof(int) * init_size); + if (!list->_array) + { + free(list); + return NULL; + } + list->size = 0; + list->allocated_size = init_size; + return list; +} + +void int_list_destroy(IntList **list) { + if (list == NULL || *list == NULL) + return; + { + free((*list)->_array); + free(*list); + } + + *list = NULL; +} + +bool int_list_add(IntList *list, int value) +{ + if (list->size >= list->allocated_size) + { + int new_size = list->allocated_size * 2; + int *new_arr = (int *)realloc(list->_array, sizeof(int) * new_size); + if (new_arr == NULL) + return false; + list->_array = new_arr; + list->allocated_size = new_size; + } + list->_array[list->size++] = value; + return true; +} + +bool int_list_remove(IntList *list, int index) { + if (index < 0 || index >= list->size) + return false; + for (int i = index; i < list->size - 1; ++i) + { + list->_array[i] = list->_array[i + 1]; + } + list->size--; + return true; +} + +int int_list_get(IntList *list, int index) +{ + if (index < 0 || index >= list->size) + return 0; + return list->_array[index]; +} \ No newline at end of file