mirror of
https://github.com/alliedmodders/amxmodx.git
synced 2024-12-25 06:15:37 +03:00
another synce with SH
This commit is contained in:
parent
7c7422a553
commit
82653094fb
@ -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;
|
||||||
|
@ -38,12 +38,18 @@
|
|||||||
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();
|
||||||
@ -184,14 +190,23 @@
|
|||||||
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;
|
||||||
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user