core/sm_trie.cpp
author Scott Ehlert <ds@alliedmods.net>
Sun Aug 24 04:35:48 2008 +0000 (2008-08-24)
changeset 2237 4403a982ec91
parent 1965 63d5a5f6935f
child 2289 44045ead61cd
permissions -rw-r--r--
Improved bot detection slightly. This was meant to go along with the fix for amb1801.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <string.h>
ds@1952
    33
#include <assert.h>
ds@1952
    34
#include <sm_trie_tpl.h>
ds@1952
    35
#include "sm_trie.h"
ds@1952
    36
ds@1952
    37
struct Trie
ds@1952
    38
{
ds@1952
    39
	KTrie<void *> k;
ds@1952
    40
};
ds@1952
    41
ds@1952
    42
Trie *sm_trie_create()
ds@1952
    43
{
ds@1952
    44
	return new Trie;
ds@1952
    45
}
ds@1952
    46
ds@1952
    47
void sm_trie_destroy(Trie *trie)
ds@1952
    48
{
ds@1952
    49
	delete trie;
ds@1952
    50
}
ds@1952
    51
ds@1952
    52
bool sm_trie_insert(Trie *trie, const char *key, void *value)
ds@1952
    53
{
ds@1952
    54
	return trie->k.insert(key, value);
ds@1952
    55
}
ds@1952
    56
ds@1952
    57
bool sm_trie_replace(Trie *trie, const char *key, void *value)
ds@1952
    58
{
ds@1952
    59
	return trie->k.replace(key, value);
ds@1952
    60
}
ds@1952
    61
ds@1952
    62
bool sm_trie_retrieve(Trie *trie, const char *key, void **value)
ds@1952
    63
{
ds@1952
    64
	void **pValue = trie->k.retrieve(key);
ds@1952
    65
	
ds@1952
    66
	if (!pValue)
ds@1952
    67
	{
ds@1952
    68
		return false;
ds@1952
    69
	}
ds@1952
    70
ds@1952
    71
	if (value)
ds@1952
    72
	{
ds@1952
    73
		*value = *pValue;
ds@1952
    74
	}
ds@1952
    75
ds@1952
    76
	return true;
ds@1952
    77
}
ds@1952
    78
ds@1952
    79
bool sm_trie_delete(Trie *trie, const char *key)
ds@1952
    80
{
ds@1952
    81
	return trie->k.remove(key);
ds@1952
    82
}
ds@1952
    83
ds@1952
    84
void sm_trie_clear(Trie *trie)
ds@1952
    85
{
ds@1952
    86
	trie->k.clear();
ds@1952
    87
}
ds@1952
    88
ds@1952
    89
size_t sm_trie_mem_usage(Trie *trie)
ds@1952
    90
{
ds@1952
    91
	return trie->k.mem_usage();
ds@1952
    92
}
dvander@1965
    93
dvander@1965
    94
struct trie_iter_data
dvander@1965
    95
{
dvander@1965
    96
	SM_TRIE_BAD_ITERATOR iter;
dvander@1965
    97
	void *ptr;
dvander@1965
    98
	Trie *pTrie;
dvander@1965
    99
};
dvander@1965
   100
dvander@1965
   101
void our_trie_iterator(KTrie<void *> *pTrie, const char *name, void *& obj, void *data)
dvander@1965
   102
{
dvander@1965
   103
	trie_iter_data *our_iter;
dvander@1965
   104
dvander@1965
   105
	our_iter = (trie_iter_data *)data;
dvander@1965
   106
	our_iter->iter(our_iter->pTrie, name, &obj, our_iter->ptr);
dvander@1965
   107
}
dvander@1965
   108
dvander@1965
   109
void sm_trie_bad_iterator(Trie *trie,
dvander@1965
   110
						  char *buffer,
dvander@1965
   111
						  size_t maxlength,
dvander@1965
   112
						  SM_TRIE_BAD_ITERATOR iter,
dvander@1965
   113
						  void *data)
dvander@1965
   114
{
dvander@1965
   115
	trie_iter_data our_iter;
dvander@1965
   116
dvander@1965
   117
	our_iter.iter = iter;
dvander@1965
   118
	our_iter.ptr = data;
dvander@1965
   119
	our_iter.pTrie = trie;
dvander@1965
   120
	trie->k.bad_iterator(buffer, maxlength, &our_iter, our_trie_iterator);
dvander@1965
   121
}