Finished implementation (for now)
This commit is contained in:
@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
#ifndef HEHE_ARRAYLIB
|
||||
#define HEHE_ARRAYLIB
|
||||
|
||||
@ -14,7 +12,7 @@ struct HeheArray {
|
||||
void *content;
|
||||
};
|
||||
|
||||
static void *hehe_alloc_array(size_t initial_len, size_t type_size) {
|
||||
static void *hehe_array_alloc(size_t initial_len, size_t type_size) {
|
||||
if(initial_len == 0) initial_len = 1;
|
||||
struct HeheArray *array = malloc(sizeof(*array));
|
||||
array->len = initial_len;
|
||||
@ -28,6 +26,12 @@ static void *hehe_alloc_array(size_t initial_len, size_t type_size) {
|
||||
return array;
|
||||
}
|
||||
|
||||
static void hehe_array_free(struct HeheArray *array) {
|
||||
free(array->content);
|
||||
free(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);
|
||||
@ -56,31 +60,75 @@ static void hehe_array_set_len(struct HeheArray *array, size_t 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->max_len <<= 1;
|
||||
} else if(new_len < array->len) {
|
||||
memset(array->content + new_len * array->type_size, 0, (array->len - new_len) * array->type_size);
|
||||
}
|
||||
array->len = new_len;
|
||||
}
|
||||
|
||||
static void hehe_array_insert(struct HeheArray *array, size_t index, const void *value) {
|
||||
hehe_array_set_len(array, array->len + 1);
|
||||
memmove(array->content + ((index + 1) * array->type_size), array->content + (index * array->type_size), (array->len - index - 1) * array->type_size);
|
||||
memcpy(array->content + index * array->type_size, value, array->type_size);
|
||||
}
|
||||
|
||||
static void hehe_array_remove(struct HeheArray *array, size_t index, void *value) {
|
||||
hehe_array_set_len(array, array->len - 1);
|
||||
memcpy(value, array->content + index * array->type_size, array->type_size);
|
||||
memmove(array->content + (index * array->type_size), array->content + ((index + 1) * array->type_size), (array->len - index + 1) * array->type_size);
|
||||
}
|
||||
|
||||
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 void hehe_array_pop(struct HeheArray *array, void *value) {
|
||||
hehe_array_remove(array, array->len - 1, value);
|
||||
}
|
||||
|
||||
static void hehe_array_delete(struct HeheArray *array, size_t index) {
|
||||
hehe_array_set_len(array, array->len - 1);
|
||||
memmove(array->content + (index * array->type_size), array->content + ((index + 1) * array->type_size), (array->len - index + 1) * array->type_size);
|
||||
}
|
||||
|
||||
static struct HeheArray *hehe_array_yoink(struct HeheArray *array, size_t start_index, size_t len) {
|
||||
if(start_index + len >= array->len) return NULL;
|
||||
struct HeheArray *ret = hehe_alloc_array(len, array->type_size);
|
||||
struct HeheArray *ret = hehe_array_alloc(len, array->type_size);
|
||||
memcpy(ret->content, array->content + start_index * array->type_size, len * array->type_size);
|
||||
memmove(array->content + start_index * array->type_size, array->content + (start_index + len) * array->type_size, (array->len - len - start_index) * array->type_size);
|
||||
hehe_array_set_len(array, array->len - len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void hehe_array_free(struct HeheArray *array) {
|
||||
free(array->content);
|
||||
free(array);
|
||||
static int hehe_array_insert_data(struct HeheArray *array, size_t index, void *data, size_t len) {
|
||||
if(index >= array->len) return -1;
|
||||
size_t current_len = array->len;
|
||||
hehe_array_set_len(array, current_len + len);
|
||||
memmove(array->content + (index + len) * array->type_size, array->content + index * array->type_size, (current_len - index) * array->type_size);
|
||||
memcpy(array->content + index * array->type_size, data, len * array->type_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int hehe_array_plop(struct HeheArray *array, size_t index, struct HeheArray *array2) {
|
||||
int ret = hehe_array_insert_data(array, index, array2->content, array2->len);
|
||||
if(ret) return ret;
|
||||
hehe_array_free(array2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef void (*Callback_func)(void *, size_t, struct HeheArray *);
|
||||
|
||||
static void hehe_array_foreach(struct HeheArray *array, Callback_func callback) {
|
||||
for(size_t i = 0; i < array->len; i++) callback(hehe_array_get(array, i), i, array);
|
||||
}
|
||||
|
||||
static struct HeheArray *hehe_array_clone(struct HeheArray *array) {
|
||||
struct HeheArray *ret = malloc(sizeof(*array));
|
||||
memcpy(ret, array, sizeof(*array));
|
||||
ret->content = malloc(array->max_len * array->type_size);
|
||||
memcpy(ret->content, array->content, array->max_len * array->type_size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user