31#define SHT_MAX_ESIZE 16384
54extern void (*sht_abort_print)(
const char *msg);
104 void *restrict context);
138 const void *restrict entry,
139 void *restrict context);
163 void *restrict context);
210static_assert(
sizeof(
enum sht_err) == 1);
228[[gnu::nonnull(1, 2)]]
231 size_t ealign,
enum sht_err *err);
255bool sht_init(
struct sht_ht *ht, uint32_t capacity);
268int sht_add(
struct sht_ht *ht,
const void *key,
const void *entry);
272int sht_set(
struct sht_ht *ht,
const void *key,
const void *entry);
276const void *
sht_get(
struct sht_ht *ht,
const void *restrict key);
280uint32_t
sht_size(
const struct sht_ht *ht);
292bool sht_delete(
struct sht_ht *ht,
const void *restrict key);
296bool sht_pop(
struct sht_ht *ht,
const void *restrict key,
void *restrict out);
300bool sht_replace(
struct sht_ht *ht,
const void *key,
305bool sht_swap(
struct sht_ht *ht,
const void *key,
306 const void *entry,
void *out);
383#define SHT_ARG2(_, a2, ...) (a2)
426#define SHT_NEW(hashfn, eqfn, freefn, etype, ...) \
428 static_assert(sizeof(etype) <= SHT_MAX_ESIZE, \
429 "Entry type (" #etype ") too large"); \
430 sht_new_(hashfn, eqfn, freefn, \
431 sizeof(etype), alignof(etype), \
432 SHT_ARG2(_, ##__VA_ARGS__, nullptr)); \
const void * sht_get(struct sht_ht *ht, const void *restrict key)
Lookup an entry in a table.
@ SHT_ERR_TOOBIG
Requested table size too large.
@ SHT_ERR_ITER_COUNT
Table has too many iterators.
@ SHT_ERR_BAD_ESIZE
Entry size too large (> 16KiB).
@ SHT_ERR_ALLOC
Memory allocation failed.
@ SHT_ERR_BAD_HASH
Too many hash collisions.
@ SHT_ERR_ITER_NO_LAST
Iterator at beginning or end.
@ SHT_ERR_COUNT
(Not an error; used for bounds checks.)
@ SHT_ERR_ITER_LOCK
Can't acquire iterator lock.
bool sht_delete(struct sht_ht *ht, const void *restrict key)
Remove an entry from the table.
bool sht_replace(struct sht_ht *ht, const void *key, const void *entry)
Replace the entry associated with an existing key.
void(* sht_freefn_t)(const void *restrict entry, void *restrict context)
Free function type.
const char * sht_iter_msg(const struct sht_iter *iter)
Get a description of an iterator's last error.
uint8_t sht_peak_psl(const struct sht_ht *ht)
Get the "peak" probe sequence length (PSL) of a table.
const void * sht_iter_next(struct sht_iter *iter)
Get the next entry from an iterator.
int sht_set(struct sht_ht *ht, const void *key, const void *entry)
Unconditionally set the value associated with a key.
void sht_set_hash_ctx(struct sht_ht *ht, void *context)
Set the "context" for a table's hash function.
bool sht_init(struct sht_ht *ht, uint32_t capacity)
Initialize a hash table.
int sht_add(struct sht_ht *ht, const void *key, const void *entry)
Add an entry to the table, if its key is not already present.
uint32_t(* sht_hashfn_t)(const void *restrict key, void *restrict context)
Hash function type.
bool sht_iter_replace(struct sht_iter *iter, const void *restrict entry)
Replace the last entry returned by an iterator.
bool sht_empty(const struct sht_ht *ht)
Determine whether a table is empty.
bool sht_swap(struct sht_ht *ht, const void *key, const void *entry, void *out)
Exchange an existing entry and a new entry.
bool sht_pop(struct sht_ht *ht, const void *restrict key, void *restrict out)
Remove and return an entry from the table.
uint32_t sht_size(const struct sht_ht *ht)
Get the number of entries in a table.
struct sht_iter * sht_iter_new(struct sht_ht *ht, enum sht_iter_type type)
Create a new iterator.
bool sht_iter_delete(struct sht_iter *iter)
Remove the last entry returned by a read/write iterator.
enum sht_err sht_get_err(const struct sht_ht *ht)
Get the error code of a table's last error.
sht_iter_type
Iterator types.
@ SHT_ITER_RO
Read-only iterator.
@ SHT_ITER_RW
Read/write iterator.
void sht_set_lft(struct sht_ht *ht, uint8_t lft)
Set the load factor threshold for a table.
void sht_set_eq_ctx(struct sht_ht *ht, void *context)
Set the "context" for a table's equality function.
void sht_set_psl_limit(struct sht_ht *ht, uint8_t limit)
Set the PSL limit of a table.
const char * sht_msg(enum sht_err err)
Get the description for an error code.
const char * sht_get_msg(const struct sht_ht *ht)
Get a description of a table's last error.
void sht_set_free_ctx(struct sht_ht *ht, void *context)
Set the "context" for a table's free function.
enum sht_err sht_iter_err(const struct sht_iter *iter)
Get the error code of an iterator's last error.
void sht_iter_free(struct sht_iter *iter)
Free an iterator.
bool(* sht_eqfn_t)(const void *restrict key, const void *restrict entry, void *restrict context)
Equality comparison function type.
struct sht_ht * sht_new_(sht_hashfn_t hashfn, sht_eqfn_t eqfn, sht_freefn_t freefn, size_t esize, size_t ealign, enum sht_err *err)
Create a new hash table (call via SHT_NEW()).
void sht_free(struct sht_ht *ht)
Free the resources used by a hash table.