sourcehook/sh_stack.h
branchsourcemm-1.6.0
changeset 428 40a9d2f61121
parent 370 4dc28ab1c722
child 429 cadecde4b802
     1.1 --- a/sourcehook/sh_stack.h	Thu Oct 04 19:46:28 2007 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,237 +0,0 @@
     1.4 -/* ======== SourceMM ========
     1.5 -* Copyright (C) 2004-2007 Metamod:Source Development Team
     1.6 -* No warranties of any kind
     1.7 -*
     1.8 -* License: zlib/libpng
     1.9 -*
    1.10 -* Author(s): Pavol "PM OnoTo" Marko
    1.11 -* ============================
    1.12 -*/
    1.13 -
    1.14 -#ifndef __SH_STACK_H__
    1.15 -#define __SH_STACK_H__
    1.16 -
    1.17 -#include <stddef.h>
    1.18 -
    1.19 -#define SH_STACK_DEFAULT_SIZE 4
    1.20 -
    1.21 -namespace SourceHook
    1.22 -{
    1.23 -	// Vector
    1.24 -	template <class T> class CStack
    1.25 -	{
    1.26 -		T *m_Elements;
    1.27 -		size_t m_AllocatedSize;
    1.28 -		size_t m_UsedSize;
    1.29 -	public:
    1.30 -		friend class iterator;
    1.31 -		class iterator
    1.32 -		{
    1.33 -			CStack<T> *m_pParent;
    1.34 -			size_t m_Index;
    1.35 -		public:
    1.36 -			iterator(CStack<T> *pParent, size_t id) : m_pParent(pParent), m_Index(id)
    1.37 -			{
    1.38 -			}
    1.39 -
    1.40 -			iterator(CStack<T> *pParent) : m_pParent(pParent), m_Index(0)
    1.41 -			{
    1.42 -			}
    1.43 -			
    1.44 -			iterator() : m_pParent(NULL), m_Index(0)
    1.45 -			{
    1.46 -			}
    1.47 -
    1.48 -			T &operator *()
    1.49 -			{
    1.50 -				return m_pParent->m_Elements[m_Index];
    1.51 -			}
    1.52 -			const T &operator *() const
    1.53 -			{
    1.54 -				return m_pParent->m_Elements[m_Index];
    1.55 -			}
    1.56 -			
    1.57 -			T * operator->()
    1.58 -			{
    1.59 -				return m_pParent->m_Elements + m_Index;
    1.60 -			}
    1.61 -
    1.62 -			const T * operator->() const
    1.63 -			{
    1.64 -				return m_pParent->m_Elements + m_Index;
    1.65 -			}
    1.66 -
    1.67 -			iterator & operator++()		// preincrement
    1.68 -			{
    1.69 -				++m_Index;
    1.70 -				return (*this);
    1.71 -			}
    1.72 -
    1.73 -			iterator operator++(int)	// postincrement
    1.74 -			{
    1.75 -				iterator tmp = *this;
    1.76 -				++m_Index;
    1.77 -				return tmp;
    1.78 -			}
    1.79 -
    1.80 -			iterator & operator--()		// predecrement
    1.81 -			{
    1.82 -				--m_Index;
    1.83 -				return (*this);
    1.84 -			}
    1.85 -
    1.86 -			iterator operator--(int)	// postdecrememnt
    1.87 -			{
    1.88 -				iterator tmp = *this;
    1.89 -				--m_Index;
    1.90 -				return tmp;
    1.91 -			}
    1.92 -
    1.93 -			bool operator==(const iterator & right) const
    1.94 -			{
    1.95 -				return (m_pParent == right.m_pParent && m_Index == right.m_Index);
    1.96 -			}
    1.97 -
    1.98 -			bool operator!=(const iterator & right) const
    1.99 -			{
   1.100 -				return !(*this == right);
   1.101 -			}
   1.102 -		};
   1.103 -		CStack() : m_Elements(new T[SH_STACK_DEFAULT_SIZE]),
   1.104 -			m_AllocatedSize(SH_STACK_DEFAULT_SIZE),
   1.105 -			m_UsedSize(0)
   1.106 -		{
   1.107 -		}
   1.108 -		CStack(size_t size) : m_Elements(new T[size]),
   1.109 -			m_AllocatedSize(size),
   1.110 -			m_UsedSize(0)
   1.111 -		{
   1.112 -		}
   1.113 -
   1.114 -		CStack(const CStack &other) : m_Elements(NULL),
   1.115 -			m_AllocatedSize(0),
   1.116 -			m_UsedSize(0)
   1.117 -		{
   1.118 -			reserve(other.m_AllocatedSize);
   1.119 -			m_UsedSize = other.m_UsedSize;
   1.120 -			for (size_t i = 0; i < m_UsedSize; ++i)
   1.121 -				m_Elements[i] = other.m_Elements[i];
   1.122 -		}
   1.123 -
   1.124 -		~CStack()
   1.125 -		{
   1.126 -			if (m_Elements)
   1.127 -				delete [] m_Elements;
   1.128 -		}
   1.129 -		
   1.130 -		void operator=(const CStack &other)
   1.131 -		{
   1.132 -			if (m_AllocatedSize < other.m_AllocatedSize)
   1.133 -			{
   1.134 -				if (m_Elements)
   1.135 -					delete [] m_Elements;
   1.136 -				m_Elements = new T[other.m_AllocatedSize];
   1.137 -				m_AllocatedSize = other.m_AllocatedSize;
   1.138 -			}
   1.139 -			m_UsedSize = other.m_UsedSize;
   1.140 -			for (size_t i = 0; i < m_UsedSize; ++i)
   1.141 -				m_Elements[i] = other.m_Elements[i];
   1.142 -		}
   1.143 -
   1.144 -		bool push(const T &val)
   1.145 -		{
   1.146 -			if (m_UsedSize + 1 == m_AllocatedSize)
   1.147 -			{
   1.148 -				// zOHNOES! REALLOCATE!
   1.149 -				m_AllocatedSize *= 2;
   1.150 -				T *newElements = new T[m_AllocatedSize];
   1.151 -				if (!newElements)
   1.152 -				{
   1.153 -					m_AllocatedSize /= 2;
   1.154 -					return false;
   1.155 -				}
   1.156 -				if (m_Elements)
   1.157 -				{
   1.158 -					for (size_t i = 0; i < m_UsedSize; ++i)
   1.159 -						newElements[i] = m_Elements[i];
   1.160 -					delete [] m_Elements;
   1.161 -				}
   1.162 -				m_Elements = newElements;
   1.163 -			}
   1.164 -			m_Elements[m_UsedSize++] = val;
   1.165 -			return true;
   1.166 -		}
   1.167 -
   1.168 -		void pop()
   1.169 -		{
   1.170 -			--m_UsedSize;
   1.171 -		}
   1.172 -
   1.173 -		void popall()
   1.174 -		{
   1.175 -			m_UsedSize = 0;
   1.176 -		}
   1.177 -
   1.178 -		T &front()
   1.179 -		{
   1.180 -			return m_Elements[m_UsedSize - 1];
   1.181 -		}
   1.182 -
   1.183 -		const T &front() const
   1.184 -		{
   1.185 -			return m_Elements[m_UsedSize - 1];
   1.186 -		}
   1.187 -
   1.188 -		T &second()
   1.189 -		{
   1.190 -			return m_Elements[m_UsedSize - 2];
   1.191 -		}
   1.192 -
   1.193 -		const T &second() const
   1.194 -		{
   1.195 -			return m_Elements[m_UsedSize - 2];
   1.196 -		}
   1.197 -
   1.198 -		iterator begin()
   1.199 -		{
   1.200 -			return iterator(this, 0);
   1.201 -		}
   1.202 -		iterator end()
   1.203 -		{
   1.204 -			return iterator(this, m_UsedSize);
   1.205 -		}
   1.206 -
   1.207 -		size_t size()
   1.208 -		{
   1.209 -			return m_UsedSize;
   1.210 -		}
   1.211 -		size_t capacity()
   1.212 -		{
   1.213 -			return m_AllocatedSize;
   1.214 -		}
   1.215 -		bool empty()
   1.216 -		{
   1.217 -			return m_UsedSize == 0 ? true : false;
   1.218 -		}
   1.219 -		bool reserve(size_t size)
   1.220 -		{
   1.221 -			if (size > m_AllocatedSize)
   1.222 -			{
   1.223 -				T *newElements = new T[size];
   1.224 -				if (!newElements)
   1.225 -					return false;
   1.226 -				if (m_Elements)
   1.227 -				{
   1.228 -					for (size_t i = 0; i < m_UsedSize; ++i)
   1.229 -						newElements[i] = m_Elements[i];
   1.230 -					delete [] m_Elements;
   1.231 -				}
   1.232 -				m_Elements = newElements;
   1.233 -				m_AllocatedSize = size;
   1.234 -			}
   1.235 -			return true;
   1.236 -		}
   1.237 -	};
   1.238 -};	//namespace SourceHook
   1.239 -
   1.240 -#endif