
template<class _t> class DynVector
{
	_t *Data;
	unsigned int Size;
public:
	DynVector()
	{
		Data = 0;
		Size = 0;
	}
	DynVector(const DynVector& d)
	{
		Data = (_t *)malloc(d.Size);
		memcpy(Data, d.Data, d.Size);
		Size = d.Size;
	}
	~DynVector()
	{
		free(Data);
		Data = 0;
		Size = 0;
	}

	DynVector &operator =(const DynVector& d)
	{
		Data = realloc(Data, d.Size); //Safe even if Data is NULL
		memcpy(Data, d.Data);
		Size = d.Size;
		return *this;
	}

	void Push(_t Item)
	{
		Data = (_t *)realloc((void *)Data, Size + sizeof(_t));
		memcpy((void *)(Data + (Size / sizeof(_t))), (void *)&Item, sizeof(_t));
		Size += sizeof(_t);
	}

	void Push(_t *Item)
	{
		if(!Item) return;
		Data = (_t *)realloc((void *)Data, Size + sizeof(_t));
		memcpy((void *)(Data + (Size / sizeof(_t))), (void *)Item, sizeof(_t));
		Size += sizeof(_t);
	}

	void Delete(signed int Index)
	{
		if(!Data) return;
		if(Index < 0 && (signed int)(Size + (Index * sizeof(_t))) > (signed int)(0 - sizeof(_t)))
		{
			unsigned int _Index = (Size / sizeof(_t)) + Index;
			if(_Index * sizeof(_t) != Size) //nothing to shift if poping off the end
			{
				memcpy((void *)(Data + _Index), (void *)(Data + _Index + 1), Size - ((_Index + 1) * sizeof(_t)));    //shift the array down 1
			}
			Size -= sizeof(_t); //shrink the class
			Data = (_t *)realloc((void *)Data, Size); //shrink the array
			
			return;
		}

		if(Index >= 0 && (signed int)(Size - (Index * sizeof(_t))) > 0)
		{
			if(Index * sizeof(_t) != Size) //nothing to shift if poping off the end
			{
				memcpy((void *)(Data + Index), (void *)(Data + Index + 1), Size - ((Index + 1) * sizeof(_t)));    //shift the array down 1
			}
			Size -= sizeof(_t); //shrink the class
			Data = (_t *)realloc((void *)Data, Size);//shrink the array
			
			return;
		}
	}

	_t *Get(signed int Index)
	{
		if(!Data) return 0;
		if(Index < 0 && Size + (Index * sizeof(_t)) > (0 - sizeof(_t)))
		{
			unsigned int _Index = (Size / sizeof(_t)) + Index;
			return Data + _Index;
		}

		if(Index >= 0 && Size - (Index * sizeof(_t)) > 0)
		{
			return Data + Index;
		}
		return 0;
	}
	void Clear()
	{
		free((void *)Data);
		Data = 0;
		Size = 0;
	}
};