diff options
author | David McMackins II <contact@mcmackins.org> | 2015-10-04 15:33:45 -0500 |
---|---|---|
committer | David McMackins II <contact@mcmackins.org> | 2015-10-04 15:33:45 -0500 |
commit | bc42b25bc327edbce3487864b87a533ae9263135 (patch) | |
tree | facf316b38c140ccc070caee31b435707e22548b | |
parent | 0d4ac09a0299e3dad3af2ae366076b8c66c7734b (diff) |
Add support for pointer data
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | README | 2 | ||||
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | pfxtree.c | 38 | ||||
-rw-r--r-- | pfxtree.h | 49 | ||||
-rw-r--r-- | pfxtree.pc | 2 |
6 files changed, 79 insertions, 16 deletions
diff --git a/Makefile.am b/Makefile.am index ef80b91..0c318b3 100644 --- a/Makefile.am +++ b/Makefile.am @@ -7,7 +7,7 @@ include_HEADERS = pfxtree.h lib_LTLIBRARIES = libpfxtree.la libpfxtree_la_SOURCES = pfxtree.c -libpfxtree_la_LDFLAGS = -version-info 0:0:0 +libpfxtree_la_LDFLAGS = -version-info 1:0:1 libpfxtree_la_CFLAGS = -Wall -Wextra -Wunreachable-code -ftrapv -std=c89 AM_CFLAGS = $(DEPS_CFLAGS) @@ -18,7 +18,7 @@ is slow and inefficient on the CPU. Trie structures can be more memory efficient that a static string dictionary as well, because any words that share the same starting characters (prefix) will share the same memory for those characters. This does come at a cost for dictionaries with words that are -nothing alike, because each character in the dictionary is more than 1 byte (24 +nothing alike, because each character in the dictionary is more than 1 byte (32 bytes each on my implementation) since it must keep track of what characters are adjacent to it, and this implementation of the structure allows storage of one integer as data for complete words (used in LiberTI's encoder to determine diff --git a/configure.ac b/configure.ac index dffcd94..333178c 100644 --- a/configure.ac +++ b/configure.ac @@ -1,5 +1,5 @@ AC_PREREQ([2.60]) -AC_INIT([libpfxtree],[0.0.0],[support@delwink.com]) +AC_INIT([libpfxtree],[0.1.0],[support@delwink.com]) AC_CONFIG_SRCDIR([pfxtree.c]) AC_CONFIG_AUX_DIR([build-aux]) @@ -34,7 +34,8 @@ pt_new () return NULL; self->ch = '\0'; - self->data = 0; + self->type = '\0'; + self->data.i = 0; self->children = NULL; self->next = NULL; @@ -80,8 +81,8 @@ get_child_by_ch (const PrefixTree *self, const char ch) return NULL; } -int -pt_add (PrefixTree *self, const char *word, int data) +static int +add (PrefixTree *self, const char *word, union _pt_data data, char type) { int rc = 0; PrefixTree *node = self; @@ -128,14 +129,43 @@ pt_add (PrefixTree *self, const char *word, int data) } node->data = data; + node->type = type; return rc; } int +pt_add (PrefixTree *self, const char *word, int data) +{ + union _pt_data d; + d.i = data; + return add (self, word, d, 'i'); +} + +int +pt_add_p (PrefixTree *self, const char *word, void *data) +{ + union _pt_data d; + d.p = data; + return add (self, word, d, 'p'); +} + +int pt_data (const PrefixTree *self) { - return self->data; + return self->data.i; +} + +void * +pt_data_p (const PrefixTree *self) +{ + return self->data.p; +} + +char +pt_data_type (const PrefixTree *self) +{ + return self->type; } const PrefixTree * @@ -17,8 +17,8 @@ /** * @file pfxtree.h - * @version 0.0 - * @date 09/05/2015 + * @version 0.1 + * @date 10/04/2015 * @author David McMackins II * @brief Delwink prefix tree (trie) library */ @@ -44,16 +44,23 @@ enum pt_error PT_EALLOC = -1 }; +union _pt_data +{ + int i; + void *p; +}; + /** * @brief A node in a prefix tree. */ -typedef struct trie +typedef struct _pt_trie { char ch; - int data; + char type; + union _pt_data data; - struct trie *children; - struct trie *next; + struct _pt_trie *children; + struct _pt_trie *next; } PrefixTree; /** @@ -71,7 +78,7 @@ void pt_free (PrefixTree *self); /** - * @brief Adds a new word to a prefix tree. + * @brief Adds a new word to a prefix tree with integer data. * @param self The tree to which to add the word. * @param word The word to be added. * @param data A number to be associated with the word. @@ -81,7 +88,17 @@ int pt_add (PrefixTree *self, const char *word, int data); /** - * @brief Gets the data from a prefix tree node. + * @brief Adds a new word to a prefix tree with pointer data. + * @param self The tree to which to add the word. + * @param word The word to be added. + * @param data A pointer to be associated with the word. + * @return Nonzero if an error occurred. + */ +int +pt_add_p (PrefixTree *self, const char *word, void *data); + +/** + * @brief Gets the integer data from a prefix tree node. * @param self The node from which to get the data. * @return self's data (default 0). */ @@ -89,6 +106,22 @@ int pt_data (const PrefixTree *self); /** + * @brief Gets the pointer data from a prefix tree node. + * @param self The node from which to get the data. + * @return self's data (default NULL). + */ +void * +pt_data_p (const PrefixTree *self); + +/** + * @brief Gets the type of the data stored in the prefix tree node. + * @param self The node whose type is to be examined. + * @return 'i' if integer, 'p' if pointer, or '\0' if no data. + */ +char +pt_data_type (const PrefixTree *self); + +/** * @brief Searches for a word in a prefix tree. * @param root The node from which to begin the search. * @param word The word for which to search. @@ -3,7 +3,7 @@ exec_prefix = ${prefix} libdir = ${exec_prefix}/lib includedir = ${prefix}/include -Version: 0.0.0 +Version: 0.1.0 Cflags = -I${includedir} Description: Delwink prefix tree library Name: pfxtree |