Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0ee2fe8874 | |||
| b444f539af |
@@ -1,5 +1,5 @@
|
|||||||
CC = clang
|
CC = zig cc
|
||||||
CFLAGS = -O2 -Wall -Wextra -Wno-c2x-extensions
|
CFLAGS = -O2 -std=c23 #-Wall -Wextra -Wno-c2x-extensions
|
||||||
test_array:
|
test_array:
|
||||||
$(CC) $(CFLAGS) -o test_array test_array.c libkeke_array.c
|
$(CC) $(CFLAGS) -o test_array test_array.c libkeke_array.c
|
||||||
./test_array
|
./test_array
|
||||||
|
|||||||
+19
-3
@@ -9,6 +9,13 @@
|
|||||||
#include "libkeke_utils.h"
|
#include "libkeke_utils.h"
|
||||||
#include "libkeke_array.h"
|
#include "libkeke_array.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
size_t type_size;
|
||||||
|
size_t len;
|
||||||
|
size_t max_len;
|
||||||
|
void *content;
|
||||||
|
} KekeArray;
|
||||||
|
|
||||||
#define ARRAY_GET(array, i) ((array)->content + (array)->type_size * (i))
|
#define ARRAY_GET(array, i) ((array)->content + (array)->type_size * (i))
|
||||||
|
|
||||||
KekeArray *keke_array_alloc(size_t initial_len, size_t type_size) {
|
KekeArray *keke_array_alloc(size_t initial_len, size_t type_size) {
|
||||||
@@ -20,8 +27,7 @@ KekeArray *keke_array_alloc(size_t initial_len, size_t type_size) {
|
|||||||
|
|
||||||
array->type_size = type_size;
|
array->type_size = type_size;
|
||||||
array->max_len = max_len;
|
array->max_len = max_len;
|
||||||
array->content = malloc(max_len * type_size);
|
array->content = calloc(max_len, type_size);
|
||||||
memset(array->content, 0, max_len * type_size);
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +36,6 @@ void keke_array_free(KekeArray *array) {
|
|||||||
free(array);
|
free(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void keke_array_set(KekeArray *array, size_t index, const void *value) {
|
void keke_array_set(KekeArray *array, size_t index, const void *value) {
|
||||||
if(index >= array->max_len) {
|
if(index >= array->max_len) {
|
||||||
array->content = realloc(array->content, (array->max_len << 1) * array->type_size);
|
array->content = realloc(array->content, (array->max_len << 1) * array->type_size);
|
||||||
@@ -67,6 +72,11 @@ 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_insert(KekeArray *array, size_t index, const void *value) {
|
||||||
|
if(array->len + 1 > 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;
|
||||||
|
}
|
||||||
keke_array_set_len(array, array->len + 1);
|
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);
|
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);
|
memcpy(array->content + index * array->type_size, value, array->type_size);
|
||||||
@@ -91,6 +101,12 @@ void keke_array_delete(KekeArray *array, size_t index) {
|
|||||||
memmove(array->content + (index * array->type_size), array->content + ((index + 1) * array->type_size), (array->len - index + 1) * 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_swap_delete(KekeArray *array, size_t index) {
|
||||||
|
size_t new_len = array->len - 1;
|
||||||
|
keke_swap(ARRAY_GET(array, index), ARRAY_GET(array, new_len), array->type_size);
|
||||||
|
keke_array_set_len(array, new_len);
|
||||||
|
}
|
||||||
|
|
||||||
KekeArray *keke_array_yoink(KekeArray *array, size_t start_index, size_t len) {
|
KekeArray *keke_array_yoink(KekeArray *array, size_t start_index, size_t len) {
|
||||||
if(start_index + len >= array->len) return NULL;
|
if(start_index + len >= array->len) return NULL;
|
||||||
KekeArray *ret = keke_array_alloc(len, array->type_size);
|
KekeArray *ret = keke_array_alloc(len, array->type_size);
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "libkeke_symbol.h"
|
#include "libkeke_symbol.h"
|
||||||
|
|
||||||
static KekeSymbol iota = KEKE_NULL_SYMBOL + 1;
|
static _Atomic KekeSymbol iota = KEKE_NULL_SYMBOL + 1;
|
||||||
|
|
||||||
KekeSymbol keke_new_symbol() {
|
KekeSymbol keke_new_symbol() {
|
||||||
return iota++;
|
return iota++;
|
||||||
|
|||||||
+8
-8
@@ -7,19 +7,19 @@
|
|||||||
|
|
||||||
#define ARRAY_LEN 10
|
#define ARRAY_LEN 10
|
||||||
|
|
||||||
void set(void *, size_t index, struct KekeArray *array) {
|
void set(void *, size_t index, KekeArray *array) {
|
||||||
keke_array_set(array, ARRAY_LEN - index - 1, &index);
|
keke_array_set(array, ARRAY_LEN - index - 1, &index);
|
||||||
}
|
}
|
||||||
|
|
||||||
void output(void *i, size_t, struct KekeArray *) {
|
void output(void *i, size_t, KekeArray *) {
|
||||||
printf("%d, ", *(int *)i);
|
printf("%d, ", *(int *)i);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sub4(void *i, size_t, struct KekeArray *) {
|
void sub4(void *i, size_t, KekeArray *) {
|
||||||
*(int *)i -= 4;
|
*(int *)i -= 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool heart(void *i, size_t, struct KekeArray *) {
|
bool heart(void *i, size_t, KekeArray *) {
|
||||||
return *(int *)i < 3;
|
return *(int *)i < 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ void add(void *vx, void *vacc) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct KekeArray *array = keke_array_alloc(ARRAY_LEN, sizeof(int));
|
KekeArray *array = keke_array_alloc(ARRAY_LEN, sizeof(int));
|
||||||
|
|
||||||
puts("Expected value:\n0");
|
puts("Expected value:\n0");
|
||||||
printf("%d", *(int *)keke_array_get(array, 0)); //initialized to 0
|
printf("%d", *(int *)keke_array_get(array, 0)); //initialized to 0
|
||||||
@@ -41,7 +41,7 @@ int main(void) {
|
|||||||
keke_array_foreach(array, output);
|
keke_array_foreach(array, output);
|
||||||
puts("\n");
|
puts("\n");
|
||||||
|
|
||||||
struct KekeArray *array2 = keke_array_yoink(array, 2, 4);
|
KekeArray *array2 = keke_array_yoink(array, 2, 4);
|
||||||
puts("Expected value:\n7, 6, 5, 4,");
|
puts("Expected value:\n7, 6, 5, 4,");
|
||||||
keke_array_foreach(array2, output);
|
keke_array_foreach(array2, output);
|
||||||
puts("\n");
|
puts("\n");
|
||||||
@@ -67,7 +67,7 @@ int main(void) {
|
|||||||
keke_array_foreach(array2, output);
|
keke_array_foreach(array2, output);
|
||||||
puts("\n");
|
puts("\n");
|
||||||
|
|
||||||
struct KekeArray *array3 = keke_array_clone(array2);
|
KekeArray *array3 = keke_array_clone(array2);
|
||||||
keke_array_plop(array2, 1, array3); //array3 is freed
|
keke_array_plop(array2, 1, array3); //array3 is freed
|
||||||
puts("Expected value:\n7, 7, 6, 5, 6, 5,");
|
puts("Expected value:\n7, 7, 6, 5, 6, 5,");
|
||||||
keke_array_foreach(array2, output);
|
keke_array_foreach(array2, output);
|
||||||
@@ -115,7 +115,7 @@ int main(void) {
|
|||||||
puts("Expected value:\nTrue, False");
|
puts("Expected value:\nTrue, False");
|
||||||
printf("%s, %s\n\n", BOOL_PRETTY(h), BOOL_PRETTY(g));
|
printf("%s, %s\n\n", BOOL_PRETTY(h), BOOL_PRETTY(g));
|
||||||
|
|
||||||
struct KekeArray *array4 = keke_array_filter(array2, heart);
|
KekeArray *array4 = keke_array_filter(array2, heart);
|
||||||
puts("Expected value:\n2, 2, 1, 2,");
|
puts("Expected value:\n2, 2, 1, 2,");
|
||||||
keke_array_foreach(array4, output);
|
keke_array_foreach(array4, output);
|
||||||
puts("\n");
|
puts("\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user