| @@ -35,7 +35,7 @@ class Bucket_Allocator { | |||||
| } | } | ||||
| public: | 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->free_list.alloc(); | ||||
| this->bucket_size = bucket_size; | this->bucket_size = bucket_size; | ||||
| next_index_in_latest_bucket = 0; | next_index_in_latest_bucket = 0; | ||||
| @@ -54,8 +54,8 @@ public: | |||||
| free(buckets); | free(buckets); | ||||
| } | } | ||||
| template <typename proc> | |||||
| void for_each(proc p) { | |||||
| template <typename lambda> | |||||
| void for_each(lambda p) { | |||||
| free_list.sort(); | free_list.sort(); | ||||
| type* val; | type* val; | ||||
| for (u32 i = 0; i < next_bucket_index; ++i) { | for (u32 i = 0; i < next_bucket_index; ++i) { | ||||
| @@ -76,8 +76,8 @@ public: | |||||
| type* ret; | type* ret; | ||||
| if (amount == 0) return nullptr; | if (amount == 0) return nullptr; | ||||
| if (amount == 1) { | 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; | ret = buckets[next_bucket_index]+next_index_in_latest_bucket; | ||||
| increment_pointers(1); | increment_pointers(1); | ||||
| @@ -23,7 +23,7 @@ g++ -std=c++17 %SRC% -o %BINDIR_WIN%\g++_%EXE_WIN% | |||||
| echo. | echo. | ||||
| echo cl: | 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% | %BINDIR_WIN%\cl_%EXE_WIN% | ||||
| echo. | echo. | ||||
| @@ -7,6 +7,15 @@ | |||||
| #include "arraylist.hpp" | #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 hm_hash(char* str) { | ||||
| u32 value = str[0] << 7; | u32 value = str[0] << 7; | ||||
| s32 i = 0; | s32 i = 0; | ||||
| @@ -20,6 +29,12 @@ u32 hm_hash(void* ptr) { | |||||
| return ((u64)ptr * 2654435761) % 4294967296; | 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) { | inline bool hm_objects_match(char* a, char* b) { | ||||
| return strcmp(a, b) == 0; | return strcmp(a, b) == 0; | ||||
| } | } | ||||
| @@ -150,6 +165,18 @@ struct Hash_Map { | |||||
| return get_object(key, hm_hash((key_type)key)); | 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) { | void delete_object(key_type key) { | ||||
| s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)); | s32 index = get_index_of_living_cell_if_it_exists(key, hm_hash((key_type)key)); | ||||
| @@ -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 = 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"); | assert(h2.get_object({.x = 3, .y = 3, .z = 3}) == 3, "value should be correct"); | ||||
| } | } | ||||
| s32 main(s32 argc, char* argv[]) { | s32 main(s32 argc, char* argv[]) { | ||||
| @@ -121,7 +118,7 @@ s32 main(s32 argc, char* argv[]) { | |||||
| test_hm(); | test_hm(); | ||||
| print("done."); | |||||
| return 0; | return 0; | ||||
| } | } | ||||