aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-03-07 22:19:57 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit27718f093689dbd3decd7021eaa97327f578c8f3 (patch)
treec41ab4bb5727b22be35c1237279cfdfec0a27561 /include
parent81b6004928015ced29b0b949e35753977aa17606 (diff)
Add hash map
Diffstat (limited to 'include')
-rw-r--r--include/binop_type.h2
-rw-r--r--include/std/buffer.h3
-rw-r--r--include/std/defs.h3
-rw-r--r--include/std/hash.h8
-rw-r--r--include/std/hash_map.h38
-rw-r--r--include/std/mem.h1
-rw-r--r--include/std/scoped_allocator.h4
-rw-r--r--include/std/thread.h1
8 files changed, 55 insertions, 5 deletions
diff --git a/include/binop_type.h b/include/binop_type.h
index d04f9d7..e747102 100644
--- a/include/binop_type.h
+++ b/include/binop_type.h
@@ -9,4 +9,4 @@ typedef enum {
BINOP_DOT
} BinopType;
-#endif \ No newline at end of file
+#endif
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);