1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/public/sm_stringhashmap.h Sun Aug 25 01:42:14 2013 -0700
1.3 @@ -0,0 +1,178 @@
1.4 +/**
1.5 + * vim: set ts=4 sw=4 tw=99 noet :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#ifndef _include_sourcemod_hashtable_h_
1.36 +#define _include_sourcemod_hashtable_h_
1.37 +
1.38 +#include <am-allocator-policies.h>
1.39 +#include <am-hashmap.h>
1.40 +#include <am-string.h>
1.41 +#include <string.h>
1.42 +
1.43 +namespace SourceMod
1.44 +{
1.45 +
1.46 +using namespace ke;
1.47 +
1.48 +namespace detail
1.49 +{
1.50 + class CharsAndLength
1.51 + {
1.52 + public:
1.53 + CharsAndLength(const char *str)
1.54 + : str_(str),
1.55 + length_(0)
1.56 + {
1.57 + int c;
1.58 + uint32_t hash = 0;
1.59 + while ((c = *str++))
1.60 + hash = c + (hash << 6) + (hash << 16) - hash;
1.61 + hash_ = hash;
1.62 + length_ = str - str_ - 1;
1.63 + }
1.64 +
1.65 + uint32_t hash() const {
1.66 + return hash_;
1.67 + }
1.68 + const char *chars() const {
1.69 + return str_;
1.70 + }
1.71 + size_t length() const {
1.72 + return length_;
1.73 + }
1.74 +
1.75 + private:
1.76 + const char *str_;
1.77 + size_t length_;
1.78 + uint32_t hash_;
1.79 + };
1.80 +
1.81 + struct StringHashMapPolicy
1.82 + {
1.83 + static inline bool matches(const CharsAndLength &lookup, const AString &key) {
1.84 + return lookup.length() == key.length() &&
1.85 + memcmp(lookup.chars(), key.chars(), key.length()) == 0;
1.86 + }
1.87 + static inline uint32_t hash(const CharsAndLength &key) {
1.88 + return key.hash();
1.89 + }
1.90 + };
1.91 +}
1.92 +
1.93 +template <typename T>
1.94 +class StringHashMap
1.95 +{
1.96 + typedef detail::CharsAndLength CharsAndLength;
1.97 + typedef HashMap<AString, T, detail::StringHashMapPolicy> Internal;
1.98 +
1.99 +public:
1.100 + StringHashMap()
1.101 + : internal_(SystemAllocatorPolicy()),
1.102 + memory_used_(0)
1.103 + {
1.104 + if (!internal_.init())
1.105 + internal_.reportOutOfMemory();
1.106 + }
1.107 +
1.108 + // Some KTrie-like helper functions.
1.109 + bool retrieve(const char *aKey, T *aResult = NULL)
1.110 + {
1.111 + CharsAndLength key(aKey);
1.112 + typename Internal::Result r = internal_.find(key);
1.113 + if (!r.found())
1.114 + return false;
1.115 + if (aResult)
1.116 + *aResult = r->value;
1.117 + return true;
1.118 + }
1.119 +
1.120 + bool contains(const char *aKey)
1.121 + {
1.122 + CharsAndLength key(aKey);
1.123 + typename Internal::Result r = internal_.find(key);
1.124 + return r.found();
1.125 + }
1.126 +
1.127 + bool replace(const char *aKey, const T &value)
1.128 + {
1.129 + CharsAndLength key(aKey);
1.130 + typename Internal::Insert i = internal_.findForAdd(key);
1.131 + if (!i.found())
1.132 + {
1.133 + memory_used_ += key.length() + 1;
1.134 + return internal_.add(i, value);
1.135 + }
1.136 + i->value = value;
1.137 + return true;
1.138 + }
1.139 +
1.140 + bool insert(const char *aKey, const T &value)
1.141 + {
1.142 + CharsAndLength key(aKey);
1.143 + typename Internal::Insert i = internal_.findForAdd(key);
1.144 + if (i.found())
1.145 + return false;
1.146 + if (!internal_.add(i))
1.147 + return false;
1.148 + memory_used_ += key.length() + 1;
1.149 + i->key = aKey;
1.150 + i->value = value;
1.151 + return true;
1.152 + }
1.153 +
1.154 + bool remove(const char *aKey)
1.155 + {
1.156 + CharsAndLength key(aKey);
1.157 + typename Internal::Result r = internal_.find(key);
1.158 + if (!r.found())
1.159 + return false;
1.160 + memory_used_ -= key.length() + 1;
1.161 + internal_.remove(r);
1.162 + return true;
1.163 + }
1.164 +
1.165 + void clear()
1.166 + {
1.167 + internal_.clear();
1.168 + }
1.169 +
1.170 + size_t mem_usage() const {
1.171 + return internal_.estimateMemoryUse() + memory_used_;
1.172 + }
1.173 +
1.174 +private:
1.175 + Internal internal_;
1.176 + size_t memory_used_;
1.177 +};
1.178 +
1.179 +}
1.180 +
1.181 +#endif // _include_sourcemod_hashtable_h_