core/sm_queue.h
author Scott Ehlert <ds@alliedmods.net>
Sun Aug 24 04:35:48 2008 +0000 (2008-08-24)
changeset 2237 4403a982ec91
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
Improved bot detection slightly. This was meant to go along with the fix for amb1801.
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#ifndef _INCLUDE_SM_QUEUE_H
ds@1952
    33
#define _INCLUDE_SM_QUEUE_H
ds@1952
    34
ds@1952
    35
#include <new>
ds@1952
    36
#include <malloc.h>
ds@1952
    37
#include <sh_stack.h>
ds@1952
    38
ds@1952
    39
using namespace SourceHook;
ds@1952
    40
ds@1952
    41
/*
ds@1952
    42
	A circular, doubly-linked List with one sentinel node
ds@1952
    43
ds@1952
    44
	Empty:
ds@1952
    45
	m_Head = sentinel
ds@1952
    46
	m_Head->next = m_Head;
ds@1952
    47
	m_Head->prev = m_Head;
ds@1952
    48
	One element:
ds@1952
    49
	m_Head = sentinel
ds@1952
    50
	m_Head->next = node1
ds@1952
    51
	m_Head->prev = node1
ds@1952
    52
	node1->next = m_Head
ds@1952
    53
	node1->prev = m_Head
ds@1952
    54
	Two elements:
ds@1952
    55
	m_Head = sentinel
ds@1952
    56
	m_Head->next = node1
ds@1952
    57
	m_Head->prev = node2
ds@1952
    58
	node1->next = node2
ds@1952
    59
	node1->prev = m_Head
ds@1952
    60
	node2->next = m_Head
ds@1952
    61
	node2->prev = node1
ds@1952
    62
*/
ds@1952
    63
template <class T>
ds@1952
    64
class Queue
ds@1952
    65
{
ds@1952
    66
public:
ds@1952
    67
	class iterator;
ds@1952
    68
	friend class iterator;
ds@1952
    69
	class QueueNode
ds@1952
    70
	{
ds@1952
    71
	public:
ds@1952
    72
		T obj;
ds@1952
    73
		QueueNode *next;
ds@1952
    74
		QueueNode *prev;
ds@1952
    75
	};
ds@1952
    76
private:
ds@1952
    77
	// Initializes the sentinel node.
ds@1952
    78
	QueueNode *_Initialize()
ds@1952
    79
	{
ds@1952
    80
		QueueNode *n = (QueueNode *)malloc(sizeof(QueueNode));
ds@1952
    81
		n->next = n;
ds@1952
    82
		n->prev = n;
ds@1952
    83
		return n;
ds@1952
    84
	}
ds@1952
    85
public:
ds@1952
    86
	Queue() : m_Head(_Initialize()), m_Size(0)
ds@1952
    87
	{
ds@1952
    88
	}
ds@1952
    89
ds@1952
    90
	Queue(const Queue &src) : m_Head(_Initialize()), m_Size(0)
ds@1952
    91
	{
ds@1952
    92
		iterator iter;
ds@1952
    93
		for (iter=src.begin(); iter!=src.end(); iter++)
ds@1952
    94
		{
ds@1952
    95
			push_back( (*iter) );
ds@1952
    96
		}
ds@1952
    97
	}
ds@1952
    98
ds@1952
    99
	~Queue()
ds@1952
   100
	{
ds@1952
   101
		clear();
ds@1952
   102
ds@1952
   103
		// Don't forget to free the sentinel
ds@1952
   104
		if (m_Head)
ds@1952
   105
		{
ds@1952
   106
			free(m_Head);
ds@1952
   107
			m_Head = NULL;
ds@1952
   108
		}
ds@1952
   109
ds@1952
   110
		while (!m_FreeNodes.empty())
ds@1952
   111
		{
ds@1952
   112
			free(m_FreeNodes.front());
ds@1952
   113
			m_FreeNodes.pop();
ds@1952
   114
		}
ds@1952
   115
	}
ds@1952
   116
ds@1952
   117
	void push(const T &obj)
ds@1952
   118
	{
ds@1952
   119
		QueueNode *node;
ds@1952
   120
		
ds@1952
   121
		if (m_FreeNodes.empty())
ds@1952
   122
		{
ds@1952
   123
			node = (QueueNode *)malloc(sizeof(QueueNode));
ds@1952
   124
		} else {
ds@1952
   125
			node = m_FreeNodes.front();
ds@1952
   126
			m_FreeNodes.pop();
ds@1952
   127
		}
ds@1952
   128
ds@1952
   129
		/* Copy the object */
ds@1952
   130
		new (&node->obj) T(obj);
ds@1952
   131
ds@1952
   132
		/* Install into the Queue */
ds@1952
   133
		node->prev = m_Head->prev;
ds@1952
   134
		node->next = m_Head;
ds@1952
   135
		m_Head->prev->next = node;
ds@1952
   136
		m_Head->prev = node;
ds@1952
   137
ds@1952
   138
		m_Size++;
ds@1952
   139
	}
ds@1952
   140
ds@1952
   141
	size_t size() const
ds@1952
   142
	{
ds@1952
   143
		return m_Size;
ds@1952
   144
	}
ds@1952
   145
ds@1952
   146
	void clear()
ds@1952
   147
	{
ds@1952
   148
		QueueNode *node = m_Head->next;
ds@1952
   149
		QueueNode *temp;
ds@1952
   150
		m_Head->next = m_Head;
ds@1952
   151
		m_Head->prev = m_Head;
ds@1952
   152
ds@1952
   153
		// Iterate through the nodes until we find g_Head (the sentinel) again
ds@1952
   154
		while (node != m_Head)
ds@1952
   155
		{
ds@1952
   156
			temp = node->next;
ds@1952
   157
			node->obj.~T();
ds@1952
   158
			m_FreeNodes.push(node);
ds@1952
   159
			node = temp;
ds@1952
   160
		}
ds@1952
   161
		m_Size = 0;
ds@1952
   162
	}
ds@1952
   163
ds@1952
   164
	bool empty() const
ds@1952
   165
	{
ds@1952
   166
		return (m_Size == 0);
ds@1952
   167
	}
ds@1952
   168
ds@1952
   169
private:
ds@1952
   170
	QueueNode *m_Head;
ds@1952
   171
	size_t m_Size;
ds@1952
   172
	CStack<QueueNode *> m_FreeNodes;
ds@1952
   173
public:
ds@1952
   174
	class iterator
ds@1952
   175
	{
ds@1952
   176
		friend class Queue;
ds@1952
   177
	public:
ds@1952
   178
		iterator()
ds@1952
   179
		{
ds@1952
   180
			m_This = NULL;
ds@1952
   181
		}
ds@1952
   182
		iterator(const Queue &src)
ds@1952
   183
		{
ds@1952
   184
			m_This = src.m_Head;
ds@1952
   185
		}
ds@1952
   186
		iterator(QueueNode *n) : m_This(n)
ds@1952
   187
		{
ds@1952
   188
		}
ds@1952
   189
		iterator(const iterator &where)
ds@1952
   190
		{
ds@1952
   191
			m_This = where.m_This;
ds@1952
   192
		}
ds@1952
   193
		//pre decrement
ds@1952
   194
		iterator & operator--()
ds@1952
   195
		{
ds@1952
   196
			if (m_This)
ds@1952
   197
				m_This = m_This->prev;
ds@1952
   198
			return *this;
ds@1952
   199
		}
ds@1952
   200
		//post decrement
ds@1952
   201
		iterator operator--(int)
ds@1952
   202
		{
ds@1952
   203
			iterator old(*this);
ds@1952
   204
			if (m_This)
ds@1952
   205
				m_This = m_This->prev;
ds@1952
   206
			return old;
ds@1952
   207
		}	
ds@1952
   208
ds@1952
   209
		//pre increment
ds@1952
   210
		iterator & operator++()
ds@1952
   211
		{
ds@1952
   212
			if (m_This)
ds@1952
   213
				m_This = m_This->next;
ds@1952
   214
			return *this;
ds@1952
   215
		}
ds@1952
   216
		//post increment
ds@1952
   217
		iterator operator++(int)
ds@1952
   218
		{
ds@1952
   219
			iterator old(*this);
ds@1952
   220
			if (m_This)
ds@1952
   221
				m_This = m_This->next;
ds@1952
   222
			return old;
ds@1952
   223
		}
ds@1952
   224
ds@1952
   225
		const T & operator * () const
ds@1952
   226
		{
ds@1952
   227
			return m_This->obj;
ds@1952
   228
		}
ds@1952
   229
		T & operator * ()
ds@1952
   230
		{
ds@1952
   231
			return m_This->obj;
ds@1952
   232
		}
ds@1952
   233
ds@1952
   234
		T * operator -> ()
ds@1952
   235
		{
ds@1952
   236
			return &(m_This->obj);
ds@1952
   237
		}
ds@1952
   238
		const T * operator -> () const
ds@1952
   239
		{
ds@1952
   240
			return &(m_This->obj);
ds@1952
   241
		}
ds@1952
   242
ds@1952
   243
		bool operator != (const iterator &where) const
ds@1952
   244
		{
ds@1952
   245
			return (m_This != where.m_This);
ds@1952
   246
		}
ds@1952
   247
		bool operator ==(const iterator &where) const
ds@1952
   248
		{
ds@1952
   249
			return (m_This == where.m_This);
ds@1952
   250
		}
ds@1952
   251
	private:
ds@1952
   252
		QueueNode *m_This;
ds@1952
   253
	};
ds@1952
   254
public:
ds@1952
   255
	iterator begin() const
ds@1952
   256
	{
ds@1952
   257
		return iterator(m_Head->next);
ds@1952
   258
	}
ds@1952
   259
ds@1952
   260
	iterator end() const
ds@1952
   261
	{
ds@1952
   262
		return iterator(m_Head);
ds@1952
   263
	}
ds@1952
   264
ds@1952
   265
	iterator erase(iterator &where)
ds@1952
   266
	{
ds@1952
   267
		QueueNode *pNode = where.m_This;
ds@1952
   268
		iterator iter(where);
ds@1952
   269
		iter++;
ds@1952
   270
ds@1952
   271
ds@1952
   272
		// Works for all cases: empty Queue, erasing first element, erasing tail, erasing in the middle...
ds@1952
   273
		pNode->prev->next = pNode->next;
ds@1952
   274
		pNode->next->prev = pNode->prev;
ds@1952
   275
ds@1952
   276
		pNode->obj.~T();
ds@1952
   277
		m_FreeNodes.push(pNode);
ds@1952
   278
		m_Size--;
ds@1952
   279
ds@1952
   280
		return iter;
ds@1952
   281
	}
ds@1952
   282
public:
ds@1952
   283
	void remove(const T & obj)
ds@1952
   284
	{
ds@1952
   285
		iterator b;
ds@1952
   286
		for (b=begin(); b!=end(); b++)
ds@1952
   287
		{
ds@1952
   288
			if ( (*b) == obj )
ds@1952
   289
			{
ds@1952
   290
				erase( b );
ds@1952
   291
				break;
ds@1952
   292
			}
ds@1952
   293
		}
ds@1952
   294
	}
ds@1952
   295
	template <typename U>
ds@1952
   296
	iterator find(const U & equ) const
ds@1952
   297
	{
ds@1952
   298
		iterator iter;
ds@1952
   299
		for (iter=begin(); iter!=end(); iter++)
ds@1952
   300
		{
ds@1952
   301
			if ( (*iter) == equ )
ds@1952
   302
			{
ds@1952
   303
				return iter;
ds@1952
   304
			}
ds@1952
   305
		}
ds@1952
   306
		return end();
ds@1952
   307
	}
ds@1952
   308
	Queue & operator =(const Queue &src)
ds@1952
   309
	{
ds@1952
   310
		clear();
ds@1952
   311
		iterator iter;
ds@1952
   312
		for (iter=src.begin(); iter!=src.end(); iter++)
ds@1952
   313
		{
ds@1952
   314
			push_back( (*iter) );
ds@1952
   315
		}
ds@1952
   316
		return *this;
ds@1952
   317
	}
ds@1952
   318
public:
ds@1952
   319
	T & first() const
ds@1952
   320
	{
ds@1952
   321
		iterator i = begin();
ds@1952
   322
ds@1952
   323
		return (*i);
ds@1952
   324
	}
ds@1952
   325
ds@1952
   326
	void pop()
ds@1952
   327
	{
ds@1952
   328
		iterator iter = begin();
ds@1952
   329
		erase(iter);
ds@1952
   330
	}
ds@1952
   331
};
ds@1952
   332
ds@1952
   333
#endif //_INCLUDE_SM_QUEUE_H