From b3066f5c8d6e378bec3ecc0f33f8ab7f80fab9b9 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Thu, 26 Oct 2006 06:19:47 +0000 Subject: [PATCH] Back-ported some changes from AMX Mod X --HG-- extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%40309 --- sourcehook/sh_stack.h | 2 ++ sourcehook/sh_tinyhash.h | 56 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/sourcehook/sh_stack.h b/sourcehook/sh_stack.h index 1f330b6..ee6c151 100644 --- a/sourcehook/sh_stack.h +++ b/sourcehook/sh_stack.h @@ -11,6 +11,8 @@ #ifndef __SH_STACK_H__ #define __SH_STACK_H__ +#include + #define SH_STACK_DEFAULT_SIZE 4 namespace SourceHook diff --git a/sourcehook/sh_tinyhash.h b/sourcehook/sh_tinyhash.h index fe455b3..d5d4f08 100644 --- a/sourcehook/sh_tinyhash.h +++ b/sourcehook/sh_tinyhash.h @@ -20,6 +20,12 @@ namespace SourceHook template int HashFunction(const K & k); + template + int HashAlt(const U &u); + + template + int CompareAlt(const U &k1, const K &k2); + template int Compare(const K & k1, const K & k2); @@ -113,6 +119,38 @@ namespace SourceHook m_Buckets = NULL; m_numBuckets = 0; } + public: + template + V & AltFindOrInsert(const U & ukey) + { + size_t place = HashAlt(ukey) % m_numBuckets; + THashNode *pNode = NULL; + if (!m_Buckets[place]) + { + m_Buckets[place] = new List; + pNode = new THashNode(ukey, V()); + m_Buckets[place]->push_back(pNode); + m_percentUsed += (1.0f / (float)m_numBuckets); + } else { + typename List::iterator iter; + for (iter=m_Buckets[place]->begin(); iter!=m_Buckets[place]->end(); iter++) + { + if (CompareAlt(ukey, (*iter)->key) == 0) + { + return (*iter)->val; + } + } + //node does not exist + pNode = new THashNode(ukey, V()); + m_Buckets[place]->push_back(pNode); + } + if (PercentUsed() > 0.75f) + { + _Refactor(); + } + return pNode->val; + } + private: THashNode *_FindOrInsert(const K & key) { size_t place = HashFunction(key) % m_numBuckets; @@ -135,7 +173,9 @@ namespace SourceHook m_Buckets[place]->push_back(pNode); } if (PercentUsed() > 0.75f) + { _Refactor(); + } return pNode; } void _Refactor() @@ -457,7 +497,20 @@ namespace SourceHook } return end(); } - + template + iterator FindAlt(const U & u) + { + iterator b = begin(); + iterator e = end(); + for (iterator iter = b; iter != e; iter++) + { + if (CompareAlt(u, (*iter).key) == 0) + { + return iter; + } + } + return e; + } iterator erase(iterator where) { where.erase(); @@ -479,3 +532,4 @@ namespace SourceHook }; #endif //_INCLUDE_SH_TINYHASH_H_ +