commit a3099d90c5a27de1218323fe2b40182f58722b10 Author: Seoxi Ryouko Date: Fri Mar 28 02:07:47 2025 -0500 y no work diff --git a/hehe_array_test b/hehe_array_test new file mode 100755 index 0000000..a957692 Binary files /dev/null and b/hehe_array_test differ diff --git a/hehe_array_test.c b/hehe_array_test.c new file mode 100644 index 0000000..0c8ca2a --- /dev/null +++ b/hehe_array_test.c @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include "libhehe_array.h" + +#define ARRAY_LEN 10 +#define ARRAY2_START_INDEX 2 +#define ARRAY2_LEN 4 + +int main(void) { + struct HeheArray *array = hehe_alloc_array(ARRAY_LEN, sizeof(int)); + int i; + for(i = 0; i < ARRAY_LEN; i++) hehe_array_set(array, ARRAY_LEN - i - 1, &i); + for(i = 0; i < ARRAY_LEN; i++) { + printf("%d\n", *(int *)hehe_array_get(array, i)); + } + puts("\n\n"); + struct HeheArray *array2 = hehe_array_yoink(array, ARRAY2_START_INDEX, ARRAY2_LEN); + if(array2 == NULL) return 69; + for(i = 0; i < ARRAY2_LEN; i++) { + //printf("%d\n", *(int *)hehe_array_get(array2, i)); + } + puts("\n\n"); + for(i = 0; i < ARRAY_LEN - ARRAY2_LEN; i++) { + printf("%d\n", *(int *)hehe_array_get(array, i)); + } + return 0; +} diff --git a/libhehe_array.h b/libhehe_array.h new file mode 100644 index 0000000..1736024 --- /dev/null +++ b/libhehe_array.h @@ -0,0 +1,99 @@ + + +#ifndef HEHE_ARRAYLIB +#define HEHE_ARRAYLIB + +#define DEBUG +#ifdef DEBUG + +#include + +#endif + +#include +#include +#include + +struct HeheArray { + size_t type_size; + size_t len; + size_t max_len; + void *content; +}; + +static void *hehe_alloc_array(size_t initial_len, size_t type_size) { + if(initial_len == 0) initial_len = 1; + struct HeheArray *array = malloc(sizeof(*array)); + size_t max_len = 1; + while(initial_len >>= 1) max_len <<= 1; + + array->type_size = type_size; + array->len = initial_len; + array->max_len = max_len; + array->content = malloc(max_len * type_size); + memset(array->content, 0, max_len * type_size); + return array; +} + +static void hehe_array_set(struct HeheArray *array, size_t index, const void *value) { + if(index >= array->max_len) { + array->content = realloc(array->content, (array->max_len << 1) * array->type_size); + memset(array->content + array->max_len * array->type_size, 0, array->max_len * array->type_size); + array->max_len <<= 1; + } + if(index >= array->len) array->len = index + 1; + memcpy(array->content + index * array->type_size, value, array->type_size); +} + +static void *hehe_array_get(struct HeheArray *array, size_t index) { + if(index >= array->len) return NULL; // is ur fault + return array->content + array->type_size * index; +} + +static int hehe_array_copy(struct HeheArray *array, size_t index, void *value) { + if(index >= array->len) return -1; // is ur fault + memcpy(value, array->content + array->type_size * index, array->type_size); + return 0; +} + +static void hehe_array_set_len(struct HeheArray *array, size_t new_len) { + if(new_len >= array->max_len) { + size_t new_max_len = 1; + size_t len = new_len; + while(len >>= 1) new_max_len <<= 1; + array->content = realloc(array->content, new_max_len * array->type_size); + memset(array->content + array->max_len * array->type_size, 0, (new_max_len - array->max_len) * array->type_size); + array->max_len <<= 1; + } + array->len = new_len; +} + +static void hehe_array_push(struct HeheArray *array, const void *value) { + hehe_array_set(array, array->len, value); +} + +static int hehe_array_pop(struct HeheArray *array, void *value) { + return hehe_array_copy(array, array->len, value) || ((array->len -= 1) && 0); +} + +static struct HeheArray *hehe_array_yoink(struct HeheArray *array, size_t start_index, size_t len) { + puts("a"); + if(start_index + len >= array->len) return NULL; + puts("b"); + struct HeheArray *ret = hehe_alloc_array(len, array->type_size); + puts("c"); + memcpy(ret->content, array->content + start_index * array->type_size, len * array->type_size); + puts("d"); + memmove(array->content + start_index * array->type_size, array->content + (start_index + len) * array->type_size, (array->len - len - start_index) * array->type_size); + puts("e"); + hehe_array_set_len(array, array->len - len); + puts("f"); + return ret; +} + +static void hehe_array_free(struct HeheArray *array) { + free(array->content); + free(array); +} + +#endif