Extended Array Library
This commit is contained in:
@ -3,7 +3,12 @@
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "libkeke_utils.h"
|
||||
|
||||
#define ARRAY_GET(array, i) ((array)->content + (array)->type_size * (i))
|
||||
|
||||
struct KekeArray {
|
||||
size_t type_size;
|
||||
@ -13,7 +18,7 @@ struct KekeArray {
|
||||
};
|
||||
|
||||
static void *keke_array_alloc(size_t initial_len, size_t type_size) {
|
||||
if(initial_len == 0) initial_len = 1;
|
||||
//if(initial_len == 0) initial_len = 1;
|
||||
struct KekeArray *array = malloc(sizeof(*array));
|
||||
array->len = initial_len;
|
||||
size_t max_len = 1;
|
||||
@ -44,12 +49,12 @@ static void keke_array_set(struct KekeArray *array, size_t index, const void *va
|
||||
|
||||
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;
|
||||
return ARRAY_GET(array, index);
|
||||
}
|
||||
|
||||
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);
|
||||
memcpy(value, ARRAY_GET(array, index), array->type_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -117,10 +122,10 @@ static int keke_array_plop(struct KekeArray *array, size_t index, struct KekeArr
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef void (*Callback_func)(void *, size_t, struct KekeArray *);
|
||||
typedef void (*Foreach_func)(void *, size_t, struct KekeArray *);
|
||||
|
||||
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 void keke_array_foreach(struct KekeArray *array, Foreach_func callback) {
|
||||
for(size_t i = 0; i < array->len; i++) callback(ARRAY_GET(array, i), i, array);
|
||||
}
|
||||
|
||||
static struct KekeArray *keke_array_clone(struct KekeArray *array) {
|
||||
@ -131,4 +136,46 @@ static struct KekeArray *keke_array_clone(struct KekeArray *array) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void keke_array_fill(struct KekeArray *array, const void *value) {
|
||||
for(size_t i = 0; i < array->len; i++) memcpy(ARRAY_GET(array, i), value, array->type_size);
|
||||
}
|
||||
|
||||
static void keke_array_reverse(struct KekeArray *array) {
|
||||
for(size_t i = 0; i < array->len / 2; i++) {
|
||||
keke_swap(ARRAY_GET(array, i), array->content + array->type_size * (array->len - i - 1), array->type_size);
|
||||
}
|
||||
}
|
||||
|
||||
typedef bool (*Test_func)(void *, size_t, struct KekeArray *);
|
||||
|
||||
static bool keke_array_some(struct KekeArray *array, Test_func test) {
|
||||
bool ret = false;
|
||||
for(size_t i = 0; i < array->len; i++) ret = ret || test(ARRAY_GET(array, i), i, array);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool keke_array_all(struct KekeArray *array, Test_func test) {
|
||||
bool ret = true;
|
||||
for(size_t i = 0; i < array->len; i++) ret = ret && test(ARRAY_GET(array, i), i, array);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct KekeArray *keke_array_filter(struct KekeArray *array, Test_func test) {
|
||||
struct KekeArray *ret = keke_array_alloc(0, array->type_size);
|
||||
for(size_t i = 0; i < array->len; i++) {
|
||||
if(test(ARRAY_GET(array, i), i, array)) {
|
||||
keke_array_push(ret, ARRAY_GET(array, i));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
typedef void (*B_func)(void *, void *);
|
||||
|
||||
static void keke_array_reduce(struct KekeArray *array, B_func op, void *acc) {
|
||||
for(size_t i = 0; i < array->len; i++) op(ARRAY_GET(array, i), acc);
|
||||
}
|
||||
|
||||
#undef ARRAY_GET
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user