57 lines
1.5 KiB
C
Executable File
57 lines
1.5 KiB
C
Executable File
#pragma once
|
|
#include "../std.h"
|
|
#include "../string/str.h"
|
|
#include "Array.h"
|
|
#include "List.h"
|
|
|
|
typedef void (*FreeFunction)(void*);
|
|
|
|
typedef struct HashMapKeyHash {
|
|
str key;
|
|
u32 hash;
|
|
} HashMapKeyHash;
|
|
|
|
typedef struct HashMapBucket {
|
|
List(HashMapKeyHash) key_hash_list;
|
|
List(T) value_list;
|
|
} HashMapBucket;
|
|
|
|
#define HashMap(T) HashMap_
|
|
|
|
typedef struct HashMap_ {
|
|
NULLABLE(HashMapBucket*) table;
|
|
NULLABLE(FreeFunction) value_destructor;
|
|
u32 value_t_size;
|
|
u32 height;
|
|
u16 height_n;
|
|
} HashMap_;
|
|
|
|
#define HashMap_construct(SELF, T, VALUE_DESTRUCTOR) HashMap_construct_size(SELF, sizeof(T), VALUE_DESTRUCTOR)
|
|
void HashMap_construct_size(HashMap_* self, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
|
|
void HashMap_destroy(HashMap_* self);
|
|
|
|
bool HashMap_tryPush(HashMap_* self, const str key, void* value_ptr);
|
|
void HashMap_pushOrUpdate(HashMap_* self, const str key, void* value_ptr);
|
|
NULLABLE(void*) HashMap_tryGetPtr(const HashMap_* self, const str key);
|
|
bool HashMap_tryDelete(HashMap_* self, const str key);
|
|
|
|
|
|
typedef struct {
|
|
const HashMap_* map;
|
|
i32 bucket_n;
|
|
i32 elem_n;
|
|
} HashMapIter;
|
|
|
|
typedef struct HashMapKeyValue {
|
|
str key;
|
|
void* value;
|
|
} HashMapKeyValue;
|
|
|
|
static inline HashMapIter HashMapIter_create(const HashMap_* table){
|
|
return (HashMapIter){ .map = table, .bucket_n = -1, .elem_n = -1 };
|
|
}
|
|
|
|
bool HashMapIter_moveNext(HashMapIter* self);
|
|
/// don't forget to call HashMapIter_moveNext() first
|
|
bool HashMapIter_getCurrent(HashMapIter* self, HashMapKeyValue* kv);
|