From 27718f093689dbd3decd7021eaa97327f578c8f3 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 7 Mar 2019 22:19:57 +0100 Subject: Add hash map --- include/std/buffer.h | 3 ++- include/std/defs.h | 3 +++ include/std/hash.h | 8 ++++++++ include/std/hash_map.h | 38 ++++++++++++++++++++++++++++++++++++++ include/std/mem.h | 1 + include/std/scoped_allocator.h | 4 +--- include/std/thread.h | 1 + 7 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 include/std/hash.h create mode 100644 include/std/hash_map.h (limited to 'include/std') diff --git a/include/std/buffer.h b/include/std/buffer.h index a60def9..688f18a 100644 --- a/include/std/buffer.h +++ b/include/std/buffer.h @@ -16,7 +16,8 @@ typedef struct { struct ScopedAllocator; CHECK_RESULT int buffer_init(Buffer *self, struct ScopedAllocator *allocator); -CHECK_RESULT int buffer_append(Buffer *self, void *data, usize size); +/* @data can be NULL */ +CHECK_RESULT int buffer_append(Buffer *self, const void *data, usize size); void* buffer_get(Buffer *self, usize index, usize type_size); CHECK_RESULT int buffer_pop(Buffer *self, void *data, usize size); void* buffer_start(Buffer *self); diff --git a/include/std/defs.h b/include/std/defs.h index 653bf73..ee049d4 100644 --- a/include/std/defs.h +++ b/include/std/defs.h @@ -4,4 +4,7 @@ typedef struct amal_thread amal_thread; typedef struct amal_mutex amal_mutex; +typedef struct ScopedAllocatorNode ScopedAllocatorNode; +typedef struct ScopedAllocator ScopedAllocator; + #endif diff --git a/include/std/hash.h b/include/std/hash.h new file mode 100644 index 0000000..f7a807d --- /dev/null +++ b/include/std/hash.h @@ -0,0 +1,8 @@ +#ifndef AMALGAM_HASH_H +#define AMALGAM_HASH_H + +#include "types.h" + +usize amal_hash_string(const u8 *data, usize size); + +#endif diff --git a/include/std/hash_map.h b/include/std/hash_map.h new file mode 100644 index 0000000..c36ecc4 --- /dev/null +++ b/include/std/hash_map.h @@ -0,0 +1,38 @@ +#ifndef AMALGAM_HASH_MAP_H +#define AMALGAM_HASH_MAP_H + +#include "defs.h" +#include "misc.h" +#include "types.h" +#include "buffer.h" +#include "buffer_view.h" + +typedef struct HashMap HashMap; + +typedef int(*HashMapCompare)(const void *a, const void *b); +typedef usize(*HashMapHash)(const u8 *data, usize size); + +struct HashMap { + ScopedAllocator *allocator; /* borrowed */ + Buffer/*HashMapBucket*/ buckets; + usize type_size; + usize num_elements; + HashMapCompare compare_func; + HashMapHash hash_func; +}; + +CHECK_RESULT int hash_map_init(HashMap *self, ScopedAllocator *allocator, usize type_size, HashMapCompare compare_func, HashMapHash hash_func); +/* +Not thread-safe. +Expected @value size to be @self->type_size. +*/ +CHECK_RESULT int hash_map_insert(HashMap *self, BufferView key, void *value); +/* +Thread-safe unless @hash_map_insert is used in another thread at the same time. +Expected @value size to be @self->type_size. +*/ +CHECK_RESULT bool hash_map_get(HashMap *self, BufferView key, void *value); + +int hash_compare_string(const void *a, const void *b); + +#endif diff --git a/include/std/mem.h b/include/std/mem.h index 8d91a5a..ddd2b31 100644 --- a/include/std/mem.h +++ b/include/std/mem.h @@ -5,6 +5,7 @@ #include "misc.h" void am_memcpy(void *dest, const void *src, usize size); +int am_memcmp(const void *lhs, const void *rhs, usize size); bool am_memeql(const void *lhs, const void *rhs, usize size); void am_memset(void *dest, int value, usize size); diff --git a/include/std/scoped_allocator.h b/include/std/scoped_allocator.h index ad6e2dd..0de4f04 100644 --- a/include/std/scoped_allocator.h +++ b/include/std/scoped_allocator.h @@ -1,13 +1,11 @@ #ifndef AMALGAM_SCOPED_ALLOCATOR_H #define AMALGAM_SCOPED_ALLOCATOR_H +#include "defs.h" #include "misc.h" #include "types.h" #include "buffer.h" -typedef struct ScopedAllocatorNode ScopedAllocatorNode; -typedef struct ScopedAllocator ScopedAllocator; - struct ScopedAllocatorNode { char *data; usize size; diff --git a/include/std/thread.h b/include/std/thread.h index 972ce3a..915d6a9 100644 --- a/include/std/thread.h +++ b/include/std/thread.h @@ -38,6 +38,7 @@ CHECK_RESULT int amal_thread_detach(amal_thread *self); CHECK_RESULT int amal_thread_join(amal_thread *self, void **result); void amal_mutex_init(amal_mutex *self); +void amal_mutex_deinit(amal_mutex *self); CHECK_RESULT int amal_mutex_lock(amal_mutex *self, const char *lock_identifier); CHECK_RESULT int amal_mutex_unlock(amal_mutex *self); void amal_mutex_tryunlock(amal_mutex *self); -- cgit v1.2.3