LibCDS
trie.h File Reference

The string dictionary container. More...

Go to the source code of this file.

Data Structures

struct  Trie
 The implementation for trie. More...
 

Typedefs

typedef struct TrieData_ TrieData
 TrieData is the data type for the container private information. More...
 

Functions

int32_t TrieInit (Trie **ppObj)
 The constructor for Trie. More...
 
void TrieDeinit (Trie **ppObj)
 The destructor for Trie. More...
 
int32_t TrieInsert (Trie *self, char *str)
 Insert a string into the trie. More...
 
int32_t TrieBulkInsert (Trie *self, char **aStr, int iNum)
 Insert an array of strings into the trie. More...
 
int32_t TrieHasExact (Trie *self, char *str)
 Check if the trie contains the designated string. More...
 
int32_t TrieHasPrefixAs (Trie *self, char *str)
 Check if the trie contains the strings matching the designated prefix. More...
 
int32_t TrieGetPrefixAs (Trie *self, char *str, char ***paStr, int *piNum)
 Retrieve the strings from the trie matching the designated prefix. More...
 
int32_t TrieDelete (Trie *self, char *str)
 Delete a string from the trie. More...
 
int32_t TrieSize (Trie *self)
 Return the number of strings stored in the trie. More...
 

Detailed Description

The string dictionary container.

Definition in file trie.h.

Typedef Documentation

typedef struct TrieData_ TrieData

TrieData is the data type for the container private information.

Definition at line 10 of file trie.h.

Function Documentation

int32_t TrieInit ( Trie **  ppObj)

The constructor for Trie.

Parameters
ppObjThe double pointer to the to be constructed trie
Return values
SUCC
ERR_NOMEMInsufficient memory for trie construction
void TrieDeinit ( Trie **  ppObj)

The destructor for Trie.

Parameters
ppObjThe double pointer to the to be destructed trie
int32_t TrieInsert ( Trie self,
char *  str 
)

Insert a string into the trie.

Parameters
selfThe pointer to Trie structure
strThe designated string
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory for trie extension
int32_t TrieBulkInsert ( Trie self,
char **  aStr,
int  iNum 
)

Insert an array of strings into the trie.

Parameters
selfThe pointer to Trie structure
aStrArray of to be inserted strings
iNumThe array size
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory for trie extension
int32_t TrieHasExact ( Trie self,
char *  str 
)

Check if the trie contains the designated string.

Parameters
selfThe pointer to Trie structure
strThe designated string
Return values
SUCC
NOKEY
ERR_NOINITUninitialized container
int32_t TrieHasPrefixAs ( Trie self,
char *  str 
)

Check if the trie contains the strings matching the designated prefix.

Parameters
selfThe pointer to Trie structure
strThe designated prefix
Return values
SUCC
NOKEY
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory to prepare trie traversal
int32_t TrieGetPrefixAs ( Trie self,
char *  str,
char ***  paStr,
int *  piNum 
)

Retrieve the strings from the trie matching the designated prefix.

To retrieve the strings, you need to pass:

  • The pointer to the string array to store the returned strings.
  • The pointer to the integer for the returned array size.

And this function will allocate the memory to store the returned strings. But if no string can be resolved, the string array will be returned as NULL and the array size will be returned as 0.

Parameters
selfThe pointer to Trie structure
strThe designated prefix
paStrThe pointer to the returned array of strings
piNumThe pointer to the returned array size
Return values
SUCC
NOKEY
ERR_NOINITUninitialized container
ERR_GETInvalid parameter to store returned data
ERR_NOMEMInsufficient memory to store the resolved strings
Note
Please remember to free the following resource:
  • Each returned string
  • The array to store returned strings
int32_t TrieDelete ( Trie self,
char *  str 
)

Delete a string from the trie.

Parameters
selfThe pointer to Trie structure
strThe designated string
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANon-existent string
int32_t TrieSize ( Trie self)

Return the number of strings stored in the trie.

Parameters
selfThe pointer to Trie structure
Returns
The number of strings
Return values
ERR_NOINITUninitialized container