#include <stdint.h>
#include <stdbool.h>
Go to the source code of this file.
|
| void | ck_trie_clear (CKTrie *trie) |
| |
| CKTrie * | ck_trie_create (size_t max_nodes) |
| |
| int32_t | ck_trie_find_longest (const CKTrie *trie, const char *text, size_t text_len, size_t start_pos, size_t *match_len) |
| |
| void | ck_trie_free (CKTrie *trie) |
| |
| bool | ck_trie_has_prefix (const CKTrie *trie, const char *text, size_t text_len, size_t pos) |
| |
| int | ck_trie_insert (CKTrie *trie, const char *token, int32_t token_id, bool is_special, int32_t priority) |
| |
| size_t | ck_trie_node_count (const CKTrie *trie) |
| |
◆ ck_trie_clear()
| void ck_trie_clear |
( |
CKTrie * |
trie | ) |
|
Definition at line 80 of file trie.c.
80 {
81 if (!trie) return;
82
83
85 if (!queue) return;
86
87 size_t head = 0, tail = 0;
88 queue[tail++] = trie->
root;
89
90 while (head < tail) {
92 for (int i = 0; i < 256; i++) {
95 }
96 }
97
98 if (node != trie->
root) {
99 free(node);
100 } else {
103 }
104 }
105
106 free(queue);
108}
struct CKTrieNode * children[256]
◆ ck_trie_create()
| CKTrie * ck_trie_create |
( |
size_t |
max_nodes | ) |
|
Definition at line 29 of file trie.c.
29 {
31 if (!trie) {
32 return NULL;
33 }
34
35 if (max_nodes == 0) {
37 }
38
41 free(trie);
42 return NULL;
43 }
44
47
48 return trie;
49}
#define DEFAULT_MAX_NODES
static CKTrieNode * create_node(void)
◆ ck_trie_find_longest()
| int32_t ck_trie_find_longest |
( |
const CKTrie * |
trie, |
|
|
const char * |
text, |
|
|
size_t |
text_len, |
|
|
size_t |
start_pos, |
|
|
size_t * |
match_len |
|
) |
| |
Definition at line 142 of file trie.c.
143 {
144 if (!trie || !
text || start_pos >=
text_len || !match_len) {
145 *match_len = 0;
146 return -1;
147 }
148
151 size_t last_token_len = 0;
152 size_t pos = start_pos;
153
154
156 unsigned char c = (
unsigned char)
text[pos];
157
159 break;
160 }
161
163 pos++;
164
165
167 last_token_node = node;
168 last_token_len = pos - start_pos;
169 }
170 }
171
172 *match_len = last_token_len;
173
174 if (last_token_node) {
176 }
177
178 return -1;
179}
◆ ck_trie_free()
| void ck_trie_free |
( |
CKTrie * |
trie | ) |
|
Definition at line 51 of file trie.c.
51 {
52 if (!trie) return;
53
54
56 if (!queue) {
57
59 free(trie);
60 return;
61 }
62
63 size_t head = 0, tail = 0;
64 queue[tail++] = trie->
root;
65
66 while (head < tail) {
68 for (int i = 0; i < 256; i++) {
71 }
72 }
73 free(node);
74 }
75
76 free(queue);
77 free(trie);
78}
◆ ck_trie_has_prefix()
| bool ck_trie_has_prefix |
( |
const CKTrie * |
trie, |
|
|
const char * |
text, |
|
|
size_t |
text_len, |
|
|
size_t |
pos |
|
) |
| |
Definition at line 181 of file trie.c.
181 {
183
185
187 unsigned char c = (
unsigned char)
text[pos];
189 pos++;
190 }
191
192 return node != NULL;
193}
◆ ck_trie_insert()
| int ck_trie_insert |
( |
CKTrie * |
trie, |
|
|
const char * |
token, |
|
|
int32_t |
token_id, |
|
|
bool |
is_special, |
|
|
int32_t |
priority |
|
) |
| |
Definition at line 110 of file trie.c.
110 {
111 if (!trie || !
token)
return -1;
112
114 const unsigned char *p = (
const unsigned char *)
token;
115
116 while (*p) {
117 unsigned char c = *p++;
118
121 return -1;
122 }
123
125 if (!new_node) return -1;
126
129 }
130
132 }
133
134
138
139 return 0;
140}
int32_t int32_t int32_t int32_t priority
◆ ck_trie_node_count()
| size_t ck_trie_node_count |
( |
const CKTrie * |
trie | ) |
|