1 /*
3 Copyright (c) 2005, Simon Howard
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16 * Neither the name of the C Algorithms project nor the names of its
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) Texas Instruments Incorporated 2010-2011
34 * ALSO: TI mods to support binary keys
35 * REVISION HISTORY: rev 0.0.1
36 *
37 */
39 /**
40 * @file trie.h
41 *
42 * @brief Fast string lookups
43 *
44 * A trie is a data structure which provides fast mappings from strings
45 * to values.
46 *
47 * To create a new trie, use @ref trie_new. To destroy a trie,
48 * use @ref trie_free.
49 *
50 * To insert a value into a trie, use @ref trie_insert. To remove a value
51 * from a trie, use @ref trie_remove.
52 *
53 * To look up a value from its key, use @ref trie_lookup.
54 *
55 * To find the number of entries in a trie, use @ref trie_num_entries.
56 */
58 #ifndef ALGORITHM_TRIE_H
59 #define ALGORITHM_TRIE_H
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
65 typedef struct _Trie Trie;
67 /**
68 * Create a new trie.
69 *
70 * @return Pointer to a new trie structure.
71 */
73 Trie *trie_new(void);
75 /**
76 * Destroy a trie.
77 *
78 * @param trie The trie to destroy.
79 */
81 void trie_free(Trie *trie);
83 /**
84 * Insert a new key-value pair into a trie.
85 *
86 * @param trie The trie.
87 * @param key The key to access the new value.
88 * @param value The value.
89 */
91 void trie_insert(Trie *trie, char *key, int keylen, void *value);
93 /**
94 * Look up a value from its key in a trie.
95 *
96 * @param trie The trie.
97 * @param key The key.
98 * @return The value associated with the key, or NULL if
99 * not found in the trie.
100 */
102 void *trie_lookup(Trie *trie, char *key, int keylen);
104 /**
105 * Remove an entry from a trie.
106 *
107 * @param trie The trie.
108 * @param key The key of the entry to remove.
109 */
111 void trie_remove(Trie *trie, char *key, int keylen);
113 /**
114 * Find the number of entries in a trie.
115 *
116 * @param trie The trie.
117 * @return Count of the number of entries in the trie.
118 */
120 int trie_num_entries(Trie *trie);
122 #ifdef __cplusplus
123 }
124 #endif
126 #endif /* #ifndef ALGORITHM_TRIE_H */