libsnobol4 1.0.2
High-performance SNOBOL4-style pattern matching library
Loading...
Searching...
No Matches
table.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdint.h>
12
26
27/* Initial hash table capacity */
28#define TABLE_INITIAL_CAPACITY 16
29
30/* Hash table entry */
31typedef struct table_entry {
32 char *key; /* Owned: must be freed (NUL-safe: key_len bytes) */
33 size_t key_len; /* Byte length of key */
34 char *value; /* Owned: must be freed (NULL = tombstone) */
35 size_t value_len; /* Byte length of value (0 when value is NULL) */
36 uint32_t hash; /* Pre-computed hash of key */
37 bool active; /* Entry is active (not deleted) */
39
40/* Runtime table object */
41typedef struct snobol_table {
42 table_entry_t *entries; /* Hash table entries */
43 size_t capacity; /* Total slots in hash table */
44 size_t size; /* Number of active entries */
45 size_t tombstones; /* Number of deleted entries */
46 uint32_t refcount; /* Reference count for ownership */
47 char *name; /* Optional table name (for debugging) */
49
57SNOBOL_NODISCARD snobol_table_t *table_create(const char *name);
58
66SNOBOL_NODISCARD snobol_table_t *table_retain(snobol_table_t *table);
67
75
85SNOBOL_NODISCARD bool table_set(snobol_table_t *table, const char *key,
86 const char *value);
87
97const char *table_get(const snobol_table_t *table, const char *key);
98
105bool table_has(const snobol_table_t *table, const char *key);
106
113SNOBOL_NODISCARD bool table_delete(snobol_table_t *table, const char *key);
114
126SNOBOL_NODISCARD bool table_set_ex(snobol_table_t *table, const char *key,
127 size_t key_len, const char *value,
128 size_t value_len);
129
141const char *table_get_ex(const snobol_table_t *table, const char *key,
142 size_t key_len, size_t *out_len);
143
151bool table_has_ex(const snobol_table_t *table, const char *key, size_t key_len);
152
160SNOBOL_NODISCARD bool table_delete_ex(snobol_table_t *table, const char *key,
161 size_t key_len);
162
168uint32_t table_hash_string(const char *str);
169
176uint32_t table_hash_bytes(const char *str, size_t len);
177
183
189size_t table_size(const snobol_table_t *table);
190
196const char *table_name(const snobol_table_t *table);
197
198#ifdef __cplusplus
199}
200#endif
struct snobol_table snobol_table_t
Associative table (opaque handle).
Portable function-attribute macros for SNOBOL4.
size_t capacity
Definition table.h:43
char * name
Definition table.h:47
size_t tombstones
Definition table.h:45
uint32_t refcount
Definition table.h:46
table_entry_t * entries
Definition table.h:42
size_t size
Definition table.h:44
Definition table.h:31
bool active
Definition table.h:37
char * value
Definition table.h:34
size_t value_len
Definition table.h:35
size_t key_len
Definition table.h:33
uint32_t hash
Definition table.h:36
char * key
Definition table.h:32
bool table_set_ex(snobol_table_t *table, const char *key, size_t key_len, const char *value, size_t value_len)
Insert or update a key-value pair (NUL-safe byte-exact variants).
const char * table_get_ex(const snobol_table_t *table, const char *key, size_t key_len, size_t *out_len)
Look up a value by key (NUL-safe).
bool table_set(snobol_table_t *table, const char *key, const char *value)
Insert or update a key-value pair.
const char * table_name(const snobol_table_t *table)
Get the table name (for debugging).
uint32_t table_hash_bytes(const char *str, size_t len)
Hash function for byte-exact strings (FNV-1a, NUL-safe).
struct table_entry table_entry_t
bool table_has_ex(const snobol_table_t *table, const char *key, size_t key_len)
Check if a key exists in the table (NUL-safe).
snobol_table_t * table_create(const char *name)
Create a new runtime table.
snobol_table_t * table_retain(snobol_table_t *table)
Increment table reference count.
bool table_delete_ex(snobol_table_t *table, const char *key, size_t key_len)
Delete a key from the table (NUL-safe).
bool table_delete(snobol_table_t *table, const char *key)
Delete a key from the table.
bool table_has(const snobol_table_t *table, const char *key)
Check if a key exists in the table.
void table_clear(snobol_table_t *table)
Clear all entries from the table.
void table_release(snobol_table_t *table)
Decrement table reference count and free if zero.
size_t table_size(const snobol_table_t *table)
Get the number of active entries.
uint32_t table_hash_string(const char *str)
Hash function for strings (FNV-1a).
const char * table_get(const snobol_table_t *table, const char *key)
Look up a value by key.