#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
template <typename T>
class Vector
{
private:
T* m_startAddr;
int m_dataCount;
int m_dataMaxCount;
public:
Vector()
:
m_startAddr(nullptr),
m_dataCount(0),
m_dataMaxCount(2)
{
m_startAddr = new T[2];
}
~Vector()
{
delete[] m_startAddr;
}
public:
void PushDataBack(const T& data);
void Resize(const int& resize);
T* Data() { return m_startAddr; }
int Size() { return m_dataCount; }
int Capacity() { return m_dataMaxCount; }
class iterator;
iterator begin();
iterator end();
iterator erase(iterator& eraseIter);
T operator [] (const int indexNumber)
{
if (-1 == indexNumber && indexNumber >= m_dataCount || nullptr == m_startAddr)
assert(nullptr);
return *(m_startAddr + indexNumber);
}
public:
class iterator
{
private:
Vector<T>* m_i_vectorAddr;
T* m_i_startAddr;
int m_i_indexNumber;
bool m_i_valid;
public:
iterator()
:
m_i_vectorAddr(nullptr),
m_i_startAddr(nullptr),
m_i_indexNumber(-1),
m_i_valid(false)
{}
iterator(Vector<T>* vectorAddr, T* startAddr, int indexNumber)
:
m_i_vectorAddr(vectorAddr),
m_i_startAddr(startAddr),
m_i_indexNumber(indexNumber),
m_i_valid(false)
{
if (nullptr != m_i_vectorAddr && nullptr != m_i_startAddr && 0 <= m_i_indexNumber)
{
m_i_valid = true;
}
}
public:
T operator * ()
{
return *(m_i_startAddr + m_i_indexNumber);
}
iterator& operator ++ ()
{
if (m_i_vectorAddr->m_startAddr != m_i_startAddr || -1 == m_i_indexNumber)
assert(nullptr);
if (m_i_indexNumber == m_i_vectorAddr->Size() - 1)
{
m_i_indexNumber = -1;
}
else
{
++m_i_indexNumber;
}
return *this;
}
iterator operator ++ (int)
{
iterator copy = *this;
++(*this);
return copy;
}
iterator& operator -- ()
{
if (-1 == m_i_indexNumber)
assert(nullptr);
if (m_i_vectorAddr->m_startAddr != m_i_startAddr)
assert(nullptr);
--m_i_indexNumber;
return *this;
}
iterator operator -- (int)
{
iterator copy = *this;
--(*this);
return copy;
}
bool operator == (const iterator& other)
{
if (m_i_startAddr == other.m_i_startAddr && m_i_vectorAddr == other.m_i_vectorAddr)
return true;
return false;
}
bool operator != (const iterator& other)
{
return !(*this == other);
}
friend class Vector;
};
};
template <typename T>
inline typename void Vector<T>::PushDataBack(const T& data)
{
if (m_dataCount >= m_dataMaxCount)
{
Resize(m_dataMaxCount * 2);
}
m_startAddr[m_dataCount++] = data;
}
template <typename T>
inline typename void Vector<T>::Resize(const int& resize)
{
if (m_dataMaxCount >= resize)
assert(nullptr);
T* newAddr = new T[resize];
for (int i = 0; i < m_dataCount; ++i)
{
newAddr[i] = m_startAddr[i];
}
delete[] m_startAddr;
m_startAddr = newAddr;
m_dataMaxCount = resize;
}
template <typename T>
inline typename Vector<T>::iterator Vector<T>::begin()
{
return iterator(this, m_startAddr, 0);
}
template <typename T>
inline typename Vector<T>::iterator Vector<T>::end()
{
return iterator(this, m_startAddr, -1);
}
template <typename T>
inline typename Vector<T>::iterator Vector<T>::erase(iterator& eraseIter)
{
if (nullptr == eraseIter.m_i_vectorAdd || nullptr == eraseIter.m_i_vectorAddr->m_startAddr || -1 == eraseIter.m_i_indexNumber )
assert(nullptr);
int loop = eraseIter.m_i_vectorAddr->m_dataCount - eraseIter.m_i_indexNumber + 1;
for (int i = 0; i < loop; ++i)
{
m_startAddr[loop + i] = m_startAddr[loop + 1 + i];
}
--m_dataCount;
eraseIter.m_i_valid = false;
return iterator(this, m_startAddr, eraseIter.m_i_indexNumber);
}