added amb1262 - adt_trie

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401785
This commit is contained in:
David Anderson 2007-12-10 05:14:37 +00:00
parent ff15a84e7c
commit d3a22aeff7
7 changed files with 670 additions and 3 deletions

View File

@ -30,7 +30,8 @@ OBJECTS += smn_admin.cpp smn_bitbuffer.cpp smn_console.cpp smn_core.cpp \
smn_filesystem.cpp smn_float.cpp smn_functions.cpp smn_gameconfigs.cpp smn_halflife.cpp \
smn_handles.cpp smn_keyvalues.cpp smn_banning.cpp smn_maplists.cpp \
smn_lang.cpp smn_player.cpp smn_string.cpp smn_sorting.cpp smn_textparse.cpp smn_timers.cpp \
smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp smn_adt_array.cpp
smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp smn_adt_array.cpp \
smn_adt_trie.cpp
OBJECTS += systems/ExtensionSys.cpp systems/ForwardSys.cpp systems/HandleSys.cpp \
systems/LibrarySys.cpp systems/PluginInfoDatabase.cpp systems/PluginSys.cpp \
systems/ShareSys.cpp vm/sp_vm_basecontext.cpp vm/sp_vm_engine.cpp \

View File

@ -30,7 +30,8 @@ OBJECTS += smn_admin.cpp smn_bitbuffer.cpp smn_console.cpp smn_core.cpp \
smn_filesystem.cpp smn_float.cpp smn_functions.cpp smn_gameconfigs.cpp smn_halflife.cpp \
smn_handles.cpp smn_keyvalues.cpp smn_banning.cpp smn_maplists.cpp \
smn_lang.cpp smn_player.cpp smn_string.cpp smn_sorting.cpp smn_textparse.cpp smn_timers.cpp \
smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp smn_adt_array.cpp
smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp smn_adt_array.cpp \
smn_adt_trie.cpp
OBJECTS += systems/ExtensionSys.cpp systems/ForwardSys.cpp systems/HandleSys.cpp \
systems/LibrarySys.cpp systems/PluginInfoDatabase.cpp systems/PluginSys.cpp \
systems/ShareSys.cpp vm/sp_vm_basecontext.cpp vm/sp_vm_engine.cpp \

View File

@ -30,7 +30,8 @@ OBJECTS += smn_admin.cpp smn_bitbuffer.cpp smn_console.cpp smn_core.cpp \
smn_filesystem.cpp smn_float.cpp smn_functions.cpp smn_gameconfigs.cpp smn_halflife.cpp \
smn_handles.cpp smn_keyvalues.cpp smn_banning.cpp smn_maplists.cpp \
smn_lang.cpp smn_player.cpp smn_string.cpp smn_sorting.cpp smn_textparse.cpp smn_timers.cpp \
smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp smn_adt_array.cpp
smn_usermsgs.cpp smn_menus.cpp smn_database.cpp smn_vector.cpp smn_adt_array.cpp \
smn_adt_trie.cpp
OBJECTS += systems/ExtensionSys.cpp systems/ForwardSys.cpp systems/HandleSys.cpp \
systems/LibrarySys.cpp systems/PluginInfoDatabase.cpp systems/PluginSys.cpp \
systems/ShareSys.cpp vm/sp_vm_basecontext.cpp vm/sp_vm_engine.cpp \

View File

@ -1310,6 +1310,10 @@
RelativePath="..\smn_adt_array.cpp"
>
</File>
<File
RelativePath="..\smn_adt_trie.cpp"
>
</File>
<File
RelativePath="..\smn_banning.cpp"
>

513
core/smn_adt_trie.cpp Normal file
View File

@ -0,0 +1,513 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod
* Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include <malloc.h>
#include "sm_globals.h"
#include "sm_stringutil.h"
#include "HandleSys.h"
#include "sm_trie_tpl.h"
HandleType_t htCellTrie;
enum TrieNodeType
{
TrieNode_Cell,
TrieNode_CellArray,
TrieNode_String,
};
struct SmartTrieNode
{
SmartTrieNode()
{
ptr = NULL;
type = TrieNode_Cell;
}
SmartTrieNode(const SmartTrieNode &obj)
{
type = obj.type;
ptr = obj.ptr;
data = obj.data;
data_len = obj.data_len;
}
SmartTrieNode & operator =(const SmartTrieNode &src)
{
type = src.type;
ptr = src.ptr;
data = src.data;
data_len = src.data_len;
return *this;
}
TrieNodeType type;
cell_t *ptr;
cell_t data;
cell_t data_len;
};
struct CellTrie
{
KTrie<SmartTrieNode> trie;
cell_t mem_usage;
};
class TrieHelpers :
public SMGlobalClass,
public IHandleTypeDispatch
{
public: //SMGlobalClass
void OnSourceModAllInitialized()
{
htCellTrie = g_HandleSys.CreateType("Trie", this, 0, NULL, NULL, g_pCoreIdent, NULL);
}
void OnSourceModShutdown()
{
g_HandleSys.RemoveType(htCellTrie, g_pCoreIdent);
}
public: //IHandleTypeDispatch
static void DestroySmartTrieNode(SmartTrieNode *pNode)
{
free(pNode->ptr);
}
void OnHandleDestroy(HandleType_t type, void *object)
{
CellTrie *pTrie = (CellTrie *)object;
pTrie->trie.run_destructor(DestroySmartTrieNode);
delete pTrie;
}
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
{
CellTrie *pArray = (CellTrie *)object;
*pSize = sizeof(CellTrie) + pArray->mem_usage + pArray->trie.mem_usage();
return true;
}
} s_CellTrieHelpers;
static cell_t CreateTrie(IPluginContext *pContext, const cell_t *params)
{
CellTrie *pTrie = new CellTrie;
Handle_t hndl;
pTrie->mem_usage = 0;
if ((hndl = g_HandleSys.CreateHandle(htCellTrie, pTrie, pContext->GetIdentity(), g_pCoreIdent, NULL))
== BAD_HANDLE)
{
delete pTrie;
return BAD_HANDLE;
}
return hndl;
}
static void UpdateNodeCells(CellTrie *pTrie, SmartTrieNode *pData, const cell_t *cells, cell_t num_cells)
{
if (num_cells == 1)
{
pData->data = *cells;
pData->type = TrieNode_Cell;
}
else
{
pData->type = TrieNode_CellArray;
if (pData->ptr == NULL)
{
pData->ptr = (cell_t *)malloc(num_cells * sizeof(cell_t));
pData->data_len = num_cells;
pTrie->mem_usage += (pData->data_len * sizeof(cell_t));
}
else if (pData->data_len < num_cells)
{
pData->ptr = (cell_t *)realloc(pData->ptr, num_cells * sizeof(cell_t));
pTrie->mem_usage += (num_cells - pData->data_len) * sizeof(cell_t);
pData->data_len = num_cells;
}
if (num_cells != 0)
{
memcpy(pData->ptr, cells, sizeof(cell_t) * num_cells);
}
pData->data = num_cells;
}
}
static void UpdateNodeString(CellTrie *pTrie, SmartTrieNode *pData, const char *str)
{
size_t len = strlen(str);
cell_t num_cells = (len + sizeof(cell_t)) / sizeof(cell_t);
if (pData->ptr == NULL)
{
pData->ptr = (cell_t *)malloc(num_cells * sizeof(cell_t));
pData->data_len = num_cells;
pTrie->mem_usage += (pData->data_len * sizeof(cell_t));
}
else if (pData->data_len < num_cells)
{
pData->ptr = (cell_t *)realloc(pData->ptr, num_cells * sizeof(cell_t));
pTrie->mem_usage += (num_cells - pData->data_len) * sizeof(cell_t);
pData->data_len = num_cells;
}
strcpy((char *)pData->ptr, str);
pData->data = len;
pData->type = TrieNode_String;
}
static cell_t SetTrieValue(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
char *key;
pContext->LocalToString(params[2], &key);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
{
SmartTrieNode node;
UpdateNodeCells(pTrie, &node, &params[3], 1);
return pTrie->trie.insert(key, node) ? 1 : 0;
}
if (!params[4])
{
return 0;
}
UpdateNodeCells(pTrie, pNode, &params[3], 1);
return 1;
}
static cell_t SetTrieArray(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
if (params[4] < 0)
{
return pContext->ThrowNativeError("Invalid array size: %d", params[4]);
}
char *key;
cell_t *array;
pContext->LocalToString(params[2], &key);
pContext->LocalToPhysAddr(params[3], &array);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
{
SmartTrieNode node;
UpdateNodeCells(pTrie, &node, array, params[4]);
if (!pTrie->trie.insert(key, node))
{
free(node.ptr);
return 0;
}
return 1;
}
if (!params[4])
{
return 0;
}
UpdateNodeCells(pTrie, pNode, array, params[4]);
return 1;
}
static cell_t SetTrieString(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
char *key, *val;
pContext->LocalToString(params[2], &key);
pContext->LocalToString(params[3], &val);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
{
SmartTrieNode node;
UpdateNodeString(pTrie, &node, val);
if (!pTrie->trie.insert(key, node))
{
free(node.ptr);
return 0;
}
return 1;
}
if (!params[4])
{
return 0;
}
UpdateNodeString(pTrie, pNode, val);
return 1;
}
static cell_t RemoveFromTrie(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
char *key;
pContext->LocalToString(params[2], &key);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
{
return 0;
}
free(pNode->ptr);
pNode->ptr = NULL;
return pTrie->trie.remove(key) ? 1 : 0;
}
static cell_t ClearTrie(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
pTrie->trie.run_destructor(TrieHelpers::DestroySmartTrieNode);
pTrie->trie.clear();
return 1;
}
static cell_t GetTrieValue(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
char *key;
cell_t *pValue;
pContext->LocalToString(params[2], &key);
pContext->LocalToPhysAddr(params[3], &pValue);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL)
{
return 0;
}
if (pNode->type == TrieNode_Cell)
{
*pValue = pNode->data;
return 1;
}
return 0;
}
static cell_t GetTrieArray(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
if (params[4] < 0)
{
return pContext->ThrowNativeError("Invalid array size: %d", params[4]);
}
char *key;
cell_t *pValue, *pSize;
pContext->LocalToString(params[2], &key);
pContext->LocalToPhysAddr(params[3], &pValue);
pContext->LocalToPhysAddr(params[5], &pSize);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL
|| pNode->type != TrieNode_CellArray)
{
return 0;
}
if (pNode->ptr == NULL)
{
*pSize = 0;
return 1;
}
if (pNode->data > params[4])
{
*pSize = params[4];
}
else if (params[4] != 0)
{
*pSize = pNode->data;
}
else
{
return 1;
}
memcpy(pValue, pNode->ptr, sizeof(cell_t) * pSize[0]);
return 1;
}
static cell_t GetTrieString(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl;
CellTrie *pTrie;
HandleError err;
HandleSecurity sec = HandleSecurity(pContext->GetIdentity(), g_pCoreIdent);
hndl = params[1];
if ((err = g_HandleSys.ReadHandle(hndl, htCellTrie, &sec, (void **)&pTrie))
!= HandleError_None)
{
return pContext->ThrowNativeError("Invalid Handle %x (error %d)", hndl, err);
}
if (params[4] < 0)
{
return pContext->ThrowNativeError("Invalid buffer size: %d", params[4]);
}
char *key;
cell_t *pSize;
pContext->LocalToString(params[2], &key);
pContext->LocalToPhysAddr(params[5], &pSize);
SmartTrieNode *pNode;
if ((pNode = pTrie->trie.retrieve(key)) == NULL
|| pNode->type != TrieNode_String)
{
return 0;
}
if (pNode->ptr == NULL)
{
*pSize = 0;
pContext->StringToLocal(params[3], params[4], "");
return 1;
}
size_t written;
pContext->StringToLocalUTF8(params[3], params[4], (char *)pNode->ptr, &written);
*pSize = (cell_t)written;
return 1;
}
REGISTER_NATIVES(trieNatives)
{
{"ClearTrie", ClearTrie},
{"CreateTrie", CreateTrie},
{"GetTrieArray", GetTrieArray},
{"GetTrieString", GetTrieString},
{"GetTrieValue", GetTrieValue},
{"RemoveFromTrie", RemoveFromTrie},
{"SetTrieArray", SetTrieArray},
{"SetTrieString", SetTrieString},
{"SetTrieValue", SetTrieValue},
{NULL, NULL},
};

View File

@ -36,3 +36,4 @@
#define _adt_included
#include <adt_array>
#include <adt_trie>

View File

@ -0,0 +1,146 @@
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This file is part of the SourceMod/SourcePawn SDK.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#if defined _adt_trie_included
#endinput
#endif
#define _adt_trie_included
/**
* Creates a Trie structure. A trie is a data storage object that maps any value to a
* string of text. It features very fast lookup and deletion, but grows very slow for
* insertion once tens of thousands of items are added.
*
* Keys in Tries are unique. That is, each key may only have one value. Unlike arrays,
* Tries cannot be iterated right now. Since the contents are known to be unique, to
* work around this, you can use ADT Arrays to store a list of keys known to be in a
* Trie.
*
* @return New Trie Handle, which must be freed via CloseHandle().
*/
native Handle:CreateTrie();
/**
* Sets a value in a Trie, either inserting a new entry or replacing an old one.
*
* @param trie Trie Handle.
* @param key Key string.
* @param value Value to store at this key.
* @param replace If false, operation will fail if the key is already set.
* @return True on success, false on failure.
* @error Invalid Handle.
*/
native bool:SetTrieValue(Handle:trie, const String:key[], any:value, bool:replace=true);
/**
* Sets an array value in a Trie, either inserting a new entry or replacing an old one.
*
* @param trie Trie Handle.
* @param key Key string.
* @param array Array to store.
* @param num_items Number of items in the array.
* @param replace If false, operation will fail if the key is already set.
* @return True on success, false on failure.
* @error Invalid Handle.
*/
native bool:SetTrieArray(Handle:trie, const String:key[], const array[], num_items, bool:replace=true);
/**
* Sets a string value in a Trie, either inserting a new entry or replacing an old one.
*
* @param trie Trie Handle.
* @param key Key string.
* @param array Array to store.
* @param num_items Number of items in the array.
* @param replace If false, operation will fail if the key is already set.
* @return True on success, false on failure.
* @error Invalid Handle.
*/
native bool:SetTrieString(Handle:trie, const String:key[], const String:value[], bool:replace=true);
/**
* Retrieves a value in a Trie.
*
* @param trie Trie Handle.
* @param key Key string.
* @param val Variable to store value.
* @return True on success. False if the key is not set, or the key is set
* as an array or string (not a value).
* @error Invalid Handle.
*/
native bool:GetTrieValue(Handle:trie, const String:key[], &any:value);
/**
* Retrieves an array in a Trie.
*
* @param trie Trie Handle.
* @param key Key string.
* @param array Buffer to store array.
* @param max_size Maximum size of array buffer.
* @param size Optional parameter to store the number of elements written to the buffer.
* @return True on success. False if the key is not set, or the key is set
* as a value or string (not an array).
* @error Invalid Handle.
*/
native bool:GetTrieArray(Handle:trie, const String:key[], array[], max_size, &size=0);
/**
* Retrieves a string in a Trie.
*
* @param trie Trie Handle.
* @param key Key string.
* @param value Buffer to store value.
* @param max_size Maximum size of string buffer.
* @param size Optional parameter to store the number of bytes written to the buffer.
* @return True on success. False if the key is not set, or the key is set
* as a value or array (not a string).
* @error Invalid Handle.
*/
native bool:GetTrieString(Handle:trie, const String:key[], String:value[], max_size, &size=0);
/**
* Removes a key entry from a Trie.
*
* @param trie Trie Handle.
* @param key Key string.
* @return True on success, false if the value was never set.
* @error Invalid Handle.
*/
native RemoveFromTrie(Handle:trie, const String:key[]);
/**
* Clears all entries from a Trie.
*
* @param trie Trie Handle.
* @error Invalid Handle.
*/
native ClearTrie(Handle:trie);