knight/shared/KeVector.h
changeset 2226 d10b38524be3
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/knight/shared/KeVector.h	Fri Aug 15 05:22:26 2008 +0000
     1.3 @@ -0,0 +1,158 @@
     1.4 +#ifndef _INCLUDE_KNIGHT_KE_VECTOR_H_
     1.5 +#define _INCLUDE_KNIGHT_KE_VECTOR_H_
     1.6 +
     1.7 +#include <new>
     1.8 +
     1.9 +namespace Knight
    1.10 +{
    1.11 +	template <class T>
    1.12 +	class KeVector
    1.13 +	{
    1.14 +	public:
    1.15 +		KeVector<T>() : m_Data(NULL), m_Size(0), m_CurrentUsedSize(0)
    1.16 +		{
    1.17 +		}
    1.18 +
    1.19 +		KeVector<T>(const KeVector<T> & other)
    1.20 +		{
    1.21 +			m_Size = other.m_CurrentUsedSize;
    1.22 +			m_CurrentUsedSize = other.m_CurrentUsedSize;
    1.23 +
    1.24 +			if (m_Size > 0)
    1.25 +			{
    1.26 +				m_Data = new T[other.m_CurrentUsedSize];
    1.27 +
    1.28 +				for (size_t i = 0; i < m_Size; i++)
    1.29 +				{
    1.30 +					m_Data[i] = other.m_Data[i];
    1.31 +				}
    1.32 +			}
    1.33 +			else
    1.34 +			{
    1.35 +				m_Data = NULL;
    1.36 +			}
    1.37 +		}
    1.38 +
    1.39 +		~KeVector<T>()
    1.40 +		{
    1.41 +			clear();
    1.42 +		}
    1.43 +
    1.44 +		KeVector & operator =(const KeVector<T> & other)
    1.45 +		{
    1.46 +			clear();
    1.47 +
    1.48 +			if (other.m_CurrentUsedSize)
    1.49 +			{
    1.50 +				m_Data = new T[other.m_CurrentUsedSize];
    1.51 +				m_Size = other.m_CurrentUsedSize;
    1.52 +				m_CurrentUsedSize = other.m_CurrentUsedSize;
    1.53 +
    1.54 +				for (size_t i = 0; i < m_Size; i++)
    1.55 +				{
    1.56 +					m_Data[i] = other.m_Data[i];
    1.57 +				}
    1.58 +			}
    1.59 +		}
    1.60 +
    1.61 +		size_t size() const
    1.62 +		{
    1.63 +			return m_CurrentUsedSize;
    1.64 +		}
    1.65 +
    1.66 +		void push_back(const T & elem)
    1.67 +		{
    1.68 +			GrowIfNeeded(1);
    1.69 +
    1.70 +			new (&m_Data[m_CurrentUsedSize]) T(elem);
    1.71 +
    1.72 +			m_CurrentUsedSize++;
    1.73 +		}
    1.74 +
    1.75 +		void pop_back(const T & elem)
    1.76 +		{
    1.77 +			if (m_CurrentUsedSize == 0)
    1.78 +			{
    1.79 +				return;
    1.80 +			}
    1.81 +
    1.82 +			m_CurrentUsedSize--;
    1.83 +			m_Data[m_CurrentUsedSize].~T();
    1.84 +		}
    1.85 +
    1.86 +		bool is_empty()
    1.87 +		{
    1.88 +			return (m_CurrentUsedSize == 0);
    1.89 +		}
    1.90 +
    1.91 +		T & operator [](size_t pos)
    1.92 +		{
    1.93 +			return m_Data[pos];
    1.94 +		}
    1.95 +
    1.96 +		const T & operator [](size_t pos) const
    1.97 +		{
    1.98 +			return m_Data[pos];
    1.99 +		}
   1.100 +
   1.101 +		void clear()
   1.102 +		{
   1.103 +			for (size_t i = 0; i < m_CurrentUsedSize; i++)
   1.104 +			{
   1.105 +				m_Data[i].~T();
   1.106 +			}
   1.107 +
   1.108 +			free(m_Data);
   1.109 +			
   1.110 +			m_Data = NULL;
   1.111 +			m_Size = 0;
   1.112 +			m_CurrentUsedSize = 0;
   1.113 +		}
   1.114 +	private:
   1.115 +		void Grow(size_t amount)
   1.116 +		{
   1.117 +			T *new_data;
   1.118 +			size_t new_size;
   1.119 +
   1.120 +			if (m_Size == 0)
   1.121 +			{
   1.122 +				new_size = 8;
   1.123 +			}
   1.124 +			else
   1.125 +			{
   1.126 +				new_size = m_Size * 2;
   1.127 +			}
   1.128 +
   1.129 +			while (m_CurrentUsedSize + amount > new_size)
   1.130 +			{
   1.131 +				new_size *= 2;
   1.132 +			}
   1.133 +
   1.134 +			new_data = (T *)malloc(sizeof(T) * new_size);
   1.135 +
   1.136 +			for (size_t i = 0; i < m_CurrentUsedSize; i++)
   1.137 +			{
   1.138 +				new (&new_data[i]) T(m_Data[i]);
   1.139 +				m_Data[i].~T();
   1.140 +			}
   1.141 +
   1.142 +			free(m_Data);
   1.143 +			m_Data = new_data;
   1.144 +			m_Size = new_size;
   1.145 +		}
   1.146 +
   1.147 +		void GrowIfNeeded(size_t amount)
   1.148 +		{
   1.149 +			if (m_CurrentUsedSize + amount >= m_Size)
   1.150 +			{
   1.151 +				Grow(amount);
   1.152 +			}
   1.153 +		}
   1.154 +	private:
   1.155 +		T *m_Data;
   1.156 +		size_t m_Size;
   1.157 +		size_t m_CurrentUsedSize;
   1.158 +	};
   1.159 +}
   1.160 +
   1.161 +#endif //_INCLUDE_KNIGHT_KE_VECTOR_H_