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) node->prev = m_Head->prev;
{ node->next = m_Head;
//link in the node as the first item m_Head->prev->next = node;
node->next = m_Head; m_Head->prev = node;
node->prev = m_Head;
m_Head->prev = node;
m_Head->next = node;
} else {
node->prev = m_Head->prev;
node->next = m_Head;
m_Head->prev->next = 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()
{ {
@ -110,7 +131,7 @@ private:
public: public:
class iterator class iterator
{ {
friend class List; friend class List;
public: public:
iterator() iterator()
{ {
@ -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...
{ pNode->prev->next = pNode->next;
m_Head->next = NULL; pNode->next->prev = pNode->prev;
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->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;

File diff suppressed because it is too large Load Diff