LibCDS
trie.h
Go to the documentation of this file.
1 /**
2  * @file trie.h The string dictionary container
3  */
4 #ifndef _TRIE_H_
5 #define _TRIE_H_
6 
7 #include "../util.h"
8 
9 /** TrieData is the data type for the container private information. */
10 typedef struct TrieData_ TrieData;
11 
12 /** The implementation for trie. */
13 typedef struct _Trie {
14  /** The container private information. */
16 
17  /** Insert a string into the trie.
18  @see TrieInsert */
19  int32_t (*insert) (struct _Trie*, char*);
20 
21  /** Insert an array of strings into the trie.
22  @see TrieBulkInsert */
23  int32_t (*bulk_insert) (struct _Trie*, char**, int);
24 
25  /** Check if the trie contains the designated string.
26  @see TrieHasExact */
27  int32_t (*has_exact) (struct _Trie*, char*);
28 
29  /** Check if the trie contains the strings matching the designated prefix.
30  @see TrieHasPrefixAs */
31  int32_t (*has_prefix_as) (struct _Trie*, char*);
32 
33  /** Retrieve the strings from the trie matching the designated prefix.
34  @see TrieGetPrefixAs */
35  int32_t (*get_prefix_as) (struct _Trie*, char*, char***, int*);
36 
37  /** Delete a string from the trie.
38  @see TrieDelete */
39  int32_t (*delete) (struct _Trie*, char*);
40 
41  /** Return the number of strings stored in the trie.
42  @see TrieSize */
43  int32_t (*size) (struct _Trie*);
44 } Trie;
45 
46 
47 /*===========================================================================*
48  * Definition for the exported member operations *
49  *===========================================================================*/
50 /**
51  * @brief The constructor for Trie.
52  *
53  * @param ppObj The double pointer to the to be constructed trie
54  *
55  * @retval SUCC
56  * @retval ERR_NOMEM Insufficient memory for trie construction
57  */
58 int32_t TrieInit(Trie **ppObj);
59 
60 /**
61  * @brief The destructor for Trie.
62  *
63  * @param ppObj The double pointer to the to be destructed trie
64  */
65 void TrieDeinit(Trie **ppObj);
66 
67 /**
68  * @brief Insert a string into the trie.
69  *
70  * @param self The pointer to Trie structure
71  * @param str The designated string
72  *
73  * @retval SUCC
74  * @retval ERR_NOINIT Uninitialized container
75  * @retval ERR_NOMEM Insufficient memory for trie extension
76  */
77 int32_t TrieInsert(Trie *self, char *str);
78 
79 /**
80  * @brief Insert an array of strings into the trie.
81  *
82  * @param self The pointer to Trie structure
83  * @param aStr Array of to be inserted strings
84  * @param iNum The array size
85  *
86  * @retval SUCC
87  * @retval ERR_NOINIT Uninitialized container
88  * @retval ERR_NOMEM Insufficient memory for trie extension
89  */
90 int32_t TrieBulkInsert(Trie *self, char **aStr, int iNum);
91 
92 /**
93  * @brief Check if the trie contains the designated string.
94  *
95  * @param self The pointer to Trie structure
96  * @param str The designated string
97  *
98  * @retval SUCC
99  * @retval NOKEY
100  * @retval ERR_NOINIT Uninitialized container
101  */
102 int32_t TrieHasExact(Trie *self, char *str);
103 
104 /**
105  * @brief Check if the trie contains the strings matching the designated prefix.
106  *
107  * @param self The pointer to Trie structure
108  * @param str The designated prefix
109  *
110  * @retval SUCC
111  * @retval NOKEY
112  * @retval ERR_NOINIT Uninitialized container
113  * @retval ERR_NOMEM Insufficient memory to prepare trie traversal
114  */
115 int32_t TrieHasPrefixAs(Trie *self, char *str);
116 
117 /**
118  * @brief Retrieve the strings from the trie matching the designated prefix.
119  *
120  * To retrieve the strings, you need to pass:
121  * - The pointer to the string array to store the returned strings.
122  * - The pointer to the integer for the returned array size.
123  *
124  * And this function will allocate the memory to store the returned strings.
125  * But if no string can be resolved, the string array will be returned as NULL
126  * and the array size will be returned as 0.
127  *
128  * @param self The pointer to Trie structure
129  * @param str The designated prefix
130  * @param paStr The pointer to the returned array of strings
131  * @param piNum The pointer to the returned array size
132  *
133  * @retval SUCC
134  * @retval NOKEY
135  * @retval ERR_NOINIT Uninitialized container
136  * @retval ERR_GET Invalid parameter to store returned data
137  * @retval ERR_NOMEM Insufficient memory to store the resolved strings
138  *
139  * @note Please remember to free the following resource:
140  * - Each returned string
141  * - The array to store returned strings
142  */
143 int32_t TrieGetPrefixAs(Trie *self, char* str, char ***paStr, int *piNum);
144 
145 /**
146  * @brief Delete a string from the trie.
147  *
148  * @param self The pointer to Trie structure
149  * @param str The designated string
150  *
151  * @retval SUCC
152  * @retval ERR_NOINIT Uninitialized container
153  * @retval ERR_NODATA Non-existent string
154  */
155 int32_t TrieDelete(Trie *self, char *str);
156 
157 /**
158  * @brief Return the number of strings stored in the trie.
159  *
160  * @param self The pointer to Trie structure
161  *
162  * @return The number of strings
163  * @retval ERR_NOINIT Uninitialized container
164  */
165 int32_t TrieSize(Trie *self);
166 
167 #endif
int32_t TrieInsert(Trie *self, char *str)
Insert a string into the trie.
TrieData * pData
The container private information.
Definition: trie.h:15
int32_t TrieGetPrefixAs(Trie *self, char *str, char ***paStr, int *piNum)
Retrieve the strings from the trie matching the designated prefix.
int32_t TrieSize(Trie *self)
Return the number of strings stored in the trie.
struct TrieData_ TrieData
TrieData is the data type for the container private information.
Definition: trie.h:10
void TrieDeinit(Trie **ppObj)
The destructor for Trie.
int32_t TrieHasPrefixAs(Trie *self, char *str)
Check if the trie contains the strings matching the designated prefix.
int32_t TrieBulkInsert(Trie *self, char **aStr, int iNum)
Insert an array of strings into the trie.
The implementation for trie.
Definition: trie.h:13
int32_t TrieDelete(Trie *self, char *str)
Delete a string from the trie.
int32_t TrieHasExact(Trie *self, char *str)
Check if the trie contains the designated string.
int32_t TrieInit(Trie **ppObj)
The constructor for Trie.