C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
hash_table.h
Go to the documentation of this file.
1/**
2 * @file hash_table.h
3 * @brief Open-addressing string→int hash table with linear probing.
4 *
5 * Open addressing was chosen over separate chaining for cache locality
6 * and zero per-entry malloc churn. Linear probing in particular keeps
7 * the math simple; the load factor is capped at 0.75 with auto-resize.
8 *
9 * Hash function: djb2 (k=33) — fast, good enough for non-adversarial data.
10 *
11 * | Operation | Avg | Worst |
12 * |-----------------|---------|---------|
13 * | get | O(1) | O(n) |
14 * | set | O(1) | O(n) |
15 * | remove | O(1) | O(n) |
16 * | resize (rehash) | O(n) | (amortized into set) |
17 */
18
19#ifndef HASH_TABLE_H
20#define HASH_TABLE_H
21
22#include <stdbool.h>
23#include <stddef.h>
24
25typedef struct hash_table hash_table_t;
26
28void ht_destroy(hash_table_t *ht);
29
30/** Inserts or updates `key` → `value`. Returns false on allocation failure. */
31bool ht_set(hash_table_t *ht, const char *key, int value);
32
33/** Writes `*out` and returns true if found, otherwise false. */
34bool ht_get(const hash_table_t *ht, const char *key, int *out);
35
36bool ht_remove(hash_table_t *ht, const char *key);
37bool ht_contains(const hash_table_t *ht, const char *key);
38
39size_t ht_size(const hash_table_t *ht);
40size_t ht_capacity(const hash_table_t *ht);
41
42#endif /* HASH_TABLE_H */
bool ht_contains(const hash_table_t *ht, const char *key)
Definition hash_table.c:118
size_t ht_capacity(const hash_table_t *ht)
Definition hash_table.c:123
bool ht_set(hash_table_t *ht, const char *key, int value)
Definition hash_table.c:77
bool ht_remove(hash_table_t *ht, const char *key)
Definition hash_table.c:107
hash_table_t * ht_create(void)
Definition hash_table.c:40
bool ht_get(const hash_table_t *ht, const char *key, int *out)
Definition hash_table.c:99
size_t ht_size(const hash_table_t *ht)
Definition hash_table.c:122
void ht_destroy(hash_table_t *ht)
Definition hash_table.c:50