libcute 0.1
Loading...
Searching...
No Matches
hashmap.h
Go to the documentation of this file.
1#pragma once
2
5#include "hash/hash.h"
6#include "macro.h"
7#include "memory/allocator.h"
8#include "object/optional.h"
9#include "object/result.h"
10#include "state.h"
11#include "utility.h"
12#include <stdbool.h>
13#include <stddef.h>
14
16typedef struct {
17 bool used;
18 bool deleted;
19 uint64_t hash;
20 void *key;
21 void *value;
23
24typedef uint64_t (*cu_HashMap_HashFn)(const void *key, size_t key_size);
25typedef bool (*cu_HashMap_EqualsFn)(
26 const void *a, const void *b, size_t key_size);
27CU_OPTIONAL_DECL(cu_HashMap_HashFn, cu_HashMap_HashFn)
28CU_OPTIONAL_DECL(cu_HashMap_EqualsFn, cu_HashMap_EqualsFn)
29
30
31typedef struct {
33 size_t capacity;
34 size_t length;
38 cu_HashMap_HashFn hash_fn;
39 cu_HashMap_EqualsFn equals_fn;
40 uint32_t seed;
42
44typedef enum {
45 CU_HASHMAP_ERROR_NONE = 0,
46 CU_HASHMAP_ERROR_OOM,
47 CU_HASHMAP_ERROR_INVALID_LAYOUT,
48 CU_HASHMAP_ERROR_INVALID,
50
53
54
66cu_HashMap_Result cu_HashMap_create(cu_Allocator allocator,
67 cu_Layout key_layout, cu_Layout value_layout,
68 Size_Optional initial_capacity, cu_HashMap_HashFn_Optional hash_fn,
69 cu_HashMap_EqualsFn_Optional equals_fn, cu_State state);
72
76cu_HashMap_Error_Optional cu_HashMap_insert(
77 cu_HashMap *map, void *key, void *value);
81Ptr_Optional cu_HashMap_get(const cu_HashMap *map, const void *key);
86 const cu_HashMap *map, size_t *index, void **key, void **value);
void cu_HashMap_destroy(cu_HashMap *map)
Definition hashmap.c:153
Ptr_Optional cu_HashMap_get(const cu_HashMap *map, const void *key)
Retrieve the value stored for key.
Definition hashmap.c:224
bool cu_HashMap_iter(const cu_HashMap *map, size_t *index, void **key, void **value)
Iterate over all stored pairs.
Definition hashmap.c:238
cu_HashMap_Error
Definition hashmap.h:44
cu_HashMap_Error_Optional cu_HashMap_insert(cu_HashMap *map, void *key, void *value)
Insert a new key-value pair.
Definition hashmap.c:173
cu_HashMap_Result cu_HashMap_create(cu_Allocator allocator, cu_Layout key_layout, cu_Layout value_layout, Size_Optional initial_capacity, cu_HashMap_HashFn_Optional hash_fn, cu_HashMap_EqualsFn_Optional equals_fn, cu_State state)
Create a new hashmap.
Definition hashmap.c:109
#define CU_OPTIONAL_DECL(NAME, T)
Definition optional.h:25
#define CU_RESULT_DECL(NAME, T, E)
Definition result.h:23
Definition allocator.h:24
Definition hashmap.h:16
void * key
Definition hashmap.h:20
void * value
Definition hashmap.h:21
bool used
Definition hashmap.h:17
bool deleted
Definition hashmap.h:18
uint64_t hash
Definition hashmap.h:19
Definition hashmap.h:31
cu_HashMap_HashFn hash_fn
Definition hashmap.h:38
cu_HashMap_EqualsFn equals_fn
Definition hashmap.h:39
size_t capacity
Definition hashmap.h:33
uint32_t seed
Definition hashmap.h:40
size_t length
Definition hashmap.h:34
cu_Layout value_layout
Definition hashmap.h:36
cu_Layout key_layout
Definition hashmap.h:35
cu_Allocator allocator
Definition hashmap.h:37
cu_HashMap_Bucket * buckets
Definition hashmap.h:32
Definition utility.h:22
Definition state.h:15