plugins/include/adt_trie.inc
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 20 19:52:30 2008 +0000 (2008-03-20)
changeset 1943 3bc85e217519
parent 1829 97cd42e800e8
child 1947 e73a1e51833b
permissions -rw-r--r--
Removed debug code?
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     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.
    12  * 
    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
    16  * details.
    17  *
    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/>.
    20  *
    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>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _adt_trie_included
    34  #endinput
    35 #endif
    36 #define _adt_trie_included
    37 
    38 /**
    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.
    42  *
    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 
    46  * Trie.
    47  *
    48  * @return 			New Trie Handle, which must be freed via CloseHandle().
    49  */
    50 native Handle:CreateTrie();
    51 
    52 /**
    53  * Sets a value in a Trie, either inserting a new entry or replacing an old one.
    54  *
    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.
    61  */
    62 native bool:SetTrieValue(Handle:trie, const String:key[], any:value, bool:replace=true);
    63 
    64 /**
    65  * Sets an array value in a Trie, either inserting a new entry or replacing an old one.
    66  *
    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.
    74  */
    75 native bool:SetTrieArray(Handle:trie, const String:key[], const array[], num_items, bool:replace=true);
    76 
    77 /**
    78  * Sets a string value in a Trie, either inserting a new entry or replacing an old one.
    79  *
    80  * @param trie		Trie Handle.
    81  * @param key		Key string.
    82  * @param array		Array to store.
    83  * @param num_items	Number of items in the array.
    84  * @param replace	If false, operation will fail if the key is already set.
    85  * @return			True on success, false on failure.
    86  * @error			Invalid Handle.
    87  */
    88 native bool:SetTrieString(Handle:trie, const String:key[], const String:value[], bool:replace=true);
    89 
    90 /**
    91  * Retrieves a value in a Trie.
    92  *
    93  * @param trie		Trie Handle.
    94  * @param key		Key string.
    95  * @param val		Variable to store value.
    96  * @return			True on success.  False if the key is not set, or the key is set 
    97  *					as an array or string (not a value).
    98  * @error			Invalid Handle.
    99  */
   100 native bool:GetTrieValue(Handle:trie, const String:key[], &any:value);
   101 
   102 /**
   103  * Retrieves an array in a Trie.
   104  *
   105  * @param trie		Trie Handle.
   106  * @param key		Key string.
   107  * @param array		Buffer to store array.
   108  * @param max_size	Maximum size of array buffer.
   109  * @param size		Optional parameter to store the number of elements written to the buffer.
   110  * @return			True on success.  False if the key is not set, or the key is set 
   111  *					as a value or string (not an array).
   112  * @error			Invalid Handle.
   113  */
   114 native bool:GetTrieArray(Handle:trie, const String:key[], array[], max_size, &size=0);
   115 
   116 /**
   117  * Retrieves a string in a Trie.
   118  *
   119  * @param trie		Trie Handle.
   120  * @param key		Key string.
   121  * @param value		Buffer to store value.
   122  * @param max_size	Maximum size of string buffer.
   123  * @param size		Optional parameter to store the number of bytes written to the buffer.
   124  * @return			True on success.  False if the key is not set, or the key is set 
   125  *					as a value or array (not a string).
   126  * @error			Invalid Handle.
   127  */
   128 native bool:GetTrieString(Handle:trie, const String:key[], String:value[], max_size, &size=0);
   129 
   130 /**
   131  * Removes a key entry from a Trie.
   132  *
   133  * @param trie		Trie Handle.
   134  * @param key		Key string.
   135  * @return			True on success, false if the value was never set.
   136  * @error			Invalid Handle.
   137  */
   138 native RemoveFromTrie(Handle:trie, const String:key[]);
   139 
   140 /**
   141  * Clears all entries from a Trie.
   142  *
   143  * @param trie		Trie Handle.
   144  * @error			Invalid Handle.
   145  */
   146 native ClearTrie(Handle:trie);
   147 
   148 /**
   149  * Retrieves the number of elements in a trie.
   150  *
   151  * Note that trie items are not enumerable/iteratable.  If you need to 
   152  * retrieve the elements in a trie, store its keys in an ADT Array. 
   153  *
   154  * @param trie		Trie Handle.
   155  * @return			Number of elements in the trie.
   156  * @error			Invalid Handle.
   157  */
   158 native GetTrieSize(Handle:trie);