68 lines
1.1 KiB
C++
68 lines
1.1 KiB
C++
|
|
#ifndef __KCUSTOMVECTOR__H
|
|
#define __KCUSTOMVECTOR__H
|
|
|
|
template <class T>
|
|
class KCustomVector
|
|
{
|
|
public:
|
|
KCustomVector(DWORD dwDefaultSize = 40)
|
|
{
|
|
_allocator = new T[dwDefaultSize];
|
|
_first = _allocator;
|
|
_last = _allocator;
|
|
_end = _first + dwDefaultSize;
|
|
}
|
|
~KCustomVector()
|
|
{
|
|
delete [] _allocator;
|
|
}
|
|
|
|
void push_back(const T & _obj)
|
|
{
|
|
if( _last == _end)
|
|
{
|
|
int nSize = static_cast<int>((_last - _first) * 2);
|
|
T * _newAllocator = new T[nSize];
|
|
|
|
for(int i = 0; i < nSize / 2; ++i)
|
|
_newAllocator[i] = _allocator[i];
|
|
|
|
delete []_allocator;
|
|
_allocator = _newAllocator;
|
|
_first = _allocator;
|
|
_last = _first + (nSize / 2);
|
|
_end = _first + nSize;
|
|
}
|
|
*_last = _obj;
|
|
_last++;
|
|
}
|
|
|
|
T & front() { return _first[0]; }
|
|
T * begin() { return _first; }
|
|
T * end() { return _last; }
|
|
T & at(int nIndex)
|
|
{
|
|
return _allocator[nIndex];
|
|
}
|
|
T & operator[](int nIndex)
|
|
{
|
|
return _allocator[nIndex];
|
|
}
|
|
void clear()
|
|
{
|
|
_last = _first;
|
|
}
|
|
|
|
int size()
|
|
{
|
|
return (int)(_last - _first);
|
|
}
|
|
typedef T* iterator;
|
|
|
|
private:
|
|
T * _first, *_last, *_end;
|
|
T * _allocator;
|
|
};
|
|
|
|
#endif |