3 * =============================================================================
4 * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved.
5 * =============================================================================
7 * This file is part of the SourceMod/SourcePawn SDK.
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, version 3.0, as published by the
11 * Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 * You should have received a copy of the GNU General Public License along with
19 * this program. If not, see <http://www.gnu.org/licenses/>.
21 * As a special exception, AlliedModders LLC gives you permission to link the
22 * code of this program (as well as its derivative works) to "Half-Life 2," the
23 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
24 * by the Valve Corporation. You must obey the GNU General Public License in
25 * all respects for all other code used. Additionally, AlliedModders LLC grants
26 * this exception to all derivative works. AlliedModders LLC defines further
27 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
28 * or <http://www.sourcemod.net/license.php>.
33 #if defined _adt_trie_included
36 #define _adt_trie_included
39 * Creates a Trie structure. A trie is a data storage object that maps any value to a
40 * string of text. It features very fast lookup and deletion, but grows very slow for
41 * insertion once tens of thousands of items are added.
43 * Keys in Tries are unique. That is, each key may only have one value. Unlike arrays,
44 * Tries cannot be iterated right now. Since the contents are known to be unique, to
45 * work around this, you can use ADT Arrays to store a list of keys known to be in a
48 * @return New Trie Handle, which must be freed via CloseHandle().
50 native Handle:CreateTrie();
53 * Sets a value in a Trie, either inserting a new entry or replacing an old one.
55 * @param trie Trie Handle.
56 * @param key Key string.
57 * @param value Value to store at this key.
58 * @param replace If false, operation will fail if the key is already set.
59 * @return True on success, false on failure.
60 * @error Invalid Handle.
62 native bool:SetTrieValue(Handle:trie, const String:key[], any:value, bool:replace=true);
65 * Sets an array value in a Trie, either inserting a new entry or replacing an old one.
67 * @param trie Trie Handle.
68 * @param key Key string.
69 * @param array Array to store.
70 * @param num_items Number of items in the array.
71 * @param replace If false, operation will fail if the key is already set.
72 * @return True on success, false on failure.
73 * @error Invalid Handle.
75 native bool:SetTrieArray(Handle:trie, const String:key[], const any:array[], num_items, bool:replace=true);
78 * Sets a string value in a Trie, either inserting a new entry or replacing an old one.
80 * @param trie Trie Handle.
81 * @param key Key string.
82 * @param value String to store.
83 * @param replace If false, operation will fail if the key is already set.
84 * @return True on success, false on failure.
85 * @error Invalid Handle.
87 native bool:SetTrieString(Handle:trie, const String:key[], const String:value[], bool:replace=true);
90 * Retrieves a value in a Trie.
92 * @param trie Trie Handle.
93 * @param key Key string.
94 * @param val Variable to store value.
95 * @return True on success. False if the key is not set, or the key is set
96 * as an array or string (not a value).
97 * @error Invalid Handle.
99 native bool:GetTrieValue(Handle:trie, const String:key[], &any:value);
102 * Retrieves an array in a Trie.
104 * @param trie Trie Handle.
105 * @param key Key string.
106 * @param array Buffer to store array.
107 * @param max_size Maximum size of array buffer.
108 * @param size Optional parameter to store the number of elements written to the buffer.
109 * @return True on success. False if the key is not set, or the key is set
110 * as a value or string (not an array).
111 * @error Invalid Handle.
113 native bool:GetTrieArray(Handle:trie, const String:key[], any:array[], max_size, &size=0);
116 * Retrieves a string in a Trie.
118 * @param trie Trie Handle.
119 * @param key Key string.
120 * @param value Buffer to store value.
121 * @param max_size Maximum size of string buffer.
122 * @param size Optional parameter to store the number of bytes written to the buffer.
123 * @return True on success. False if the key is not set, or the key is set
124 * as a value or array (not a string).
125 * @error Invalid Handle.
127 native bool:GetTrieString(Handle:trie, const String:key[], String:value[], max_size, &size=0);
130 * Removes a key entry from a Trie.
132 * @param trie Trie Handle.
133 * @param key Key string.
134 * @return True on success, false if the value was never set.
135 * @error Invalid Handle.
137 native RemoveFromTrie(Handle:trie, const String:key[]);
140 * Clears all entries from a Trie.
142 * @param trie Trie Handle.
143 * @error Invalid Handle.
145 native ClearTrie(Handle:trie);
148 * Retrieves the number of elements in a trie.
150 * Note that trie items are not enumerable/iteratable. If you need to
151 * retrieve the elements in a trie, store its keys in an ADT Array.
153 * @param trie Trie Handle.
154 * @return Number of elements in the trie.
155 * @error Invalid Handle.
157 native GetTrieSize(Handle:trie);