tlibc/include/tlibc/collections/HashMap.h

37 lines
1019 B
C
Executable File

#pragma once
#include "../std.h"
#include "../string/str.h"
#include "Array.h"
#include "List.h"
typedef void (*FreeFunction)(void*);
typedef struct KeyHash {
str key;
u32 hash;
} KeyHash;
typedef struct HashMapBucket {
List(KeyHash) key_hash_list;
List(T) value_list;
} HashMapBucket;
#define HashMap(T) HashMap_
typedef struct HashMap_ {
HashMapBucket* table;
FreeFunction NULLABLE(value_destructor);
u32 value_t_size;
u32 height;
u16 height_n;
} HashMap_;
#define HashMap_construct(PTR, T, VALUE_DESTRUCTOR) HashMap_construct_size(PTR, sizeof(T), VALUE_DESTRUCTOR)
void HashMap_construct_size(HashMap_* ptr, u32 value_t_size, FreeFunction NULLABLE(value_destructor));
void HashMap_destroy(HashMap_* ptr);
bool HashMap_tryPush(HashMap_* ptr, const str key, void* value_ptr);
void HashMap_pushOrUpdate(HashMap_* ptr, const str key, void* value_ptr);
NULLABLE(void*) HashMap_tryGetPtr(const HashMap_* ptr, const str key);
bool HashMap_tryDelete(HashMap_* ptr, const str key);