-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrie.h
50 lines (43 loc) · 1.17 KB
/
trie.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
/*
choices[5] = "abcde"
choice_links[5] = {130, 6, 16, 2483, 134}
values[5] = NULL // if this word is illegal
values[6] = VALUE // if this word is legal
choices[6] = "#ing" // "patricia trie" optimization
choice_links[6] = {9641}
values[6] = VALUE
*/
#pragma pack(push,1)
typedef struct Node {
int value;
char *choices;
struct Node* children;
} Node;
typedef struct Trie {
Node* root;
} Trie;
typedef struct SerialTrie {
Node* root;
char* stream;
int size; // == len(stream)
int nodes;
int chars;
} SerialTrie;
typedef struct FrozenTrie {
Node* root;
char* chars;
unsigned int node_count; // == len(nodes)
unsigned int char_count; // == len(chars)
} FrozenTrie;
Trie* trie_create();
int trie_find_word(Node *trie, const char* str);
Node* trie_add_word(Node *trie, const char* str);
int trie_size(Node* trie);
void trie_destroy(Trie *trie);
void trie_print(Node* trie);
int* trie_find_prefixes(Node *trie, const char* str);
int* trie_find_splits(Node* prefixes, Node* suffixes, char* key);
SerialTrie* trie_save(Node* trie);
FrozenTrie* trie_load(char* stream);
#pragma pack(pop)