core/sm_fastlink.h
author Phong Dang <teame06@gmail.com>
Wed Aug 01 04:17:36 2007 +0000 (2007-08-01)
changeset 1235 88bb46f9731e
parent 1231 17badaf66ce4
child 1330 ab94316bb52b
permissions -rw-r--r--
Implemented requested amb603. Added voice natives.
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #ifndef _INCLUDE_SOURCEMOD_FASTLINK_H_
    34 #define _INCLUDE_SOURCEMOD_FASTLINK_H_
    35 
    36 #include <assert.h>
    37 
    38 template <typename T>
    39 class FastLink
    40 {
    41 	friend class iterator;
    42 public:
    43 	struct FastLinkNode
    44 	{
    45 		unsigned int prev;
    46 		unsigned int next;
    47 		unsigned int freeNode;
    48 		T obj;
    49 	};
    50 public:
    51 	FastLink(unsigned int maxsize) : m_Size(0), m_FirstLink(0), m_FreeNodes(0), m_LastLink(0), m_UsableIndex(0)
    52 	{
    53 		m_MaxSize = maxsize;
    54 		m_Nodes = new FastLinkNode[m_MaxSize+1];
    55 	}
    56 public:
    57 	bool push_back(const T & obj)
    58 	{
    59 		unsigned int new_node = GetFreeIndex();
    60 		if (!new_node)
    61 		{
    62 			return false;
    63 		}
    64 		m_Nodes[new_node].obj = obj;
    65 		m_Nodes[new_node].next = 0;
    66 		if (!m_FirstLink)
    67 		{
    68 			m_Nodes[new_node].prev = 0;
    69 			m_FirstLink = new_node;
    70 			m_LastLink = new_node;
    71 		} else {
    72 			m_Nodes[new_node].prev = m_LastLink;
    73 			m_Nodes[m_LastLink].next = new_node;
    74 			m_LastLink = new_node;
    75 		}
    76 		m_Size++;
    77 		return true;
    78 	}
    79 	size_t size() const
    80 	{
    81 		return m_Size;
    82 	}
    83 private:
    84 	unsigned int GetFreeIndex()
    85 	{
    86 		if (m_FreeNodes)
    87 		{
    88 			return m_Nodes[m_FreeNodes--].freeNode;
    89 		} else {
    90 			if (m_UsableIndex >= m_MaxSize)
    91 			{
    92 				return 0;
    93 			}
    94 			m_UsableIndex++;
    95 			return m_UsableIndex;
    96 		}
    97 	}
    98 public:
    99 	class iterator
   100 	{
   101 		friend class FastLink;
   102 	public:
   103 		iterator()
   104 		{
   105 			link = NULL;
   106 			position = 0;
   107 		}
   108 		iterator(const FastLink *_link, unsigned int _position)
   109 		{
   110 			link = _link;
   111 			position = _position;
   112 		}
   113 	public:
   114 		bool operator ==(const iterator &where) const
   115 		{
   116 			return (link == where.link && position == where.position);
   117 		}
   118 		bool operator !=(const iterator &where) const
   119 		{
   120 			return (link != where.link || position != where.position);
   121 		}
   122 		T & operator *()
   123 		{
   124 			return link->m_Nodes[position].obj;
   125 		}
   126 		const T & operator *() const
   127 		{
   128 			return link->m_Nodes[position].obj;
   129 		}
   130 		iterator & operator++()
   131 		{
   132 			position = link->m_Nodes[position].next;
   133 			return *this;
   134 		}
   135 	private:
   136 		const FastLink *link;
   137 		unsigned int position;
   138 	};
   139 public:
   140 	iterator begin() const
   141 	{
   142 		return iterator(this, m_FirstLink);
   143 	}
   144 	iterator end() const
   145 	{
   146 		return iterator(this, 0);
   147 	}
   148 	void remove(const T & obj)
   149 	{
   150 		for (iterator iter=begin(); iter!=end(); ++iter)
   151 		{
   152 			if ((*iter) == obj)
   153 			{
   154 				erase(iter);
   155 				return;
   156 			}
   157 		}
   158 	}
   159 	iterator erase(const iterator &where)
   160 	{
   161 		/* Whoa, this better be right! */
   162 		assert(where.link == this);
   163 
   164 		iterator iter(where.link, where.position);
   165 		++iter;
   166 
   167 		unsigned int index = where.position;
   168 
   169 		/* Each case is different, we have no sentinel.
   170 		 * CASE: We're the HEAD AND the TAIL */
   171 		if (index == m_FirstLink
   172 			&& index == m_LastLink)
   173 		{
   174 			m_FirstLink = 0;
   175 			m_LastLink = 0;
   176 		}
   177 		/* We're the HEAD */
   178 		else if (index == m_FirstLink)
   179 		{
   180 			m_FirstLink = m_Nodes[index].next;
   181 			m_Nodes[m_FirstLink].prev = 0;
   182 		}
   183 		/* We're the TAIL */
   184 		else if (index == m_LastLink)
   185 		{
   186 			m_LastLink = m_Nodes[index].prev;
   187 			m_Nodes[m_LastLink].next = 0;
   188 		}
   189 		/* We're in the middle! */
   190 		else
   191 		{
   192 			/* Patch forward reference */
   193 			m_Nodes[m_Nodes[index].next].prev = m_Nodes[index].prev;
   194 			/* Patch backward reference */
   195 			m_Nodes[m_Nodes[index].prev].next = m_Nodes[index].next;
   196 		}
   197 
   198 		/* Add us to the free list */
   199 		m_Nodes[++m_FreeNodes].freeNode = index;
   200 
   201 		m_Size--;
   202 
   203 		return iter;
   204 	}
   205 #if defined MENU_DEBUG
   206 public:
   207 #else
   208 private:
   209 #endif
   210 	size_t m_Size;
   211 	unsigned int m_FirstLink;
   212 	unsigned int m_FreeNodes;
   213 	unsigned int m_LastLink;
   214 	unsigned int m_MaxSize;
   215 	unsigned int m_UsableIndex;
   216 	FastLinkNode *m_Nodes;
   217 };
   218 
   219 #endif //_INCLUDE_SOURCEMOD_FASTLINK_H_