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