Compare commits
8 Commits
ffffca52a2
...
meta
| Author | SHA1 | Date | |
|---|---|---|---|
| 35f0d4cc10 | |||
| 8fb4ca0749 | |||
| 6c08c28ae5 | |||
| c23cdb9e1e | |||
| 065f5db5cf | |||
| 03ad39114a | |||
| ffa15aa06d | |||
| f9ad42da25 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1 @@
|
|||||||
hehe_array_test
|
test_array
|
||||||
|
|||||||
26
LICENSE
Normal file
26
LICENSE
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
MIT NON-AI License
|
||||||
|
|
||||||
|
Copyright (c) 2025, Seoxi Ryouko
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of the software and associated documentation files (the "Software"),
|
||||||
|
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions.
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
In addition, the following restrictions apply:
|
||||||
|
|
||||||
|
1. The Software and any modifications made to it may not be used for the purpose of training or improving machine learning algorithms,
|
||||||
|
including but not limited to artificial intelligence, natural language processing, or data mining. This condition applies to any derivatives,
|
||||||
|
modifications, or updates based on the Software code. Any usage of the Software in an AI-training dataset is considered a breach of this License.
|
||||||
|
|
||||||
|
2. The Software may not be included in any dataset used for training or improving machine learning algorithms,
|
||||||
|
including but not limited to artificial intelligence, natural language processing, or data mining.
|
||||||
|
|
||||||
|
3. Any person or organization found to be in violation of these restrictions will be subject to legal action and may be held liable
|
||||||
|
for any damages resulting from such use.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||||
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
|
||||||
|
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
5
Makefile
Normal file
5
Makefile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
CC = clang
|
||||||
|
CFLAGS = -O2 -Wall -Wextra -Wno-c2x-extensions
|
||||||
|
test_array:
|
||||||
|
$(CC) $(CFLAGS) -o test_array test_array.c libkeke_array.c
|
||||||
|
./test_array
|
||||||
11
README.md
Normal file
11
README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
libkeke
|
||||||
|
=======
|
||||||
|
|
||||||
|
personal utils library
|
||||||
|
named after my third favorite [Baba is You](https://hempuli.itch.io/baba) character
|
||||||
|
|
||||||
|
currently includes submodules:
|
||||||
|
|
||||||
|
libkeke_array
|
||||||
|
-------------
|
||||||
|
very usable array library, contained in a single header file
|
||||||
@ -1,29 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#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;
|
|
||||||
}
|
|
||||||
@ -1,86 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
#ifndef HEHE_ARRAYLIB
|
|
||||||
#define HEHE_ARRAYLIB
|
|
||||||
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
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));
|
|
||||||
array->len = initial_len;
|
|
||||||
size_t max_len = 1;
|
|
||||||
while(initial_len >>= 1) max_len <<= 1;
|
|
||||||
|
|
||||||
array->type_size = type_size;
|
|
||||||
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) {
|
|
||||||
if(start_index + len >= array->len) return NULL;
|
|
||||||
struct HeheArray *ret = hehe_alloc_array(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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
175
libkeke_array.c
Normal file
175
libkeke_array.c
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
#ifndef LIBKEKE_ARRAY
|
||||||
|
#define LIBKEKE_ARRAY
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include "libkeke_utils.h"
|
||||||
|
#include "libkeke_array.h"
|
||||||
|
|
||||||
|
#define ARRAY_GET(array, i) ((array)->content + (array)->type_size * (i))
|
||||||
|
|
||||||
|
KekeArray *keke_array_alloc(size_t initial_len, size_t type_size) {
|
||||||
|
//if(initial_len == 0) initial_len = 1;
|
||||||
|
KekeArray *array = malloc(sizeof(*array));
|
||||||
|
array->len = initial_len;
|
||||||
|
size_t max_len = 1;
|
||||||
|
while(initial_len >>= 1) max_len <<= 1;
|
||||||
|
|
||||||
|
array->type_size = type_size;
|
||||||
|
array->max_len = max_len;
|
||||||
|
array->content = malloc(max_len * type_size);
|
||||||
|
memset(array->content, 0, max_len * type_size);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_free(KekeArray *array) {
|
||||||
|
free(array->content);
|
||||||
|
free(array);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void keke_array_set(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);
|
||||||
|
array->max_len <<= 1;
|
||||||
|
}
|
||||||
|
if(index >= array->len) array->len = index + 1;
|
||||||
|
memcpy(array->content + index * array->type_size, value, array->type_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void *keke_array_get(KekeArray *array, size_t index) {
|
||||||
|
if(index >= array->len) return NULL; // is ur fault
|
||||||
|
return ARRAY_GET(array, index);
|
||||||
|
}
|
||||||
|
|
||||||
|
int keke_array_copy(KekeArray *array, size_t index, void *value) {
|
||||||
|
if(index >= array->len) return -1; // is ur fault
|
||||||
|
memcpy(value, ARRAY_GET(array, index), array->type_size);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_set_len(KekeArray *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;
|
||||||
|
} 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_insert(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_remove(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_push(KekeArray *array, const void *value) {
|
||||||
|
keke_array_set(array, array->len, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_pop(KekeArray *array, void *value) {
|
||||||
|
keke_array_remove(array, array->len - 1, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_delete(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);
|
||||||
|
}
|
||||||
|
|
||||||
|
KekeArray *keke_array_yoink(KekeArray *array, size_t start_index, size_t len) {
|
||||||
|
if(start_index + len >= array->len) return NULL;
|
||||||
|
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);
|
||||||
|
keke_array_set_len(array, array->len - len);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int keke_array_insert_data(KekeArray *array, size_t index, void *data, size_t len) {
|
||||||
|
if(index >= array->len) return -1;
|
||||||
|
size_t current_len = array->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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int keke_array_plop(KekeArray *array, size_t index, KekeArray *array2) {
|
||||||
|
int ret = keke_array_insert_data(array, index, array2->content, array2->len);
|
||||||
|
if(ret) return ret;
|
||||||
|
keke_array_free(array2);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void (*Foreach_func)(void *, size_t, KekeArray *);
|
||||||
|
|
||||||
|
void keke_array_foreach(KekeArray *array, Foreach_func callback) {
|
||||||
|
for(size_t i = 0; i < array->len; i++) callback(ARRAY_GET(array, i), i, array);
|
||||||
|
}
|
||||||
|
|
||||||
|
KekeArray *keke_array_clone(KekeArray *array) {
|
||||||
|
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);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_fill(KekeArray *array, const void *value) {
|
||||||
|
for(size_t i = 0; i < array->len; i++) memcpy(ARRAY_GET(array, i), value, array->type_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keke_array_reverse(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, KekeArray *);
|
||||||
|
|
||||||
|
bool keke_array_some(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool keke_array_all(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
KekeArray *keke_array_filter(KekeArray *array, Test_func test) {
|
||||||
|
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 *);
|
||||||
|
|
||||||
|
void keke_array_reduce(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
|
||||||
49
libkeke_array.h
Normal file
49
libkeke_array.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#ifndef LIBKEKE_ARRAY
|
||||||
|
#define LIBKEKE_ARRAY
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t type_size;
|
||||||
|
size_t len;
|
||||||
|
size_t max_len;
|
||||||
|
void *content;
|
||||||
|
} KekeArray;
|
||||||
|
|
||||||
|
KekeArray *keke_array_alloc(size_t initial_len, size_t type_size);
|
||||||
|
void keke_array_free(KekeArray *array);
|
||||||
|
|
||||||
|
void keke_array_set(KekeArray *array, size_t index, const void *value);
|
||||||
|
void *keke_array_get(KekeArray *array, size_t index);
|
||||||
|
|
||||||
|
int keke_array_copy(KekeArray *array, size_t index, void *value);
|
||||||
|
void keke_array_set_len(KekeArray *array, size_t new_len);
|
||||||
|
void keke_array_insert(KekeArray *array, size_t index, const void *value);
|
||||||
|
void keke_array_remove(KekeArray *array, size_t index, void *value);
|
||||||
|
void keke_array_push(KekeArray *array, const void *value);
|
||||||
|
void keke_array_pop(KekeArray *array, void *value);
|
||||||
|
void keke_array_delete(KekeArray *array, size_t index);
|
||||||
|
|
||||||
|
KekeArray *keke_array_yoink(KekeArray *array, size_t start_index, size_t len);
|
||||||
|
int keke_array_insert_data(KekeArray *array, size_t index, void *data, size_t len);
|
||||||
|
int keke_array_plop(KekeArray *array, size_t index, KekeArray *array2);
|
||||||
|
|
||||||
|
typedef void (*Foreach_func)(void *, size_t, KekeArray *);
|
||||||
|
void keke_array_foreach(KekeArray *array, Foreach_func callback);
|
||||||
|
KekeArray *keke_array_clone(KekeArray *array);
|
||||||
|
|
||||||
|
void keke_array_fill(KekeArray *array, const void *value);
|
||||||
|
void keke_array_reverse(KekeArray *array);
|
||||||
|
|
||||||
|
typedef bool (*Test_func)(void *, size_t, KekeArray *);
|
||||||
|
bool keke_array_some(KekeArray *array, Test_func test);
|
||||||
|
bool keke_array_all(KekeArray *array, Test_func test);
|
||||||
|
|
||||||
|
KekeArray *keke_array_filter(KekeArray *array, Test_func test);
|
||||||
|
|
||||||
|
typedef void (*B_func)(void *, void *);
|
||||||
|
void keke_array_reduce(KekeArray *array, B_func op, void *acc);
|
||||||
|
|
||||||
|
#endif
|
||||||
18
libkeke_utils.h
Normal file
18
libkeke_utils.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef LIBKEKE_UTILS
|
||||||
|
#define LIBKEKE_UTILS
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#define BOOL_PRETTY(b) (b ? "True" : "False")
|
||||||
|
|
||||||
|
static void keke_swap(void *a, void *b, size_t size) {
|
||||||
|
char *aa = a;
|
||||||
|
char *bb = b;
|
||||||
|
for(size_t i = 0; i < size; i++) {
|
||||||
|
aa[i] ^= bb[i];
|
||||||
|
bb[i] ^= aa[i];
|
||||||
|
aa[i] ^= bb[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
132
test_array.c
Normal file
132
test_array.c
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "libkeke_array.h"
|
||||||
|
#include "libkeke_utils.h"
|
||||||
|
|
||||||
|
#define ARRAY_LEN 10
|
||||||
|
|
||||||
|
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 KekeArray *) {
|
||||||
|
printf("%d, ", *(int *)i);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sub4(void *i, size_t, struct KekeArray *) {
|
||||||
|
*(int *)i -= 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool heart(void *i, size_t, struct KekeArray *) {
|
||||||
|
return *(int *)i < 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
void add(void *vx, void *vacc) {
|
||||||
|
int *x = (int *)vx;
|
||||||
|
int *acc = (int *)vacc;
|
||||||
|
*acc += *x;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
struct KekeArray *array = keke_array_alloc(ARRAY_LEN, sizeof(int));
|
||||||
|
|
||||||
|
puts("Expected value:\n0");
|
||||||
|
printf("%d", *(int *)keke_array_get(array, 0)); //initialized to 0
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_foreach(array, set);
|
||||||
|
puts("Expected value:\n9, 8, 7, 6, 5, 4, 3, 2, 1, 0,");
|
||||||
|
keke_array_foreach(array, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
struct KekeArray *array2 = keke_array_yoink(array, 2, 4);
|
||||||
|
puts("Expected value:\n7, 6, 5, 4,");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
puts("Expected value:\n9, 8, 3, 2, 1, 0,");
|
||||||
|
keke_array_foreach(array, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
const int k = 100;
|
||||||
|
keke_array_insert(array2, 2, &k);
|
||||||
|
puts("Expected value:\n7, 6, 100, 5, 4,");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
int j;
|
||||||
|
keke_array_remove(array2, 2, &j);
|
||||||
|
puts("Expected value:\n7, 6, 5, 0,\t100");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
printf("\t%d", j);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_delete(array2, 3);
|
||||||
|
puts("Expected value:\n7, 6, 5");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
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,");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_pop(array2, &j);
|
||||||
|
puts("Expected value:\n7, 7, 6, 5, 6,\t0");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
printf("\t%d", j);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_copy(array2, 2, &j);
|
||||||
|
keke_array_push(array2, &j);
|
||||||
|
puts("Expected value:\n7, 7, 6, 5, 6, 6,");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_set_len(array, 2);
|
||||||
|
puts("Expected value:\n9, 8,");
|
||||||
|
keke_array_foreach(array, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_set(array, 4, &j);
|
||||||
|
puts("Expected value:\n9, 8, 0, 0, 6,");
|
||||||
|
keke_array_foreach(array, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_fill(array, &j);
|
||||||
|
puts("Expected value:\n6, 6, 6, 6, 6,");
|
||||||
|
keke_array_foreach(array, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_reverse(array2);
|
||||||
|
puts("Expected value:\n6, 6, 5, 6, 7, 7,");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
keke_array_foreach(array2, sub4);
|
||||||
|
puts("Expected value:\n2, 2, 1, 2, 3, 3,");
|
||||||
|
keke_array_foreach(array2, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
bool h = keke_array_some(array2, heart);
|
||||||
|
bool g = keke_array_all(array2, heart);
|
||||||
|
puts("Expected value:\nTrue, False");
|
||||||
|
printf("%s, %s\n\n", BOOL_PRETTY(h), BOOL_PRETTY(g));
|
||||||
|
|
||||||
|
struct KekeArray *array4 = keke_array_filter(array2, heart);
|
||||||
|
puts("Expected value:\n2, 2, 1, 2,");
|
||||||
|
keke_array_foreach(array4, output);
|
||||||
|
puts("\n");
|
||||||
|
|
||||||
|
j = 0;
|
||||||
|
keke_array_reduce(array4, add, &j);
|
||||||
|
printf("Expected value:\n7\n%d\n\n", j);
|
||||||
|
|
||||||
|
keke_array_free(array);
|
||||||
|
keke_array_free(array2);
|
||||||
|
keke_array_free(array4);
|
||||||
|
puts("\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user