1
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3
* libdatrie - Double-Array Trie Library
4
* Copyright (C) 2006 Theppitak Karoonboonyanan <thep@linux.thai.net>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3
22
* darray.h - Double-array trie structure
4
23
* Created: 2006-08-11
5
24
* Author: Theppitak Karoonboonyanan <thep@linux.thai.net>
38
* @brief Create a new double-array object
40
* Create a new empty doubla-array object.
42
56
DArray * da_new ();
45
* @brief Read double-array data from file
47
* @param file : the file to read
49
* @return a pointer to the openned double-array, NULL on failure
51
* Read double-array data from the opened file, starting from the current
52
* file pointer until the end of double array data block. On return, the
53
* file pointer is left at the position after the read block.
55
58
DArray * da_read (FILE *file);
58
* @brief Free double-array data
60
* @param d : the double-array data
62
* Free the given double-array data.
64
60
void da_free (DArray *d);
67
* @brief Write double-array data
69
* @param d : the double-array data
70
* @param file : the file to write to
72
* @return 0 on success, non-zero on failure
74
* Write double-array data to the given @a file, starting from the current
75
* file pointer. On return, the file pointer is left after the double-array
78
62
int da_write (const DArray *d, FILE *file);
82
* @brief Get root state
84
* @param d : the double-array data
86
* @return root state of the @a index set, or TRIE_INDEX_ERROR on failure
88
* Get root state for stepwise walking.
90
65
TrieIndex da_get_root (const DArray *d);
94
* @brief Get BASE cell
96
* @param d : the double-array data
97
* @param s : the double-array state to get data
99
* @return the BASE cell value for the given state
101
* Get BASE cell value for the given state.
103
68
TrieIndex da_get_base (const DArray *d, TrieIndex s);
106
* @brief Get CHECK cell
108
* @param d : the double-array data
109
* @param s : the double-array state to get data
111
* @return the CHECK cell value for the given state
113
* Get CHECK cell value for the given state.
115
70
TrieIndex da_get_check (const DArray *d, TrieIndex s);
119
* @brief Set BASE cell
121
* @param d : the double-array data
122
* @param s : the double-array state to get data
123
* @param val : the value to set
125
* Set BASE cell for the given state to the given value.
127
73
void da_set_base (DArray *d, TrieIndex s, TrieIndex val);
130
* @brief Set CHECK cell
132
* @param d : the double-array data
133
* @param s : the double-array state to get data
134
* @param val : the value to set
136
* Set CHECK cell for the given state to the given value.
138
75
void da_set_check (DArray *d, TrieIndex s, TrieIndex val);
141
* @brief Walk in double-array structure
143
* @param d : the double-array structure
144
* @param s : current state
145
* @param c : the input character
147
* @return boolean indicating success
149
* Walk the double-array trie from state @a *s, using input character @a c.
150
* If there exists an edge from @a *s with arc labeled @a c, this function
151
* returns TRUE and @a *s is updated to the new state. Otherwise, it returns
152
* FALSE and @a *s is left unchanged.
154
77
Bool da_walk (const DArray *d, TrieIndex *s, TrieChar c);
170
93
#define da_is_walkable(d,s,c) \
171
94
(da_get_check ((d), da_get_base ((d), (s)) + (c)) == (s))
174
* @brief Insert a branch from trie node
176
* @param d : the double-array structure
177
* @param s : the state to add branch to
178
* @param c : the character for the branch label
180
* @return the index of the new node
182
* Insert a new arc labelled with character @a c from the trie node
183
* represented by index @a s in double-array structure @a d.
184
* Note that it assumes that no such arc exists before inserting.
186
96
TrieIndex da_insert_branch (DArray *d, TrieIndex s, TrieChar c);
189
* @brief Prune the single branch
191
* @param d : the double-array structure
192
* @param s : the dangling state to prune off
194
* Prune off a non-separate path up from the final state @a s.
195
* If @a s still has some children states, it does nothing. Otherwise,
196
* it deletes the node and all its parents which become non-separate.
198
98
void da_prune (DArray *d, TrieIndex s);
201
* @brief Prune the single branch up to given parent
203
* @param d : the double-array structure
204
* @param p : the parent up to which to be pruned
205
* @param s : the dangling state to prune off
207
* Prune off a non-separate path up from the final state @a s to the
208
* given parent @a p. The prunning stop when either the parent @a p
209
* is met, or a first non-separate node is found.
211
100
void da_prune_upto (DArray *d, TrieIndex p, TrieIndex s);
214
* @brief Enumerate entries stored in double-array structure
216
* @param d : the double-array structure
217
* @param enum_func : the callback function to be called on each separate node
218
* @param user_data : user-supplied data to send as an argument to @a enum_func
220
* @return boolean value indicating whether all the keys are visited
222
* Enumerate all keys stored in double-array structure. For each entry, the
223
* user-supplied @a enum_func callback function is called, with the entry key,
224
* the separate node, and user-supplied data. Returning FALSE from such
225
* callback will stop enumeration and return FALSE.
227
102
Bool da_enumerate (const DArray *d, DAEnumFunc enum_func, void *user_data);
229
104
#endif /* __DARRAY_H */