I have tried to implement a basic vector type in C++, yet I am not sure if I can do anything any more efficiently.
template < typename _Ty > class vector
{
public:
typedef _Ty *iterator;
typedef _Ty _Value_type;
typedef vector<_Ty> _Myt;
vector()
: __size(0), __capacity(10), __data((_Value_type *)calloc(10, sizeof(_Value_type)))
{
}
vector(_Myt &_Rhs)
: __data((_Value_type *)calloc((__capacity = _Rhs.size() + 10), sizeof(_Value_type)))
{
memcpy(__data, _Rhs.__data, (__size = _Rhs.size()) * sizeof(_Value_type));
}
~vector()
{
memset(__data, NULL, 1);
}
_Value_type *data() const
{
return __data;
}
_Myt &push_back(const _Value_type &_Rhs)
{
if (++__size > __capacity)
{
reserve(__capacity + 10);
}
__data[__size - 1] = _Rhs;
return *this;
}
_Myt &operator+=(const _Value_type &_Rhs)
{
return push_back(_Rhs);
}
UINT size() const
{
return __size;
}
UINT capacity() const
{
return __capacity;
}
iterator begin() const
{
return &__data[0];
}
iterator rbegin()
{
return reversed_adaptor().begin();
}
iterator end() const
{
return &__data[__size];
}
iterator rend()
{
return reversed_adaptor().end();
}
iterator find(const _Value_type &_Search) const
{
for (iterator i = begin(); i != end(); ++i)
{
if (*i == _Search)
{
return i;
}
}
return NULL;
}
bool contains(const _Value_type &_Search) const
{
return find(_Search) != NULL;
}
_Myt &operator=(_Myt &_Rhs)
{
reserve((__size = _Rhs.size()) + 10);
memcpy(__data, _Rhs.__data, _Rhs.size() * sizeof(_Value_type));
return *this;
}
_Value_type pop_back()
{
_Value_type temp = __data[__size -= 1];
resize(__size);
return temp;
}
const _Value_type &at(UINT _Base) const
{
if (_Base >= 0 && _Base < size())
{
return __data[_Base];
}
throw std::out_of_range("vector::at - out of range");
}
_Value_type &at(UINT _Base)
{
if (_Base >= 0 && _Base < size())
{
return __data[_Base];
}
throw std::out_of_range("vector::at - out of range");
}
_Value_type &operator[](UINT _Base)
{
return __data[_Base];
}
const _Value_type &operator[](UINT _Base) const
{
return __data[_Base];
}
_Myt &swap(_Myt &_Rhs)
{
std::swap(*this, _Rhs);
return *this;
}
_Myt &reserve(UINT _Capacity)
{
__data = (_Value_type *)realloc(__data, (__capacity = _Capacity) * sizeof(_Value_type));
return *this;
}
_Myt &resize(UINT _Size, _Value_type _Value = _Value_type())
{
int over = (_Size > __size), temp = __size;
__data = (_Value_type *)realloc(__data, (__capacity = (__size = _Size)) * sizeof(_Value_type));
if (over)
{
for (iterator i = &__data[temp]; i != end(); ++i)
{
*i = _Value;
}
}
return *this;
}
_Value_type erase(iterator _Iter)
{
if (_Iter == end())
{
return pop_back();
}
for (iterator i = _Iter; i + 1 != end(); ++i)
{
*i = *(i + 1);
}
return pop_back();
}
template < typename _Ty1 > bool operator==(const vector<_Ty1> &_Rhs)
{
if ((typeid(_Value_type) != typeid(_Ty1)) || size() != _Rhs.size())
{
return false;
}
for (iterator i = begin(), j = _Rhs.begin(); i != end(), j != _Rhs.end(); ++i, ++j)
{
if (*i != *j)
{
return false;
}
}
return true;
}
template < typename _Ty1 > bool operator!=(const vector<_Ty1> &_Rhs)
{
return !(*this == _Rhs);
}
bool empty()
{
return size > 0;
}
_Myt &reverse()
{
std::reverse(begin(), end());
return *this;
}
_Myt reversed_adaptor()
{
_Myt adaptor(*this);
return adaptor.reverse();
}
_Myt insert(iterator _Begin, iterator _End)
{
for (iterator i = _Begin; i != _End; ++i)
{
push_back(*i);
}
return *this;
}
private:
_Value_type *__data;
UINT __size, __capacity;
};