mirror of
https://github.com/alliedmodders/metamod-source.git
synced 2025-12-07 02:18:30 +00:00
Back-ported some changes from AMX Mod X
--HG-- extra : convert_revision : svn%3Ac2935e3e-5518-0410-8daf-afa5dab7d4e3/trunk%40309
This commit is contained in:
parent
08a498aaee
commit
b3066f5c8d
@ -11,6 +11,8 @@
|
|||||||
#ifndef __SH_STACK_H__
|
#ifndef __SH_STACK_H__
|
||||||
#define __SH_STACK_H__
|
#define __SH_STACK_H__
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#define SH_STACK_DEFAULT_SIZE 4
|
#define SH_STACK_DEFAULT_SIZE 4
|
||||||
|
|
||||||
namespace SourceHook
|
namespace SourceHook
|
||||||
|
|||||||
@ -20,6 +20,12 @@ namespace SourceHook
|
|||||||
template <class K>
|
template <class K>
|
||||||
int HashFunction(const K & k);
|
int HashFunction(const K & k);
|
||||||
|
|
||||||
|
template <class U>
|
||||||
|
int HashAlt(const U &u);
|
||||||
|
|
||||||
|
template <class U, class K>
|
||||||
|
int CompareAlt(const U &k1, const K &k2);
|
||||||
|
|
||||||
template <class K>
|
template <class K>
|
||||||
int Compare(const K & k1, const K & k2);
|
int Compare(const K & k1, const K & k2);
|
||||||
|
|
||||||
@ -113,6 +119,38 @@ namespace SourceHook
|
|||||||
m_Buckets = NULL;
|
m_Buckets = NULL;
|
||||||
m_numBuckets = 0;
|
m_numBuckets = 0;
|
||||||
}
|
}
|
||||||
|
public:
|
||||||
|
template <typename U>
|
||||||
|
V & AltFindOrInsert(const U & ukey)
|
||||||
|
{
|
||||||
|
size_t place = HashAlt(ukey) % m_numBuckets;
|
||||||
|
THashNode *pNode = NULL;
|
||||||
|
if (!m_Buckets[place])
|
||||||
|
{
|
||||||
|
m_Buckets[place] = new List<THashNode *>;
|
||||||
|
pNode = new THashNode(ukey, V());
|
||||||
|
m_Buckets[place]->push_back(pNode);
|
||||||
|
m_percentUsed += (1.0f / (float)m_numBuckets);
|
||||||
|
} else {
|
||||||
|
typename List<THashNode *>::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)
|
THashNode *_FindOrInsert(const K & key)
|
||||||
{
|
{
|
||||||
size_t place = HashFunction(key) % m_numBuckets;
|
size_t place = HashFunction(key) % m_numBuckets;
|
||||||
@ -135,7 +173,9 @@ namespace SourceHook
|
|||||||
m_Buckets[place]->push_back(pNode);
|
m_Buckets[place]->push_back(pNode);
|
||||||
}
|
}
|
||||||
if (PercentUsed() > 0.75f)
|
if (PercentUsed() > 0.75f)
|
||||||
|
{
|
||||||
_Refactor();
|
_Refactor();
|
||||||
|
}
|
||||||
return pNode;
|
return pNode;
|
||||||
}
|
}
|
||||||
void _Refactor()
|
void _Refactor()
|
||||||
@ -457,7 +497,20 @@ namespace SourceHook
|
|||||||
}
|
}
|
||||||
return end();
|
return end();
|
||||||
}
|
}
|
||||||
|
template <typename U>
|
||||||
|
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)
|
iterator erase(iterator where)
|
||||||
{
|
{
|
||||||
where.erase();
|
where.erase();
|
||||||
@ -479,3 +532,4 @@ namespace SourceHook
|
|||||||
};
|
};
|
||||||
|
|
||||||
#endif //_INCLUDE_SH_TINYHASH_H_
|
#endif //_INCLUDE_SH_TINYHASH_H_
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user