another synce with SH

This commit is contained in:
Borja Ferrer 2006-01-24 22:51:29 +00:00
parent 7c7422a553
commit 82653094fb
2 changed files with 579 additions and 541 deletions

View File

@ -17,6 +17,28 @@
//namespace SourceHook //namespace SourceHook
//{ //{
//This class is from CSDM for AMX Mod X //This class is from CSDM for AMX Mod X
/*
A circular, doubly-linked list with one sentinel node
Empty:
m_Head = sentinel
m_Head->next = m_Head;
m_Head->prev = m_Head;
One element:
m_Head = sentinel
m_Head->next = node1
m_Head->prev = node1
node1->next = m_Head
node1->prev = m_Head
Two elements:
m_Head = sentinel
m_Head->next = node1
m_Head->prev = node2
node1->next = node2
node1->prev = m_Head
node2->next = m_Head
node2->prev = node1
*/
template <class T> template <class T>
class List class List
{ {
@ -33,11 +55,13 @@ public:
ListNode *prev; ListNode *prev;
}; };
private: private:
// Initializes the sentinel node.
// BAIL used malloc instead of new in order to bypass the need for a constructor.
ListNode *_Initialize() ListNode *_Initialize()
{ {
ListNode *n = (ListNode *)malloc(sizeof(ListNode)); ListNode *n = (ListNode *)malloc(sizeof(ListNode));
n->next = NULL; n->next = n;
n->prev = NULL; n->prev = n;
return n; return n;
} }
public: public:
@ -53,6 +77,8 @@ public:
~List() ~List()
{ {
clear(); clear();
// Don't forget to free the sentinel
if (m_Head) if (m_Head)
{ {
free(m_Head); free(m_Head);
@ -63,32 +89,27 @@ public:
{ {
ListNode *node = new ListNode(obj); ListNode *node = new ListNode(obj);
if (!m_Head->prev)
{
//link in the node as the first item
node->next = m_Head;
node->prev = m_Head;
m_Head->prev = node;
m_Head->next = node;
} else {
node->prev = m_Head->prev; node->prev = m_Head->prev;
node->next = m_Head; node->next = m_Head;
m_Head->prev->next = node; m_Head->prev->next = node;
m_Head->prev = node; m_Head->prev = node;
}
m_Size++; m_Size++;
} }
size_t size() size_t size()
{ {
return m_Size; return m_Size;
} }
void clear() void clear()
{ {
ListNode *node = m_Head->next; ListNode *node = m_Head->next;
ListNode *temp; ListNode *temp;
m_Head->next = NULL; m_Head->next = m_Head;
m_Head->prev = NULL; m_Head->prev = m_Head;
while (node && node != m_Head)
// Iterate through the nodes until we find g_Head (the sentinel) again
while (node != m_Head)
{ {
temp = node->next; temp = node->next;
delete node; delete node;
@ -98,7 +119,7 @@ public:
} }
bool empty() bool empty()
{ {
return (m_Head->next == NULL); return (m_Size == 0);
} }
T & back() T & back()
{ {
@ -191,10 +212,7 @@ public:
public: public:
iterator begin() const iterator begin() const
{ {
if (m_Size)
return iterator(m_Head->next); return iterator(m_Head->next);
else
return iterator(m_Head);
} }
iterator end() const iterator end() const
{ {
@ -206,30 +224,34 @@ public:
iterator iter(where); iterator iter(where);
iter++; iter++;
//If we are both the head and tail...
if (m_Head->next == pNode && m_Head->prev == pNode) // Works for all cases: empty list, erasing first element, erasing tail, erasing in the middle...
{
m_Head->next = NULL;
m_Head->prev = NULL;
} else if (m_Head->next == pNode) {
//we are only the first
pNode->next->prev = m_Head;
m_Head->next = pNode->next;
} else if (m_Head->prev == pNode) {
//we are the tail
pNode->prev->next = m_Head;
m_Head->prev = pNode->prev;
} else {
//middle unlink
pNode->prev->next = pNode->next; pNode->prev->next = pNode->next;
pNode->next->prev = pNode->prev; pNode->next->prev = pNode->prev;
}
delete pNode; delete pNode;
m_Size--; m_Size--;
return iter; return iter;
} }
iterator insert(iterator where, const T &obj)
{
// Insert obj right before where
ListNode *node = new ListNode(obj);
ListNode *pWhereNode = where.m_This;
pWhereNode->prev->next = node;
node->prev = pWhereNode->prev;
pWhereNode->prev = node;
node->next = pWhereNode;
m_Size++;
return iterator(node);
}
public: public:
void remove(const T & obj) void remove(const T & obj)
{ {
@ -254,7 +276,7 @@ public:
} }
return end(); return end();
} }
List & operator =(List &src) List & operator =(const List &src)
{ {
clear(); clear();
iterator iter; iterator iter;

View File

@ -18,32 +18,38 @@
//namespace SourceHook //namespace SourceHook
//{ //{
template <class K> template <class K>
int HashFunction(const K & k); int HashFunction(const K & k);
template <class K> template <class K>
int Compare(const K & k1, const K & k2); int Compare(const K & k1, const K & k2);
/** /**
* This is a tiny, growable hash class. * This is a tiny, growable hash class.
* Meant for quick and dirty dictionaries only! * Meant for quick and dirty dictionaries only!
*/ */
template <class K, class V> template <class K, class V>
class THash class THash
{ {
public: public:
struct THashNode struct THashNode
{ {
THashNode(const K & k, const V & v) : THashNode(const K & k, const V & v) :
key(k), val(v) key(k), val(v)
{ {
}; };
THashNode & operator =(const THashNode &other)
{
key = other.key;
val = other.val;
}
K key; K key;
V val; V val;
time_t stamp; time_t stamp;
}; };
typedef List<THashNode *> * NodePtr; typedef List<THashNode *> * NodePtr;
public: public:
class const_iterator;
THash() : m_Buckets(NULL), m_numBuckets(0), m_percentUsed(0.0f), m_Items(0) THash() : m_Buckets(NULL), m_numBuckets(0), m_percentUsed(0.0f), m_Items(0)
{ {
_Refactor(); _Refactor();
@ -181,17 +187,26 @@
THashNode *pNode = _FindOrInsert(key); THashNode *pNode = _FindOrInsert(key);
return pNode->val; return pNode->val;
} }
private: private:
void _Clear() void _Clear()
{ {
typename List<THashNode *>::iterator iter, end;
for (size_t i=0; i<m_numBuckets; i++) for (size_t i=0; i<m_numBuckets; i++)
{ {
if (m_Buckets[i]) if (m_Buckets[i])
{ {
end = m_Buckets[i]->end();
iter = m_Buckets[i]->begin();
while (iter != end)
{
delete (*iter);
iter++;
}
m_Buckets[i]->clear(); m_Buckets[i]->clear();
delete m_Buckets[i]; delete m_Buckets[i];
} }
} }
if (m_Buckets)
delete [] m_Buckets; delete [] m_Buckets;
m_numBuckets = 0; m_numBuckets = 0;
m_Items = 0; m_Items = 0;
@ -272,7 +287,7 @@
m_Buckets = temp; m_Buckets = temp;
} }
} }
public: public:
friend class iterator; friend class iterator;
friend class const_iterator; friend class const_iterator;
class iterator class iterator
@ -414,6 +429,7 @@
iterator tmp = *this; iterator tmp = *this;
++tmp; ++tmp;
delete (*iter);
hash->m_Items--; hash->m_Items--;
hash->m_Buckets[curbucket]->erase(iter); hash->m_Buckets[curbucket]->erase(iter);
*this = tmp; *this = tmp;
@ -490,7 +506,7 @@
const THash *hash; const THash *hash;
bool end; bool end;
}; };
public: public:
iterator begin() iterator begin()
{ {
return iterator(this); return iterator(this);
@ -548,12 +564,12 @@
} }
return end(); return end();
} }
private: private:
NodePtr *m_Buckets; NodePtr *m_Buckets;
size_t m_numBuckets; size_t m_numBuckets;
float m_percentUsed; float m_percentUsed;
size_t m_Items; size_t m_Items;
}; };
//}; //};
#endif //_INCLUDE_SH_TINYHASH_H_ #endif //_INCLUDE_SH_TINYHASH_H_