Compare commits

...

10 Commits

10 changed files with 119 additions and 71 deletions

2
.gitignore vendored
View File

@ -68,4 +68,4 @@ _deps
vgcore.* vgcore.*
src/main.c src/main.c
.clangd .clangd
acl /acl

View File

@ -8,12 +8,5 @@ if(CCACHE_PROGRAM)
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif() endif()
set( CMAKE_EXPORT_COMPILE_COMMANDS ON ) set( CMAKE_EXPORT_COMPILE_COMMANDS ON )
set_property(TARGET acl PROPERTY C_STANDARD 11) set_property(TARGET acl PROPERTY C_STANDARD 99)
INCLUDE(TestBigEndian)
TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
if(IS_BIG_ENDIAN)
add_compile_definitions(ACL_BIG_ENDIAN)
else()
add_compile_definitions(ACL_LITTLE_ENDIAN)
endif()
target_link_libraries(acl PRIVATE m) target_link_libraries(acl PRIVATE m)

16
include/acl/alignof.h Normal file
View File

@ -0,0 +1,16 @@
#if defined(__STDC__)
#if (__STDC_VERSION__ >= 201112L)
#include <stdalign.h>
#include <stddef.h>
#else
#define alignof(type) offsetof(struct { char c; type d; }, d)
typedef union {
double dummy_double;
long double dummy_long_double;
long long dummy_long_long;
void *dummy_ptr;
void (*dummy_func_ptr)(void);
} max_align_t;
#endif
#endif
#include <stdio.h>

View File

@ -1,10 +1,27 @@
#ifndef _acl_array_h #ifndef _acl_array_h
#define _acl_array_h #define _acl_array_h
#include <stddef.h> #include <stddef.h>
size_t acl_arraylist_len(void *arraylist); #include <stdlib.h>
void acl_arraylist_free(void *arraylist); union acl_arraylist_meta {
double dummy_double;
long double dummy_long_double;
long long dummy_long_long;
void *dummy_ptr;
void (*dummy_func_ptr)(void);
struct {
size_t len;
size_t cap;
size_t sizeof_one_element;
};
};
inline size_t acl_arraylist_len(void *arraylist) {
return ((union acl_arraylist_meta*)arraylist - 1)->len;
}
inline void acl_arraylist_free(void *arraylist) {
free((union acl_arraylist_meta*)arraylist-1);
}
void* acl_arraylist_remove(void *arraylist_void, size_t index); void* acl_arraylist_remove(void *arraylist_void, size_t index);
void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element); void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element);
void* acl_arraylist_append(void *arraylist_void, void *element_void); void* acl_arraylist_append(void *arraylist_void, void *element);
void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element); void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element);
#endif #endif

View File

@ -1,5 +1,6 @@
#ifndef _acl_file_h #ifndef _acl_file_h
#define _acl_file_h #define _acl_file_h
#include <stdbool.h> #include <stdbool.h>
char* acl_ReadTextFile(const char *filePath, bool *sucess); #include <stdio.h>
char* acl_ReadTextFile(FILE *file, bool *sucess);
#endif #endif

View File

@ -1,15 +1,19 @@
#ifndef _acl_hashmap_h #ifndef _acl_hashmap_h
#define _acl_hashmap_h #define _acl_hashmap_h
#include <stddef.h> #include <stddef.h>
#include <stdbool.h>
#define acl_hashmap_create(type, keyType, bucketCount) acl_hashmap_init(bucketCount, sizeof(struct{keyType c; type d;}), offsetof(struct{keyType c; type d;}, d), sizeof(keyType))
union acl_hashmap_meta { union acl_hashmap_meta {
void *dummy_ptr; void *dummy_ptr;
struct { struct {
size_t bucketCount; size_t bucketBits;
size_t sizeOneElement; size_t sizeOneElement;
size_t keyBits; size_t keySize;
size_t offset;
}; };
}; };
/* set keySize to NULL for a dynamic keySize */ union acl_hashmap_meta* acl_hashmap_init(size_t bucketCount, size_t sizeOneElement, size_t offset, size_t keySize);
union acl_hashmap_meta* acl_hashmap_create(size_t bucketCount, size_t sizeOneElement, size_t keySize); void* acl_hashmap_declare(union acl_hashmap_meta *hashmap_meta, void *key);
void acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key, void *element); void* acl_hashmap_get(union acl_hashmap_meta *hashmap_meta, void *key);
bool acl_hashmap_remove;
#endif #endif

4
include/acl/sizeof.h Normal file
View File

@ -0,0 +1,4 @@
/* The following macro alows the usage of the sizeof operator without an identifier */
#include <stddef.h>
#define acl_sizeof(type) offsetof(struct{type a; char b;}, b)
offsetof(struct{type a; char b;}, b)

View File

@ -1,24 +1,21 @@
#include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <acl/array.h> #include <acl/array.h>
union arraylist_meta { size_t acl_arraylist_len(void *arraylist);
double dummy_double; void acl_arraylist_free(void *arraylist);
long double dummy_long_double;
long long dummy_long_long;
void *dummy_ptr;
void (*dummy_func_ptr)(void);
struct {
size_t len;
size_t cap;
size_t sizeof_one_element;
};
};
static inline void* acl_arraylist_resize(union acl_arraylist_meta *arraylist, int64_t relativLen) {
size_t cap = arraylist->cap + relativLen;
arraylist = realloc(arraylist, cap * arraylist->sizeof_one_element + sizeof *arraylist);
if(arraylist != NULL) {
arraylist->cap = cap;
}
return arraylist;
}
void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element) { void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element) {
union arraylist_meta *arraylist_new = malloc(array_size * sizeof_one_element + sizeof*arraylist_new); union acl_arraylist_meta *arraylist_new = malloc(array_size * sizeof_one_element + sizeof*arraylist_new);
if(arraylist_new == NULL) return NULL; if(arraylist_new == NULL) return NULL;
arraylist_new->len = array_size; arraylist_new->len = array_size;
arraylist_new->cap = array_size; arraylist_new->cap = array_size;
@ -27,21 +24,18 @@ void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element) {
} }
void* acl_arraylist_append(void *arraylist_void, void *element) { void* acl_arraylist_append(void *arraylist_void, void *element) {
void *append_pointer; void *element_append;
void **element_append = &append_pointer; union acl_arraylist_meta *arraylist = acl_arraylist_append_ptr(arraylist_void, &element_append);
union arraylist_meta *arraylist = acl_arraylist_append_ptr(arraylist_void, element_append);
if(arraylist == NULL) return NULL; if(arraylist == NULL) return NULL;
--arraylist; --arraylist;
memcpy(*element_append, element, arraylist->sizeof_one_element); memcpy(element_append, element, arraylist->sizeof_one_element);
return arraylist + 1; return arraylist + 1;
} }
void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element) { void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element) {
union arraylist_meta *arraylist = arraylist_void; union acl_arraylist_meta *arraylist = arraylist_void;
--arraylist; --arraylist;
if(arraylist->len == arraylist->cap) { if(arraylist->len == arraylist->cap) {
if(arraylist->len > 10) arraylist->cap = arraylist->len + 10; arraylist = acl_arraylist_resize(arraylist, 10);
else arraylist->cap = arraylist->len * 2 + 1;
arraylist = realloc(arraylist, arraylist->cap * arraylist->sizeof_one_element + sizeof *arraylist);
if(arraylist == NULL) return NULL; if(arraylist == NULL) return NULL;
} }
*append_element = (char*)(arraylist + 1) + arraylist->sizeof_one_element * arraylist->len; *append_element = (char*)(arraylist + 1) + arraylist->sizeof_one_element * arraylist->len;
@ -49,24 +43,16 @@ void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element) {
return arraylist + 1; return arraylist + 1;
} }
void acl_arraylist_free(void *arraylist) {
free((union arraylist_meta*)arraylist-1);
}
void* acl_arraylist_remove(void *arraylist_void, size_t index) { void* acl_arraylist_remove(void *arraylist_void, size_t index) {
union arraylist_meta *arraylist = (union arraylist_meta*)arraylist_void - 1; union acl_arraylist_meta *arraylist = (union acl_arraylist_meta*)arraylist_void - 1;
char *arraylist_char = arraylist_void; char *arraylist_char = arraylist_void;
if(index != arraylist->len - 1) { if(index != arraylist->len - 1) {
memcpy(arraylist_char + arraylist->sizeof_one_element * index, arraylist_char + arraylist->sizeof_one_element * (arraylist->len - 1), arraylist->sizeof_one_element); memcpy(arraylist_char + arraylist->sizeof_one_element * index, arraylist_char + arraylist->sizeof_one_element * (arraylist->len - 1), arraylist->sizeof_one_element);
} }
--arraylist->len; --arraylist->len;
if(arraylist->len < arraylist->cap - 10) { if(arraylist->len < arraylist->cap - 20) {
arraylist->cap = arraylist->len; void* arraylistTmp = acl_arraylist_resize(arraylist, -10);
arraylist = realloc(arraylist, arraylist->cap * arraylist->sizeof_one_element + sizeof *arraylist); if(arraylistTmp != NULL) arraylist = arraylistTmp;
} }
return arraylist; return arraylist + 1;
}
size_t acl_arraylist_len(void *arraylist) {
return ((union arraylist_meta*)arraylist - 1)->len;
} }

View File

@ -1,25 +1,23 @@
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
char* acl_ReadTextFile(const char *filePath, bool *sucess){ char* acl_ReadTextFile(FILE *file, bool *sucess){
FILE *fp = fopen(filePath, "rb");
size_t lSize; size_t lSize;
char *buffer; char *buffer;
if(!fp) { if(!file) {
*sucess = false; *sucess = false;
return buffer; return buffer;
} }
fseek(fp, 0L, SEEK_END); fseek(file, 0L, SEEK_END);
lSize = ftell(fp); lSize = ftell(file);
rewind(fp); rewind(file);
buffer = malloc(lSize + 1); buffer = malloc(lSize + 1);
if(!buffer) { if(!buffer) {
fclose(fp);
*sucess = false; *sucess = false;
return buffer; return buffer;
} }
size_t readReturn = fread(buffer, lSize, 1, fp); size_t readReturn = fread(buffer, lSize, 1, file);
fclose(fp);
*sucess = readReturn == 1; *sucess = readReturn == 1;
buffer[lSize] = '\0';
return buffer; return buffer;
} }

View File

@ -21,8 +21,20 @@
*hash_char ^= *i;\ *hash_char ^= *i;\
}\ }\
} }
static size_t acl_hash(void *data, size_t dataSize, size_t bucketBits);
inline static void* acl_hashmap_elementFromBucketAndKey(void **bucket, void *key, union acl_hashmap_meta *hashmap_meta) {
void **hashmap_buckets = (void**)(hashmap_meta + 1);
*bucket = hashmap_buckets[acl_hash(key, hashmap_meta->keySize, hashmap_meta->bucketBits)];
char *i = *bucket;
for(char *dest = *bucket + acl_arraylist_len(*bucket); i < dest; i += hashmap_meta->sizeOneElement) {
if(!memcmp(key, i, hashmap_meta->keySize)) {
return i;
}
}
return NULL;
}
size_t acl_hash(void *data, size_t dataSize, size_t bucketBits) { static size_t acl_hash(void *data, size_t dataSize, size_t bucketBits) {
switch(dataSize) { switch(dataSize) {
case 1: case 1:
return (uint8_t)(101u * *(uint8_t*)data >> (8 - bucketBits)); return (uint8_t)(101u * *(uint8_t*)data >> (8 - bucketBits));
@ -53,21 +65,38 @@ size_t acl_hash(void *data, size_t dataSize, size_t bucketBits) {
} }
} }
union acl_hashmap_meta* acl_hashmap_create(size_t bucketCount, size_t sizeOneElement, size_t keySize) { union acl_hashmap_meta* acl_hashmap_init(size_t bucketCount, size_t sizeOneElement, size_t offset, size_t keySize) {
union acl_hashmap_meta *hashmap_meta = malloc(sizeof *hashmap_meta + bucketCount * sizeof(void*)); union acl_hashmap_meta *hashmap_meta = malloc(sizeof *hashmap_meta + bucketCount * sizeof(void*));
hashmap_meta->bucketCount = bucketCount; hashmap_meta->bucketBits = log2(bucketCount);
hashmap_meta->sizeOneElement = sizeOneElement; hashmap_meta->sizeOneElement = sizeOneElement;
hashmap_meta->keyBits = log2(keySize); hashmap_meta->keySize = keySize;
hashmap_meta->offset = offset;
void **hashmap_buckets = (void**)(hashmap_meta + 1); void **hashmap_buckets = (void**)(hashmap_meta + 1);
for(size_t i; i < bucketCount; ++i) { for(size_t i = 0; i < bucketCount; ++i) {
hashmap_buckets[i] = acl_arraylist_create(0, sizeOneElement); hashmap_buckets[i] = acl_arraylist_create(0, sizeOneElement);
if(hashmap_buckets[i] == NULL) {
for(size_t j = 0; j < i; ++j) {
acl_arraylist_free(hashmap_buckets[j]);
}
return NULL;
}
} }
return hashmap_meta; return hashmap_meta;
} }
void acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key, void *element) { void* acl_hashmap_declare(union acl_hashmap_meta *hashmap_meta, void *key) {
void **hashmap_buckets = (void**)(hashmap_meta + 1); void *bucket;
if(acl_hashmap_elementFromBucketAndKey(&bucket, key, hashmap_meta) != NULL) return NULL;
void *elementKey; // pointer to the key inside the bucket
void *bucketTmp = acl_arraylist_append_ptr(bucket, &elementKey);
if(bucketTmp == NULL) return NULL;
bucket = bucketTmp;
memcpy(elementKey, key, hashmap_meta->keySize);
return (char*)elementKey + hashmap_meta->offset;
} }
int main() { void* acl_hashmap_get(union acl_hashmap_meta *hashmap_meta, void *key) {
void *bucket;
char *elementKey = acl_hashmap_elementFromBucketAndKey(&bucket, key, hashmap_meta);
if(elementKey == NULL) return NULL;
return elementKey + hashmap_meta->offset;
} }