summaryrefslogtreecommitdiffstats
path: root/trie.h
diff options
context:
space:
mode:
Diffstat (limited to 'trie.h')
-rw-r--r--trie.h107
1 files changed, 107 insertions, 0 deletions
diff --git a/trie.h b/trie.h
new file mode 100644
index 0000000..e81f80e
--- /dev/null
+++ b/trie.h
@@ -0,0 +1,107 @@
+/****************************************************************************
+ * bfs *
+ * Copyright (C) 2019 Tavian Barnes <tavianator@tavianator.com> *
+ * *
+ * Permission to use, copy, modify, and/or distribute this software for any *
+ * purpose with or without fee is hereby granted. *
+ * *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF *
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR *
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES *
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN *
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF *
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
+ ****************************************************************************/
+
+#ifndef BFS_TRIE_H
+#define BFS_TRIE_H
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+/**
+ * A trie that holds a set of fixed- or dynamic-length strings.
+ */
+struct trie {
+ uintptr_t root;
+};
+
+/**
+ * Initialize an empty trie.
+ */
+void trie_init(struct trie *trie);
+
+/**
+ * Check if a NUL-terminated string key exists in a trie.
+ *
+ * @param trie
+ * The trie to search.
+ * @param key
+ * The key to search for.
+ * @return
+ * Whether the key was found.
+ */
+bool trie_strsearch(const struct trie *trie, const char *key);
+
+/**
+ * Check if a prefix of a NUL-terminated string key exists in a trie.
+ *
+ * @param trie
+ * The trie to search.
+ * @param key
+ * The key to search for.
+ * @param size
+ * The length of the prefix to consider, in bytes.
+ * @return
+ * Whether the key was found.
+ */
+bool trie_strnsearch(const struct trie *trie, const char *key, size_t size);
+
+/**
+ * Check if a fixed-length key exists in a trie.
+ *
+ * @param trie
+ * The trie to search.
+ * @param key
+ * The key to search for.
+ * @param size
+ * The length of the key in bytes.
+ * @return
+ * Whether the key was found.
+ */
+bool trie_memsearch(const struct trie *trie, const void *key, size_t size);
+
+/**
+ * Insert a NUL-terminated string key in the trie.
+ *
+ * @param trie
+ * The trie to modify.
+ * @param key
+ * The key to insert.
+ * @return
+ * 0 on success, 1 if the key was already present, or -1 on error.
+ */
+int trie_strinsert(struct trie *trie, const char *key);
+
+/**
+ * Insert a fixed-length key in the trie.
+ *
+ * @param trie
+ * The trie to modify.
+ * @param key
+ * The key to insert.
+ * @param size
+ * The length of the key in bytes.
+ * @return
+ * 0 on success, 1 if the key was already present, or -1 on error.
+ */
+int trie_meminsert(struct trie *trie, const void *key, size_t size);
+
+/**
+ * Destroy a trie and its contents.
+ */
+void trie_destroy(struct trie *trie);
+
+#endif // BFS_TRIE_H