Fixed bug in prune()

Synced changes from SourceHook
This commit is contained in:
David Anderson 2005-11-20 20:54:20 +00:00
parent 3324d65f3f
commit 6f2121cd4d
3 changed files with 181 additions and 11 deletions

View File

@ -2026,7 +2026,7 @@ typedef void (*PFN_MERGEDEFINITION_FILE) (const char * /*filename*/);
typedef const char * (*PFN_FORMAT) (const char * /*fmt*/, ... /*params*/); typedef const char * (*PFN_FORMAT) (const char * /*fmt*/, ... /*params*/);
typedef void (*PFN_REGISTERFUNCTION) (void * /*pfn*/, const char * /*desc*/); typedef void (*PFN_REGISTERFUNCTION) (void * /*pfn*/, const char * /*desc*/);
typedef int (*PFN_AMX_PUSH) (AMX * /*amx*/, cell /*value*/); typedef int (*PFN_AMX_PUSH) (AMX * /*amx*/, cell /*value*/);
typedef int (*PFN_SET_TEAM_INFO) (int /*player */, int /*teamid */, const char */*name */); typedef int (*PFN_SET_TEAM_INFO) (int /*player */, int /*teamid */, const char * /*name */);
extern PFN_ADD_NATIVES g_fn_AddNatives; extern PFN_ADD_NATIVES g_fn_AddNatives;
extern PFN_BUILD_PATHNAME g_fn_BuildPathname; extern PFN_BUILD_PATHNAME g_fn_BuildPathname;

View File

@ -11,8 +11,8 @@
#ifndef _INCLUDE_SMM_LIST_H #ifndef _INCLUDE_SMM_LIST_H
#define _INCLUDE_SMM_LIST_H #define _INCLUDE_SMM_LIST_H
#include <new>
#include <malloc.h> #include <malloc.h>
#include <string.h>
//namespace SourceHook //namespace SourceHook
//{ //{
@ -127,12 +127,22 @@ public:
{ {
m_This = where.m_This; m_This = where.m_This;
} }
//pre decrement
iterator & operator--() iterator & operator--()
{ {
if (m_This) if (m_This)
m_This = m_This->prev; m_This = m_This->prev;
return *this; return *this;
} }
//post decrement
iterator operator--(int)
{
iterator old(*this);
if (m_This)
m_This = m_This->prev;
return old;
}
//pre increment //pre increment
iterator & operator++() iterator & operator++()
{ {
@ -148,7 +158,8 @@ public:
m_This = m_This->next; m_This = m_This->next;
return old; return old;
} }
T & operator * () const
const T & operator * () const
{ {
return m_This->obj; return m_This->obj;
} }
@ -156,10 +167,16 @@ public:
{ {
return m_This->obj; return m_This->obj;
} }
T * operator -> () const
T * operator -> ()
{ {
return &(m_This->obj); return &(m_This->obj);
} }
const T * operator -> () const
{
return &(m_This->obj);
}
bool operator != (const iterator &where) const bool operator != (const iterator &where) const
{ {
return (m_This != where.m_This); return (m_This != where.m_This);
@ -239,6 +256,7 @@ public:
} }
List & operator =(List &src) List & operator =(List &src)
{ {
clear();
iterator iter; iterator iter;
for (iter=src.begin(); iter!=src.end(); iter++) for (iter=src.begin(); iter!=src.end(); iter++)
push_back( (*iter) ); push_back( (*iter) );

View File

@ -44,10 +44,25 @@
}; };
typedef List<THashNode *> * NodePtr; typedef List<THashNode *> * NodePtr;
public: public:
THash() : m_numBuckets(0), m_Buckets(NULL), m_Items(0) THash() : m_Buckets(NULL), m_numBuckets(0), m_percentUsed(0.0f), m_Items(0)
{ {
_Refactor(); _Refactor();
} }
THash(const THash &other) : m_Buckets(new NodePtr[other.numBuckets]),
m_numBuckets(other.m_numBuckets),
m_percentUsed(other.m_percentUsed)
{
for (size_t i=0; i<m_numBuckets; i++)
m_Buckets[i] = NULL;
for (const_iterator iter=other.begin(); iter != other.end(); ++iter)
_FindOrInsert(iter->key)->val = iter->val;
}
void operator=(const THash &other)
{
clear();
for (const_iterator iter=other.begin(); iter!=other.end(); ++iter)
_FindOrInsert(iter->key)->val = iter->val;
}
~THash() ~THash()
{ {
_Clear(); _Clear();
@ -106,6 +121,7 @@
if (Compare(key, (*iter)->key) == 0) if (Compare(key, (*iter)->key) == 0)
{ {
iter = m_Buckets[place]->erase(iter); iter = m_Buckets[place]->erase(iter);
m_Items--;
return; return;
} }
} }
@ -117,6 +133,8 @@
size_t removed = 0; size_t removed = 0;
for (size_t i=0; i<m_numBuckets; i++) for (size_t i=0; i<m_numBuckets; i++)
{ {
if (!m_Buckets[i])
continue;
iter = m_Buckets[i]->begin(); iter = m_Buckets[i]->begin();
bool remove; bool remove;
while (iter != m_Buckets[i]->end()) while (iter != m_Buckets[i]->end())
@ -143,6 +161,7 @@
} }
} }
} }
m_Items -= removed;
return removed; return removed;
} }
size_t Size() size_t Size()
@ -188,6 +207,7 @@
pNode = new THashNode(key, V()); pNode = new THashNode(key, V());
m_Buckets[place]->push_back(pNode); m_Buckets[place]->push_back(pNode);
m_percentUsed += (1.0f / (float)m_numBuckets); m_percentUsed += (1.0f / (float)m_numBuckets);
m_Items++;
} else { } else {
typename List<THashNode *>::iterator iter; typename List<THashNode *>::iterator iter;
for (iter=m_Buckets[place]->begin(); iter!=m_Buckets[place]->end(); iter++) for (iter=m_Buckets[place]->begin(); iter!=m_Buckets[place]->end(); iter++)
@ -198,10 +218,10 @@
//node does not exist //node does not exist
pNode = new THashNode(key, V()); pNode = new THashNode(key, V());
m_Buckets[place]->push_back(pNode); m_Buckets[place]->push_back(pNode);
m_Items++;
} }
if (PercentUsed() > 0.75f) if (PercentUsed() > 0.75f)
_Refactor(); _Refactor();
m_Items++;
return pNode; return pNode;
} }
void _Refactor() void _Refactor()
@ -254,14 +274,15 @@
} }
public: public:
friend class iterator; friend class iterator;
friend class const_iterator;
class iterator class iterator
{ {
friend class THash; friend class THash;
public: public:
iterator() : hash(NULL), end(true) iterator() : curbucket(-1), hash(NULL), end(true)
{ {
}; };
iterator(THash *h) : hash(h), end(false) iterator(THash *h) : curbucket(-1), hash(h), end(false)
{ {
if (!h->m_Buckets) if (!h->m_Buckets)
end = true; end = true;
@ -281,7 +302,7 @@
_Inc(); _Inc();
return old; return old;
} }
THashNode & operator * () const const THashNode & operator * () const
{ {
return *(*iter); return *(*iter);
} }
@ -289,7 +310,7 @@
{ {
return *(*iter); return *(*iter);
} }
THashNode * operator ->() const const THashNode * operator ->() const
{ {
return (*iter); return (*iter);
} }
@ -316,7 +337,7 @@
private: private:
void _Inc() void _Inc()
{ {
if (end || !hash || curbucket >= (int)hash->m_numBuckets) if (end || !hash || curbucket >= static_cast<int>(hash->m_numBuckets))
return; return;
if (curbucket < 0) if (curbucket < 0)
{ {
@ -361,6 +382,114 @@
THash *hash; THash *hash;
bool end; bool end;
}; };
class const_iterator
{
friend class THash;
public:
const_iterator() : curbucket(-1), hash(NULL), end(true)
{
};
const_iterator(const THash *h) : curbucket(-1), hash(h), end(true)
{
if (!h->m_Buckets)
end = true;
else
_Inc();
};
const_iterator & operator++()
{
_Inc();
return *this;
}
const_iterator operator++(int)
{
iterator old(*this);
_Inc();
return old;
}
void erase()
{
if (end || !hash || curbucket < 0 || curbucket >= static_cast<int>(hash->m_numBuckets))
return true;
iterator tmp = *this;
++tmp;
hash->m_Items--;
hash->m_Buckets[curbucket]->erase(iter);
*this = tmp;
}
const THashNode & operator *() const
{
return *(*iter);
}
const THashNode * operator ->() const
{
return (*iter);
}
bool operator ==(const const_iterator &where) const
{
if (where.hash == this->hash
&& where.end == this->end
&&
(this->end ||
((where.curbucket == this->curbucket)
&& (where.iter == iter))
))
return true;
return false;
}
bool operator !=(const const_iterator &where) const
{
return !((*this)==where);
}
private:
void _Inc()
{
if (end || !hash || curbucket >= static_cast<int>(hash->m_numBuckets))
return;
if (curbucket < 0)
{
for (int i=0; i<(int)hash->m_numBuckets; i++)
{
if (hash->m_Buckets[i])
{
iter = hash->m_Buckets[i]->begin();
if (iter == hash->m_Buckets[i]->end())
continue;
curbucket = i;
break;
}
}
if (curbucket < 0)
end = true;
} else {
if (iter != hash->m_Buckets[curbucket]->end())
iter++;
if (iter == hash->m_Buckets[curbucket]->end())
{
int oldbucket = curbucket;
for (int i=curbucket+1; i<(int)hash->m_numBuckets; i++)
{
if (hash->m_Buckets[i])
{
iter = hash->m_Buckets[i]->begin();
if (iter == hash->m_Buckets[i]->end())
continue;
curbucket = i;
break;
}
}
if (curbucket == oldbucket)
end = true;
}
}
}
private:
int curbucket;
typename List<THashNode *>::iterator iter;
const THash *hash;
bool end;
};
public: public:
iterator begin() iterator begin()
{ {
@ -372,6 +501,29 @@
iter.hash = this; iter.hash = this;
return iter; return iter;
} }
const_iterator begin() const
{
return const_iterator(this);
}
const_iterator end() const
{
const_iterator iter;
iter.hash = this;
return iter;
}
iterator erase(iterator where)
{
where.erase();
return where;
}
template <typename U>
void erase(const U & u)
{
iterator iter = find(u);
if (iter == end())
return;
iter.erase();
}
template <typename U> template <typename U>
iterator find(const U & u) const iterator find(const U & u) const
{ {