From f3d92bc25e42d452831254ad510f587a19d77fd1 Mon Sep 17 00:00:00 2001 From: Felix Brendel Date: Tue, 10 Nov 2020 23:01:26 +0100 Subject: [PATCH] yes --- bucket_allocator.hpp | 10 +++++----- build.bat | 2 +- hashmap.hpp | 27 +++++++++++++++++++++++++++ test.cpp | 5 +---- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/bucket_allocator.hpp b/bucket_allocator.hpp index c8cdcf0..499250c 100644 --- a/bucket_allocator.hpp +++ b/bucket_allocator.hpp @@ -35,7 +35,7 @@ class Bucket_Allocator { } public: - void alloc(u32 bucket_size, u32 initial_bucket_count) { + void alloc(u32 bucket_size = 16, u32 initial_bucket_count = 8) { this->free_list.alloc(); this->bucket_size = bucket_size; next_index_in_latest_bucket = 0; @@ -54,8 +54,8 @@ public: free(buckets); } - template - void for_each(proc p) { + template + void for_each(lambda p) { free_list.sort(); type* val; for (u32 i = 0; i < next_bucket_index; ++i) { @@ -76,8 +76,8 @@ public: type* ret; if (amount == 0) return nullptr; if (amount == 1) { - if (free_list.next_index != 0) { - return free_list.data[--free_list.next_index]; + if (free_list.count != 0) { + return free_list.data[--free_list.count]; } ret = buckets[next_bucket_index]+next_index_in_latest_bucket; increment_pointers(1); diff --git a/build.bat b/build.bat index 53b0f88..f61f2f2 100644 --- a/build.bat +++ b/build.bat @@ -23,7 +23,7 @@ g++ -std=c++17 %SRC% -o %BINDIR_WIN%\g++_%EXE_WIN% echo. echo cl: -cl %SRC% /nologo /Zi /Fd: %BINDIR_WIN%\cl_%EXE_WIN%.pdb /Fo: %BINDIR_WIN%\ /Fe: %BINDIR_WIN%\cl_%EXE_WIN% /wd4090 +cl %SRC% /std:c++latest /nologo /Zi /Fd: %BINDIR_WIN%\cl_%EXE_WIN%.pdb /Fo: %BINDIR_WIN%\ /Fe: %BINDIR_WIN%\cl_%EXE_WIN% /wd4090 %BINDIR_WIN%\cl_%EXE_WIN% echo. diff --git a/hashmap.hpp b/hashmap.hpp index afe1f96..39c04b2 100644 --- a/hashmap.hpp +++ b/hashmap.hpp @@ -7,6 +7,15 @@ #include "arraylist.hpp" +u32 hm_hash(const char* str) { + u32 value = str[0] << 7; + s32 i = 0; + while (str[i]) { + value = (10000003 * value) ^ str[i++]; + } + return value ^ i; +} + u32 hm_hash(char* str) { u32 value = str[0] << 7; s32 i = 0; @@ -20,6 +29,12 @@ u32 hm_hash(void* ptr) { return ((u64)ptr * 2654435761) % 4294967296; } + +inline bool hm_objects_match(const char* a, const char* b) { + return strcmp(a, b) == 0; +} + + inline bool hm_objects_match(char* a, char* b) { return strcmp(a, b) == 0; } @@ -150,6 +165,18 @@ struct Hash_Map { return get_object(key, hm_hash((key_type)key)); } + value_type* get_object_ptr(key_type key, u64 hash_val) { + s32 index = get_index_of_living_cell_if_it_exists(key, hash_val); + if (index != -1) { + return &(data[index].object); + } + return 0; + } + + value_type* get_object_ptr(key_type key) { + return get_object_ptr(key, hm_hash((key_type)key)); + } + void delete_object(key_type key) { s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)); diff --git a/test.cpp b/test.cpp index f5f3c21..7efae1c 100644 --- a/test.cpp +++ b/test.cpp @@ -111,9 +111,6 @@ auto test_hm() -> void { assert(h2.get_object({.x = 1, .y = 2, .z = 3}) == 1, "value should be correct"); assert(h2.get_object({.x = 3, .y = 3, .z = 3}) == 3, "value should be correct"); - - - } s32 main(s32 argc, char* argv[]) { @@ -121,7 +118,7 @@ s32 main(s32 argc, char* argv[]) { test_hm(); - + print("done."); return 0; }