59 lines
1.2 KiB
C
59 lines
1.2 KiB
C
/*
|
|
* An implementation of tries.
|
|
*/
|
|
|
|
#ifndef _TRI_H_
|
|
#define _TRI_H_
|
|
|
|
#include <stdbool.h>
|
|
|
|
#define NUM_ALPHA_CHARS (256)
|
|
|
|
struct tri_node {
|
|
struct tri_node *nodes[NUM_ALPHA_CHARS];
|
|
void *value;
|
|
};
|
|
|
|
struct tri {
|
|
struct tri_node *head;
|
|
};
|
|
|
|
|
|
/**
|
|
* Initialize a tri and set its maximum depth.
|
|
*/
|
|
void tri_init(struct tri *tri);
|
|
|
|
/**
|
|
* Destroys an initialized tri and frees its internal structures.
|
|
*/
|
|
void tri_free(struct tri *tri);
|
|
|
|
/**
|
|
* Add a string as a key, and a value to the tri.
|
|
*/
|
|
int tri_add_string(struct tri *tri, const char *string, void * value);
|
|
|
|
/**
|
|
* Returns the value cooresponding to string. If the string is not in the tri, return
|
|
* NULL.
|
|
*/
|
|
void *tri_get_string(struct tri *tri, const char *string);
|
|
|
|
/**
|
|
* Returns the value cooresponding to the longest matching prefix
|
|
*/
|
|
void *tri_prefix_match(struct tri *tri, const char *string);
|
|
|
|
/**
|
|
* Using 'string' as a prefix, returns the value of the cooresponding string if there
|
|
* is only one that matches.
|
|
*
|
|
* If there are multiple possible matches, return NULL and set 'ambiguous' to true. If
|
|
* there are no possible matches, return NULL and set 'ambiguous' to false.
|
|
*/
|
|
void *tri_get_string_autocomplete(struct tri *tri, const char *string, bool *ambiguous);
|
|
|
|
|
|
#endif
|