1 #ifndef fpst_H
2 #define fpst_H 1
3 
4 #include <stdint.h>
5 #include <stdlib.h>
6 
7 /** A trie */
8 #ifndef fpst_GLOBALS
9 typedef struct FPST FPST;
10 #endif
11 
12 /** Type of the function pointer for `fpst_free()` */
13 typedef void (*FPST_FreeFn)(const char *key, uint32_t val);
14 
15 /** Returns an empty trie */
16 FPST *fpst_new(void);
17 
18 /**
19  * Deallocates a trie, optionally calling `free_kv_fn` for each element.
20  * `free_kv_fn` can be `NULL` if this is not necessary.
21  */
22 void fpst_free(FPST *trie, FPST_FreeFn free_kv_fn);
23 
24 /**
25  * Check if the string `str` of length `len` starts with one of the keys
26  * present in the trie. Returns `1` if this is the case, `0` otherwise.
27  * If `found_key_p` and/or `found_val_p` are not `NULL`, these are filled
28  * with the longest matching key and its corresponding value.
29  */
30 int fpst_starts_with_existing_key(FPST *t,
31                                   const char *str, size_t len,
32                                   const char **found_key_p,
33                                   uint32_t *found_val_p);
34 
35 /**
36  * Check if the zero-terminated string `str` starts with one of the keys
37  * present in the trie. Returns `1` if this is the case, `0` otherwise.
38  * If `found_key_p` and/or `found_val_p` are not `NULL`, these are filled
39  * with the longest matching key and its corresponding value.
40  */
41 int fpst_str_starts_with_existing_key(FPST *trie, const char *str,
42                                       const char **found_key_p, uint32_t *found_val_p);
43 
44 /** Check if the exact key `key` of length `len` (`\0` not included) exists */
45 int fpst_has_key(FPST *trie, const char *key, size_t len, uint32_t *found_val_p);
46 
47 /** Check if the exact zero-terminated key `key` is present in the trie */
48 int fpst_has_key_str(FPST *trie, const char *key, uint32_t *found_val_p);
49 
50 /**
51  * Inserts a key `key` of length `len` (not including the leading `\0`)
52  * into the trie.
53  */
54 FPST * fpst_insert(FPST *trie, const char *key, size_t len, uint32_t val);
55 
56 /**
57  * Inserts a zero-terminated key `key` into the trie.
58  */
59 FPST * fpst_insert_str(FPST *trie, const char *key, uint32_t val);
60 
61 #endif
62