summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2015-10-04 15:33:45 -0500
committerDavid McMackins II <contact@mcmackins.org>2015-10-04 15:33:45 -0500
commitbc42b25bc327edbce3487864b87a533ae9263135 (patch)
treefacf316b38c140ccc070caee31b435707e22548b
parent0d4ac09a0299e3dad3af2ae366076b8c66c7734b (diff)
Add support for pointer data
-rw-r--r--Makefile.am2
-rw-r--r--README2
-rw-r--r--configure.ac2
-rw-r--r--pfxtree.c38
-rw-r--r--pfxtree.h49
-rw-r--r--pfxtree.pc2
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)
diff --git a/README b/README
index 4979729..cbe400f 100644
--- a/README
+++ b/README
@@ -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])
diff --git a/pfxtree.c b/pfxtree.c
index aed2784..190b226 100644
--- a/pfxtree.c
+++ b/pfxtree.c
@@ -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 *
diff --git a/pfxtree.h b/pfxtree.h
index 1eb5a3e..45c16fa 100644
--- a/pfxtree.h
+++ b/pfxtree.h
@@ -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.
diff --git a/pfxtree.pc b/pfxtree.pc
index 50272c8..e609fe3 100644
--- a/pfxtree.pc
+++ b/pfxtree.pc
@@ -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