mccu/include/mcutil/collect/hash_map.h

34 lines
1.2 KiB
C
Raw Normal View History

2025-01-28 21:42:26 +00:00
#ifndef _H_MCU_COLLECT_HASH_MAP
#define _H_MCU_COLLECT_HASH_MAP
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
2025-01-29 21:21:18 +00:00
#include "mcutil/mcutil.h"
2025-01-28 21:42:26 +00:00
typedef struct mcu_hash_map_node_s {
uint8_t key[16];
void* value;
struct mcu_hash_map_node_s* next;
} mcu_hash_map_node_t;
typedef struct mcu_hash_map_s {
mcu_hash_map_node_t* nodes;
} mcu_hash_map_t;
#define mcu_hm_insert(hm, key, value) _mcu_hm_insert((hm), (key), (const size_t)sizeof(key), (value));
#define mcu_hm_get(hm, key) _mcu_hm_insert((hm), (key), (const size_t)sizeof(key));
#define mcu_hm_remove(hm, key) _mcu_hm_insert((hm), (key), (const size_t)sizeof(key));
MCU_API mcu_hash_map_t mcu_hm_new();
MCU_API void _mcu_hm_insert(mcu_hash_map_t* hm, const void* key, const size_t key_len, const void* value);
MCU_API void* _mcu_hm_get (mcu_hash_map_t* hm, const void* key, const size_t key_len);
MCU_API void* _mcu_hm_remove(mcu_hash_map_t* hm, const void* key, const size_t key_len);
/// Frees the hashmap but not the pointers inside the values
MCU_API void mcu_free(mcu_hash_map_t* hm);
/// Frees the hashmap and the pointers inside the values
MCU_API void mcu_free_all(mcu_hash_map_t* hm);
#endif // _H_MCU_COLLECT_HASH_MAP