From f10b74763e0323319d9ec362323b0359e83f278a Mon Sep 17 00:00:00 2001 From: rhuibertsjr Date: Tue, 4 Jun 2024 11:27:18 +0200 Subject: [PATCH] Added: implementation for conflict resolution --- docs/CHANGELOG | 7 ----- src/base.h | 18 ++++++++++++ src/hash-store.c | 58 ------------------------------------- src/hash-store.h | 21 -------------- src/hash_store.c | 69 ++++++++++++++++++++++++++++++++++++++++++++ src/hash_store.h | 39 +++++++++++++++++++++++++ src/main.c | 28 ++++++++---------- src/string.h | 1 + src/win32_platform.c | 34 ++++++++++++++++++++++ src/win32_platform.h | 17 +++++++++++ 10 files changed, 190 insertions(+), 102 deletions(-) delete mode 100644 docs/CHANGELOG delete mode 100644 src/hash-store.c delete mode 100644 src/hash-store.h create mode 100644 src/hash_store.c create mode 100644 src/hash_store.h create mode 100644 src/win32_platform.c create mode 100644 src/win32_platform.h diff --git a/docs/CHANGELOG b/docs/CHANGELOG deleted file mode 100644 index 503b74d..0000000 --- a/docs/CHANGELOG +++ /dev/null @@ -1,7 +0,0 @@ -12-04-2024 René Huiberts - - Updated: references & documentation - -12-04-2024 René Huiberts - - Added: project layout diff --git a/src/base.h b/src/base.h index 780f0bc..fe22ad5 100644 --- a/src/base.h +++ b/src/base.h @@ -18,12 +18,30 @@ typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; +typedef uint8_t b8; +typedef uint16_t b16; +typedef uint32_t b32; +typedef uint64_t b64; + #define KB(b) ((b) << 10) #define MB(b) ((b) << 20) #define STATEMENT(S) do{ S } while(0) #define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 0); } ) +#define STATIC_ASSERT(c,l) typedef u8 Glue(l,__LINE__) [(c)?1:-1] #define MIN(A,B) (((A)<(B))?(A):(B)) +//= rhjr: linked-list + +#define check_null(p) ((p)==0) +#define set_null(p) ((p)=0) + +#define queue_push_nz(f,l,n,next,zchk,zset) (zchk(f)?\ +(((f)=(l)=(n)), zset((n)->next)):\ +((l)->next=(n),(l)=(n),zset((n)->next))) + +#define queue_push(f,l,n) queue_push_nz(f,l,n,next,check_null,set_null) +#define stack_push(f,n,next) ((n)->next=(f), (f)=(n)) + #endif // BASE_H diff --git a/src/hash-store.c b/src/hash-store.c deleted file mode 100644 index 89ade06..0000000 --- a/src/hash-store.c +++ /dev/null @@ -1,58 +0,0 @@ - -internal Table -hash_store_initialize(void) -{ - Table result = {0}; - - result.ptr_arena = arena_initialize(KB(1)); - result.str8_arena = arena_initialize(KB(1)); - - result.str8_ptr = arena_push(result.ptr_arena, String8); - result.str8_count = 1; - result.str8_ptr[0] = str8_lit(""); - - return result; -} - -internal Key -hash_store_string8_to_key(Table *table, String8 value) -{ - Key result = 0; - u32 count = table->str8_count; - result = count; - - String8 *str8_ptr = table->str8_ptr; - for (u32 index = 0; index < count; index += 1, str8_ptr += 1) - { - if (string8_match(*str8_ptr, value)) - { - result = index; - break; - } - } - - if (result == count) - { - String8 *new_str8 = arena_push(table->ptr_arena, String8); - new_str8->content = - arena_push_array(table->str8_arena, u8, value.length); - - memory_copy(new_str8->content, value.content, value.length); - table->str8_count += 1; - } - - return result; -} - -internal String8 -hash_store_string8_from_key(Table *table, Key index) -{ - String8 result = {0}; - - if (index < table->str8_count) - { - result = table->str8_ptr[index]; - } - - return result; -} diff --git a/src/hash-store.h b/src/hash-store.h deleted file mode 100644 index a3064d2..0000000 --- a/src/hash-store.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef HASH_TABLE_H -#define HASH_TABLE_H - -typedef u32 Key; - -typedef struct Table Table; -struct Table -{ - Arena *ptr_arena; - Arena *str8_arena; - - String8 *str8_ptr; - u32 str8_count; -}; - -internal Table hash_store_initialize(void); - -internal String8 hash_store_string8_from_key(Table *table, Key index); -internal Key hash_store_string8_to_key(Table *table, String8 value); - -#endif diff --git a/src/hash_store.c b/src/hash_store.c new file mode 100644 index 0000000..49eddb3 --- /dev/null +++ b/src/hash_store.c @@ -0,0 +1,69 @@ + +internal Key +hash_store_hash(String8 data) +{ + Key result = {0}; + + u64 hash = 5381; + for(u64 i = 0; i < data.length; i += 1) + { + hash = ((hash << 5) + hash) + data.content[i]; + } + + memory_copy(&result, &hash, sizeof(u64)); + return result; +} + +internal Table * +hash_store_initialize(void) +{ + Arena *arena = arena_initialize_default(); + Arena *str8_arena = arena_initialize_default(); + + Table *table = arena_push(arena, Table); + table->arena = arena; + table->str8_arena = str8_arena; + table->slots_count = 200; + + table->slots = arena_push_array(arena, TableSlot, table->slots_count); + + return table; +} + +internal Key +hash_store_string8_to_key(Table *table, String8 data) +{ + Key result = hash_store_hash(data); + u64 slot_idx = result.U32[0]%table->slots_count; + + TableNode *node = arena_push(table->arena, TableNode); + node->data.content = arena_push_array(table->str8_arena, char, data.length); + node->data.length = data.length; + node->key = result; + + memory_copy(&node->data.content, &data.content, data.length); + + TableSlot *slot = &table->slots[slot_idx]; + queue_push(slot->first, slot->last, node); + + return result; +} + +internal String8 +hash_store_string8_from_key(Table *table, Key key) +{ + String8 result = {0}; + u64 slot_idx = key.U32[0]%table->slots_count; + TableSlot *slot = &table->slots[slot_idx]; + + for (TableNode *n = slot->first; n != 0; n = n->next) + { + if (key.U32[1] == n->key.U32[1]) + { + result = n->data; + break; + } + } + + return result; +} diff --git a/src/hash_store.h b/src/hash_store.h new file mode 100644 index 0000000..d4bbf46 --- /dev/null +++ b/src/hash_store.h @@ -0,0 +1,39 @@ +#ifndef HASH_TABLE_H +#define HASH_TABLE_H + +typedef struct Key Key; +struct Key +{ + u32 U32[2]; +}; + +typedef struct TableNode TableNode; +struct TableNode +{ + TableNode *next; + String8 data; + Key key; +}; + +typedef struct TableSlot TableSlot; +struct TableSlot +{ + TableNode *first; + TableNode *last; +}; + +typedef struct Table Table; +struct Table +{ + Arena *arena; + Arena *str8_arena; + + TableSlot *slots; + u64 slots_count; +}; + +internal Table * hash_store_initialize(void); + +internal Key hash_store_string8_to_key(Table *table, String8 data); + +#endif diff --git a/src/main.c b/src/main.c index f6a385c..32fe950 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,28 @@ #include "base.h" - +#include "win32_platform.h" #include "arena.h" #include "string.h" -#include "hash-store.h" +#include "hash_store.h" +#include "win32_platform.c" #include "arena.c" #include "string.c" -#include "hash-store.c" +#include "hash_store.c" int main(void) { - Table store = hash_store_initialize(); - Key a = hash_store_string8_to_key(&store, str8_lit("Hello")); - Key b = hash_store_string8_to_key(&store, str8_lit("Beautiful")); - Key c = hash_store_string8_to_key(&store, str8_lit("World")); + Table *table = hash_store_initialize(); - String8 str_a = hash_store_string8_from_key(&store, a); - String8 str_b = hash_store_string8_from_key(&store, b); - String8 str_c = hash_store_string8_from_key(&store, c); + String8 str8_1 = str8_lit("Hello world"); + String8 str8_2 = str8_lit("ayenooo"); - ASSERT(hash_store_string8_to_key(&store, str8_lit("Hello")) == a); - ASSERT(hash_store_string8_to_key(&store, str8_lit("Beautiful")) == b); - ASSERT(hash_store_string8_to_key(&store, str8_lit("World")) == c); + Key key1 = hash_store_string8_to_key(table, str8_1); + Key key2 = hash_store_string8_to_key(table, str8_2); + Key key3 = hash_store_string8_to_key(table, str8_1); - ASSERT(string8_match(hash_store_string8_from_key(&store, a), str8_lit("Hello"))); - ASSERT(string8_match(hash_store_string8_from_key(&store, b), str8_lit("Beautiful"))); - ASSERT(string8_match(hash_store_string8_from_key(&store, c), str8_lit("World"))); + String8 string = hash_store_string8_from_key(table, key2); + printf("%.*s\n", (i32) string.length, string.content); return 0; }; diff --git a/src/string.h b/src/string.h index 2435158..a897f6b 100644 --- a/src/string.h +++ b/src/string.h @@ -11,6 +11,7 @@ struct String8 internal String8 str8_init(u8 *cstring, u64 length); #define str8_lit(cstring) \ str8_init((u8*)(cstring), sizeof(cstring) - 1) +#define str8_lit_comp(cstring) {(u8*)(cstring), sizeof(cstring) - 1} internal u8 string8_match(String8 a, String8 b); diff --git a/src/win32_platform.c b/src/win32_platform.c new file mode 100644 index 0000000..bf0f9eb --- /dev/null +++ b/src/win32_platform.c @@ -0,0 +1,34 @@ + +internal void * +platform_memory_reserve(u64 size) +{ + void *result = VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE); + return result; +} + +internal b8 +platform_memory_commit(void *ptr, u64 size) +{ + b32 result = (VirtualAlloc(ptr, size, MEM_COMMIT, PAGE_READWRITE) != 0); + return result; +} + +internal void +platform_memory_decommit(void *ptr, u64 size) +{ + VirtualFree(ptr, size, MEM_DECOMMIT); +}; + +internal void +platform_memory_release(void *ptr, u64 size) +{ + VirtualFree(ptr, 0, MEM_RELEASE); +} + +internal u64 +platform_get_page_size(void) +{ + SYSTEM_INFO sysinfo = {0}; + GetSystemInfo(&sysinfo); + return sysinfo.dwPageSize; +} diff --git a/src/win32_platform.h b/src/win32_platform.h new file mode 100644 index 0000000..63e279a --- /dev/null +++ b/src/win32_platform.h @@ -0,0 +1,17 @@ +#ifndef WIN32_PLATFORM_H +#define WIN32_PLATFORM_H + +#undef internal +#include +#define internal static + +//= rhjr: platform memory management + +internal void * platform_memory_reserve(u64 size); +internal b8 platform_memory_commit(void *ptr, u64 size); +internal void platform_memory_decommit(void *ptr, u64 size); +internal void platform_memory_release(void *ptr, u64 size); + +internal u64 platform_get_page_size(void); + +#endif // WIN32_PLATFORM_H