Renamed project
This commit is contained in:
@ -1,20 +1,20 @@
|
||||
#ifndef HEHE_ARRAYLIB
|
||||
#define HEHE_ARRAYLIB
|
||||
#ifndef LIBKEKE_ARRAY
|
||||
#define LIBKEKE_ARRAY
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct HeheArray {
|
||||
struct KekeArray {
|
||||
size_t type_size;
|
||||
size_t len;
|
||||
size_t max_len;
|
||||
void *content;
|
||||
};
|
||||
|
||||
static void *hehe_array_alloc(size_t initial_len, size_t type_size) {
|
||||
static void *keke_array_alloc(size_t initial_len, size_t type_size) {
|
||||
if(initial_len == 0) initial_len = 1;
|
||||
struct HeheArray *array = malloc(sizeof(*array));
|
||||
struct KekeArray *array = malloc(sizeof(*array));
|
||||
array->len = initial_len;
|
||||
size_t max_len = 1;
|
||||
while(initial_len >>= 1) max_len <<= 1;
|
||||
@ -26,13 +26,13 @@ static void *hehe_array_alloc(size_t initial_len, size_t type_size) {
|
||||
return array;
|
||||
}
|
||||
|
||||
static void hehe_array_free(struct HeheArray *array) {
|
||||
static void keke_array_free(struct KekeArray *array) {
|
||||
free(array->content);
|
||||
free(array);
|
||||
}
|
||||
|
||||
|
||||
static void hehe_array_set(struct HeheArray *array, size_t index, const void *value) {
|
||||
static void keke_array_set(struct KekeArray *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);
|
||||
@ -42,18 +42,18 @@ static void hehe_array_set(struct HeheArray *array, size_t index, const void *va
|
||||
memcpy(array->content + index * array->type_size, value, array->type_size);
|
||||
}
|
||||
|
||||
static void *hehe_array_get(struct HeheArray *array, size_t index) {
|
||||
static void *keke_array_get(struct KekeArray *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) {
|
||||
static int keke_array_copy(struct KekeArray *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) {
|
||||
static void keke_array_set_len(struct KekeArray *array, size_t new_len) {
|
||||
if(new_len >= array->max_len) {
|
||||
size_t new_max_len = 1;
|
||||
size_t len = new_len;
|
||||
@ -67,64 +67,64 @@ static void hehe_array_set_len(struct HeheArray *array, size_t new_len) {
|
||||
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);
|
||||
static void keke_array_insert(struct KekeArray *array, size_t index, const void *value) {
|
||||
keke_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);
|
||||
static void keke_array_remove(struct KekeArray *array, size_t index, void *value) {
|
||||
keke_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 void keke_array_push(struct KekeArray *array, const void *value) {
|
||||
keke_array_set(array, array->len, value);
|
||||
}
|
||||
|
||||
static void hehe_array_pop(struct HeheArray *array, void *value) {
|
||||
hehe_array_remove(array, array->len - 1, value);
|
||||
static void keke_array_pop(struct KekeArray *array, void *value) {
|
||||
keke_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);
|
||||
static void keke_array_delete(struct KekeArray *array, size_t index) {
|
||||
keke_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) {
|
||||
static struct KekeArray *keke_array_yoink(struct KekeArray *array, size_t start_index, size_t len) {
|
||||
if(start_index + len >= array->len) return NULL;
|
||||
struct HeheArray *ret = hehe_array_alloc(len, array->type_size);
|
||||
struct KekeArray *ret = keke_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);
|
||||
keke_array_set_len(array, array->len - len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int hehe_array_insert_data(struct HeheArray *array, size_t index, void *data, size_t len) {
|
||||
static int keke_array_insert_data(struct KekeArray *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);
|
||||
keke_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);
|
||||
static int keke_array_plop(struct KekeArray *array, size_t index, struct KekeArray *array2) {
|
||||
int ret = keke_array_insert_data(array, index, array2->content, array2->len);
|
||||
if(ret) return ret;
|
||||
hehe_array_free(array2);
|
||||
keke_array_free(array2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef void (*Callback_func)(void *, size_t, struct HeheArray *);
|
||||
typedef void (*Callback_func)(void *, size_t, struct KekeArray *);
|
||||
|
||||
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 void keke_array_foreach(struct KekeArray *array, Callback_func callback) {
|
||||
for(size_t i = 0; i < array->len; i++) callback(keke_array_get(array, i), i, array);
|
||||
}
|
||||
|
||||
static struct HeheArray *hehe_array_clone(struct HeheArray *array) {
|
||||
struct HeheArray *ret = malloc(sizeof(*array));
|
||||
static struct KekeArray *keke_array_clone(struct KekeArray *array) {
|
||||
struct KekeArray *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);
|
||||
60
test.c
60
test.c
@ -2,84 +2,84 @@
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "libhehe_array.h"
|
||||
#include "libkeke_array.h"
|
||||
|
||||
#define ARRAY_LEN 10
|
||||
|
||||
void set(void *, size_t index, struct HeheArray *array) {
|
||||
hehe_array_set(array, ARRAY_LEN - index - 1, &index);
|
||||
void set(void *, size_t index, struct KekeArray *array) {
|
||||
keke_array_set(array, ARRAY_LEN - index - 1, &index);
|
||||
}
|
||||
|
||||
void output(void *i, size_t, struct HeheArray *) {
|
||||
void output(void *i, size_t, struct KekeArray *) {
|
||||
printf("%d, ", *(int *)i);
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
struct HeheArray *array = hehe_array_alloc(ARRAY_LEN, sizeof(int));
|
||||
struct KekeArray *array = keke_array_alloc(ARRAY_LEN, sizeof(int));
|
||||
|
||||
hehe_array_foreach(array, set);
|
||||
keke_array_foreach(array, set);
|
||||
puts("Expected value:\n9, 8, 7, 6, 5, 4, 3, 2, 1, 0,");
|
||||
hehe_array_foreach(array, output);
|
||||
keke_array_foreach(array, output);
|
||||
puts("\n");
|
||||
|
||||
struct HeheArray *array2 = hehe_array_yoink(array, 2, 4);
|
||||
struct KekeArray *array2 = keke_array_yoink(array, 2, 4);
|
||||
puts("Expected value:\n7, 6, 5, 4,");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
puts("\n");
|
||||
puts("Expected value:\n9, 8, 3, 2, 1, 0,");
|
||||
hehe_array_foreach(array, output);
|
||||
keke_array_foreach(array, output);
|
||||
puts("\n");
|
||||
|
||||
const int k = 100;
|
||||
hehe_array_insert(array2, 2, &k);
|
||||
keke_array_insert(array2, 2, &k);
|
||||
puts("Expected value:\n7, 6, 100, 5, 4,");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
puts("\n");
|
||||
|
||||
int j;
|
||||
hehe_array_remove(array2, 2, &j);
|
||||
keke_array_remove(array2, 2, &j);
|
||||
puts("Expected value:\n7, 6, 5, 0,\t100");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
printf("\t%d", j);
|
||||
puts("\n");
|
||||
|
||||
hehe_array_delete(array2, 3);
|
||||
keke_array_delete(array2, 3);
|
||||
puts("Expected value:\n7, 6, 5");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
puts("\n");
|
||||
|
||||
struct HeheArray *array3 = hehe_array_clone(array2);
|
||||
hehe_array_plop(array2, 1, array3); //array3 is freed
|
||||
struct KekeArray *array3 = keke_array_clone(array2);
|
||||
keke_array_plop(array2, 1, array3); //array3 is freed
|
||||
puts("Expected value:\n7, 7, 6, 5, 6, 5,");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
puts("\n");
|
||||
|
||||
hehe_array_pop(array2, &j);
|
||||
keke_array_pop(array2, &j);
|
||||
puts("Expected value:\n7, 7, 6, 5, 6,\t0");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
printf("\t%d", j);
|
||||
puts("\n");
|
||||
|
||||
hehe_array_copy(array2, 2, &j);
|
||||
hehe_array_push(array2, &j);
|
||||
keke_array_copy(array2, 2, &j);
|
||||
keke_array_push(array2, &j);
|
||||
puts("Expected value:\n7, 7, 6, 5, 6, 6,");
|
||||
hehe_array_foreach(array2, output);
|
||||
keke_array_foreach(array2, output);
|
||||
puts("\n");
|
||||
|
||||
hehe_array_set_len(array, 2);
|
||||
keke_array_set_len(array, 2);
|
||||
puts("Expected value:\n9, 8,");
|
||||
hehe_array_foreach(array, output);
|
||||
keke_array_foreach(array, output);
|
||||
puts("\n");
|
||||
|
||||
hehe_array_set(array, 4, &j);
|
||||
keke_array_set(array, 4, &j);
|
||||
puts("Expected value:\n9, 8, 0, 0, 6,");
|
||||
hehe_array_foreach(array, output);
|
||||
keke_array_foreach(array, output);
|
||||
puts("\n");
|
||||
|
||||
|
||||
hehe_array_free(array);
|
||||
hehe_array_free(array2);
|
||||
keke_array_free(array);
|
||||
keke_array_free(array2);
|
||||
puts("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user