Zephyr API Documentation  3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
hash_map_api.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_HASHMAP_API_H_
8#define ZEPHYR_INCLUDE_SYS_HASHMAP_API_H_
9
10#include <stdbool.h>
11#include <stddef.h>
12#include <stdint.h>
13
15#include <zephyr/sys/util.h>
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
46 const struct sys_hashmap *map;
48 void (*next)(struct sys_hashmap_iterator *it);
50 void *state;
56 const size_t size;
58 size_t pos;
59};
60
68static inline bool sys_hashmap_iterator_has_next(const struct sys_hashmap_iterator *it)
69{
70 return it->pos < it->size;
71}
72
84typedef void *(*sys_hashmap_allocator_t)(void *ptr, size_t new_size);
85
94typedef void (*sys_hashmap_iterator_t)(const struct sys_hashmap *map,
95 struct sys_hashmap_iterator *it);
96
106typedef void (*sys_hashmap_callback_t)(uint64_t key, uint64_t value, void *cookie);
107
118 void *cookie);
119
134typedef int (*sys_hashmap_insert_t)(struct sys_hashmap *map, uint64_t key, uint64_t value,
135 uint64_t *old_value);
136
149typedef bool (*sys_hashmap_remove_t)(struct sys_hashmap *map, uint64_t key, uint64_t *value);
150
163typedef bool (*sys_hashmap_get_t)(const struct sys_hashmap *map, uint64_t key, uint64_t *value);
164
179};
180
199 size_t max_size;
204};
205
214#define SYS_HASHMAP_CONFIG(_max_size, _load_factor) \
215 { \
216 .max_size = (size_t)_max_size, .load_factor = (uint8_t)_load_factor, \
217 .initial_n_buckets = NHPOT(DIV_ROUND_UP(100, _load_factor)), \
218 }
219
227 void *buckets;
229 size_t n_buckets;
231 size_t size;
232};
233
236#ifdef __cplusplus
237}
238#endif
239
240#endif /* ZEPHYR_INCLUDE_SYS_HASHMAP_API_H_ */
static bool sys_hashmap_iterator_has_next(const struct sys_hashmap_iterator *it)
Check if a Hashmap iterator has a next entry.
Definition: hash_map_api.h:68
void(* sys_hashmap_callback_t)(uint64_t key, uint64_t value, void *cookie)
Callback interface for sys_hashmap.
Definition: hash_map_api.h:106
bool(* sys_hashmap_remove_t)(struct sys_hashmap *map, uint64_t key, uint64_t *value)
Remove an entry from a sys_hashmap.
Definition: hash_map_api.h:149
bool(* sys_hashmap_get_t)(const struct sys_hashmap *map, uint64_t key, uint64_t *value)
Get a value from a sys_hashmap.
Definition: hash_map_api.h:163
void(* sys_hashmap_clear_t)(struct sys_hashmap *map, sys_hashmap_callback_t cb, void *cookie)
Clear all entries contained in a sys_hashmap.
Definition: hash_map_api.h:117
int(* sys_hashmap_insert_t)(struct sys_hashmap *map, uint64_t key, uint64_t value, uint64_t *old_value)
Insert a new entry into a sys_hashmap.
Definition: hash_map_api.h:134
void(* sys_hashmap_iterator_t)(const struct sys_hashmap *map, struct sys_hashmap_iterator *it)
In-place iterator constructor for sys_hashmap.
Definition: hash_map_api.h:94
#define bool
Definition: stdbool.h:13
__UINT64_TYPE__ uint64_t
Definition: stdint.h:91
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Generic Hashmap API.
Definition: hash_map_api.h:168
sys_hashmap_iterator_t iter
Iterator constructor (in-place)
Definition: hash_map_api.h:170
sys_hashmap_remove_t remove
Remove a key-value pair from the Hashmap.
Definition: hash_map_api.h:176
sys_hashmap_clear_t clear
Clear the hash table, freeing all resources.
Definition: hash_map_api.h:172
sys_hashmap_insert_t insert
Insert a key-value pair into the Hashmap.
Definition: hash_map_api.h:174
sys_hashmap_get_t get
Retrieve the value associated with a given key from the Hashmap.
Definition: hash_map_api.h:178
Generic Hashmap configuration.
Definition: hash_map_api.h:197
uint8_t initial_n_buckets
Initial number of buckets to allocate.
Definition: hash_map_api.h:203
uint8_t load_factor
Maximum load factor expressed in hundredths.
Definition: hash_map_api.h:201
size_t max_size
Maximum number of entries.
Definition: hash_map_api.h:199
Generic Hashmap data.
Definition: hash_map_api.h:225
size_t n_buckets
The number of buckets currently allocated.
Definition: hash_map_api.h:229
size_t size
The number of entries currently in the Hashmap.
Definition: hash_map_api.h:231
void * buckets
Pointer for implementation-specific Hashmap storage.
Definition: hash_map_api.h:227
Generic Hashmap iterator interface.
Definition: hash_map_api.h:44
size_t pos
Number of entries already iterated.
Definition: hash_map_api.h:58
const struct sys_hashmap * map
Pointer to the associated Hashmap.
Definition: hash_map_api.h:46
void * state
Implementation-specific iterator state.
Definition: hash_map_api.h:50
const size_t size
Number of entries in the map.
Definition: hash_map_api.h:56
uint64_t key
Key associated with the current entry.
Definition: hash_map_api.h:52
void(* next)(struct sys_hashmap_iterator *it)
Modify the iterator in-place to point to the next Hashmap entry.
Definition: hash_map_api.h:48
uint64_t value
Value associated with the current entry.
Definition: hash_map_api.h:54
Generic Hashmap.
Definition: hash_map.h:125
Misc utilities.