Compare commits

...

16 Commits

11 changed files with 213 additions and 50 deletions

1
.gitignore vendored
View File

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

View File

@ -1,11 +1,12 @@
project(advanced_C_standard_library C) project(advanced_C_standard_library C)
set_property(GLOBAL PROPERTY C_STANDARD) cmake_minimum_required(VERSION 3.17)
cmake_minimum_required(VERSION 3.9)
include_directories(include) include_directories(include)
file(GLOB SOURCES "src/*.c") file(GLOB ACLSOURCES "src/*.c")
add_executable(test.out ${SOURCES}) add_executable(acl ${ACLSOURCES})
find_program(CCACHE_PROGRAM ccache) find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM) 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 99)
target_link_libraries(acl PRIVATE m)

View File

@ -2,4 +2,4 @@
rm -rf build rm -rf build
cmake . cmake .
make make
./build/test.out ./acl

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,8 +1,27 @@
#ifndef array_h #ifndef _acl_array_h
#define array_h #define _acl_array_h
#include <stddef.h> #include <stddef.h>
void acl_arraylist_free(void *arraylist); #include <stdlib.h>
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);
#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

19
include/acl/hashmap.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef _acl_hashmap_h
#define _acl_hashmap_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 {
void *dummy_ptr;
struct {
size_t bucketBits;
size_t sizeOneElement;
size_t keySize;
size_t offset;
};
};
union acl_hashmap_meta* acl_hashmap_init(size_t bucketCount, size_t sizeOneElement, size_t offset, size_t keySize);
void* acl_hashmap_declare(union acl_hashmap_meta *hashmap_meta, void *key);
void* acl_hashmap_get(union acl_hashmap_meta *hashmap_meta, void *key);
bool acl_hashmap_remove;
#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,23 +1,22 @@
#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>
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;
arraylist_new->len = array_size; arraylist_new->len = array_size;
arraylist_new->cap = array_size; arraylist_new->cap = array_size;
arraylist_new->sizeof_one_element = sizeof_one_element; arraylist_new->sizeof_one_element = sizeof_one_element;
@ -25,32 +24,35 @@ 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) {
union arraylist_meta *arraylist = arraylist_void; void *element_append;
union acl_arraylist_meta *arraylist = acl_arraylist_append_ptr(arraylist_void, &element_append);
if(arraylist == NULL) return NULL;
--arraylist;
memcpy(element_append, element, arraylist->sizeof_one_element);
return arraylist + 1;
}
void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element) {
union acl_arraylist_meta *arraylist = arraylist_void;
--arraylist; --arraylist;
if(arraylist->len == arraylist->cap) { if(arraylist->len == arraylist->cap) {
arraylist->cap = arraylist->len + 10; arraylist = acl_arraylist_resize(arraylist, 10);
arraylist = realloc(arraylist, arraylist->cap * arraylist->sizeof_one_element + sizeof *arraylist); if(arraylist == NULL) return NULL;
if(!arraylist) return NULL;
} }
memcpy((char*)(arraylist + 1) + arraylist->sizeof_one_element * arraylist->len, element, arraylist->sizeof_one_element); *append_element = (char*)(arraylist + 1) + arraylist->sizeof_one_element * arraylist->len;
++arraylist->len; ++arraylist->len;
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;
} }

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;
} }

102
src/hashmap.c Normal file
View File

@ -0,0 +1,102 @@
#include <math.h>
#include <acl/alignof.h>
#include <stddef.h>
#include <stdlib.h>
#include <acl/hashmap.h>
#include <acl/array.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#define acl_hashVarLen(type)\
if(((uintptr_t)data & (alignof(type) - 1)) == 0) {\
type *src;\
for(uint64_t *keySrc = data; (char*)(keySrc + 1) <= dest; ++keySrc) {\
src = (type*)keySrc;\
for(type *hashValue_ptr = (type*)&hashValue; src < (type*)(keySrc + 1); ++src, ++hashValue_ptr) {\
*hashValue_ptr ^= *src;\
}\
}\
char *hash_char = (char*)&hashValue;\
for(char *i = (char*)src; i <= dest; ++i, ++hashValue) {\
*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;
}
static size_t acl_hash(void *data, size_t dataSize, size_t bucketBits) {
switch(dataSize) {
case 1:
return (uint8_t)(101u * *(uint8_t*)data >> (8 - bucketBits));
case 2: {
uint16_t cache;
memcpy(&cache, data, 2);
return (uint16_t)(15227u * cache >> (16 - bucketBits));
}
case 3:
case 4: {
uint32_t cache = 0;
memcpy(&cache, data, dataSize);
return (uint32_t)(4000846301u * cache >> (32 - bucketBits));
}
default:
if(dataSize <= 8) {
uint64_t cache = 0;
memcpy(&cache, data, dataSize);
return (uint64_t)(10223372036854775833u * cache >> (64 - bucketBits));
}
uint64_t hashValue = 0;
char * dest = (char*)data + dataSize - 1;
acl_hashVarLen(uint64_t)
else acl_hashVarLen(uint32_t)
else acl_hashVarLen(uint16_t)
else acl_hashVarLen(uint8_t)
return hashValue * 10223372036854775833u >> (64 - bucketBits);
}
}
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*));
hashmap_meta->bucketBits = log2(bucketCount);
hashmap_meta->sizeOneElement = sizeOneElement;
hashmap_meta->keySize = keySize;
hashmap_meta->offset = offset;
void **hashmap_buckets = (void**)(hashmap_meta + 1);
for(size_t i = 0; i < bucketCount; ++i) {
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;
}
void* acl_hashmap_declare(union acl_hashmap_meta *hashmap_meta, void *key) {
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;
}
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;
}