From 0ea49d42c910777e8d6afb6a857daffbd402c405 Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Thu, 30 Jul 2020 20:17:02 +0200 Subject: [PATCH] Hashmap --- .gitignore | 1 + CMakeLists.txt | 16 ++++++--- build_and_run.sh | 2 +- include/acl/array.h | 1 + include/acl/hashmap.h | 12 ++++++- src/array.c | 14 ++++++-- src/hashmap.c | 81 ++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 114 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index e026c1c..bf2418a 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ _deps vgcore.* src/main.c .clangd +acl diff --git a/CMakeLists.txt b/CMakeLists.txt index 276f52d..ff867cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,19 @@ project(advanced_C_standard_library C) -set_property(GLOBAL PROPERTY C_STANDARD) -cmake_minimum_required(VERSION 3.9) +cmake_minimum_required(VERSION 3.17) include_directories(include) -file(GLOB SOURCES "src/*.c") -add_executable(test.out ${SOURCES}) +file(GLOB ACLSOURCES "src/*.c") +add_executable(acl ${ACLSOURCES}) find_program(CCACHE_PROGRAM ccache) if(CCACHE_PROGRAM) set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}") endif() set( CMAKE_EXPORT_COMPILE_COMMANDS ON ) +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) diff --git a/build_and_run.sh b/build_and_run.sh index 122da4e..63f61f7 100755 --- a/build_and_run.sh +++ b/build_and_run.sh @@ -2,4 +2,4 @@ rm -rf build cmake . make -./build/test.out +./acl diff --git a/include/acl/array.h b/include/acl/array.h index 64cbb3c..b8c3ade 100644 --- a/include/acl/array.h +++ b/include/acl/array.h @@ -6,4 +6,5 @@ void acl_arraylist_free(void *arraylist); 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_append(void *arraylist_void, void *element_void); +void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element); #endif diff --git a/include/acl/hashmap.h b/include/acl/hashmap.h index d65a400..d1cc7c3 100644 --- a/include/acl/hashmap.h +++ b/include/acl/hashmap.h @@ -1,4 +1,14 @@ #ifndef _acl_hashmap_h #define _acl_hashmap_h -unsigned hash(unsigned K); +#include +union acl_hashmap_meta { + void *dummy_ptr; + struct { + size_t bucketCount; + size_t sizeOneElement; + size_t keyBits; + }; +}; +union acl_hashmap_meta* acl_hashmap_create(size_t bucketCount, size_t sizeOneElement, size_t keySize); +void acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key, void *element); #endif diff --git a/src/array.c b/src/array.c index c46f229..05255ef 100644 --- a/src/array.c +++ b/src/array.c @@ -2,6 +2,7 @@ #include #include #include +#include union arraylist_meta { double dummy_double; @@ -25,17 +26,24 @@ void* acl_arraylist_create(size_t array_size, size_t sizeof_one_element) { } void* acl_arraylist_append(void *arraylist_void, void *element) { + void *append_pointer; + void **element_append = &append_pointer; + union arraylist_meta *arraylist = (union arraylist_meta*)acl_arraylist_append_ptr(arraylist_void, element_append) - 1; + memcpy(*element_append, element, arraylist->sizeof_one_element); + return arraylist + 1; +} +void* acl_arraylist_append_ptr(void *arraylist_void, void **append_element) { union arraylist_meta *arraylist = arraylist_void; --arraylist; if(arraylist->len == arraylist->cap) { if(arraylist->len > 10) arraylist->cap = arraylist->len + 10; - else arraylist->cap = arraylist->len * 2; + else arraylist->cap = arraylist->len * 2 + 1; arraylist = realloc(arraylist, arraylist->cap * arraylist->sizeof_one_element + sizeof *arraylist); 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; - return arraylist+1; + return arraylist + 1; } void acl_arraylist_free(void *arraylist) { diff --git a/src/hashmap.c b/src/hashmap.c index 07f2f94..d3c82c3 100644 --- a/src/hashmap.c +++ b/src/hashmap.c @@ -1,6 +1,79 @@ +#ifdef ACL_LITTLE_ENDIAN +#define LEFT_OR_RIGHT_SHIFT >> +#else +#ifdef ACL_BIG_ENDIAN +#define LEFT_OR_RIGHT_SHIFT << +#else +#error "endianiss not specified. make sure to properly add the cmake subdirectory." +#endif +#endif #include -const unsigned acl_w = sizeof (unsigned) * 8; -const unsigned acl_m = 9; -unsigned hash(unsigned K) { - return (149695736*K) >> (acl_w-acl_m); +#include +#include +#include +#include +#include +#include + +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)); + } + uint8_t rest = (uintptr_t)data % sizeof(uint64_t); + size_t data_len = dataSize / sizeof(uint64_t); + uint8_t restEnd = dataSize % sizeof(uint64_t); + if(restEnd) ++data_len; + uint64_t *cache; + if(rest) { + cache = malloc(data_len * sizeof *cache); + memcpy(cache, data, dataSize); + cache[data_len - 1] = 0; + } + else cache = data; + uint64_t hash = 0; + for(size_t i = 0; i < data_len - 1; ++i) { + hash ^= (uint64_t)(10223372036854775833u * cache[i]); + } + hash ^= (uint64_t)(10223372036854775833u * cache[data_len - 1] & (18446744073709551615u LEFT_OR_RIGHT_SHIFT (sizeof *cache - restEnd))); + free(cache); + return hash >> (64 - bucketBits); + } +} +union acl_hashmap_meta* acl_hashmap_create(size_t bucketCount, size_t sizeOneElement, size_t keySize) { + union acl_hashmap_meta *hashmap_meta = malloc(sizeof *hashmap_meta + bucketCount * sizeof(void*)); + hashmap_meta->bucketCount = bucketCount; + hashmap_meta->sizeOneElement = sizeOneElement; + hashmap_meta->keyBits = log2(keySize); + void **hashmap_buckets = (void**)(hashmap_meta + 1); + for(size_t i; i < bucketCount; ++i) { + hashmap_buckets[i] = acl_arraylist_create(0, sizeOneElement); + } + return hashmap_meta; +} + +void acl_hashmap_put(union acl_hashmap_meta *hashmap_meta, void *key, void *element) { + void **hashmap_buckets = (void**)(hashmap_meta + 1); + +} +#include +int main() { + int baum[3] = {89, 120, 36}; + printf("Hash: %lu\n", acl_hash(baum, 3 * sizeof *baum , 5)); }