188 lines
4.2 KiB
C++
188 lines
4.2 KiB
C++
/*
|
|
* Copyright (c) by BlackMark 2015
|
|
* Date 06/12/2015
|
|
* Version 1.6
|
|
*/
|
|
|
|
#ifndef VECTOR_H
|
|
#define VECTOR_H
|
|
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "cppalloc.h"
|
|
|
|
template<typename T>
|
|
class vector
|
|
{
|
|
private:
|
|
size_t m_sizeSize;
|
|
size_t m_sizeCapacity;
|
|
T* m_pData;
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void extend()
|
|
{
|
|
m_sizeCapacity = ( ( m_sizeCapacity > 0 ) ? ( 2 * m_sizeCapacity ) : ( 1 ) );
|
|
|
|
T *pCopyData = new T[m_sizeCapacity];
|
|
memcpy( pCopyData, m_pData, m_sizeSize * sizeof( T ) );
|
|
|
|
delete [] m_pData;
|
|
m_pData = pCopyData;
|
|
}
|
|
|
|
public:
|
|
//////////////////////////////////////////////////////////////////////////
|
|
vector() :
|
|
m_sizeSize( 0 ), m_sizeCapacity( 0 ), m_pData( nullptr )
|
|
{}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
vector( size_t sizeSize, const T &Value = T() ) :
|
|
m_sizeSize( sizeSize ), m_sizeCapacity( sizeSize ), m_pData( nullptr )
|
|
{
|
|
m_pData = new T[m_sizeCapacity];
|
|
|
|
for( size_t i = 0; i < m_sizeSize; ++i )
|
|
{
|
|
m_pData[i] = Value;
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
vector( const vector &vecVector ) :
|
|
m_sizeSize( vecVector.m_sizeSize ), m_sizeCapacity( vecVector.m_sizeCapacity ), m_pData( nullptr )
|
|
{
|
|
m_pData = new T[m_sizeCapacity];
|
|
memcpy( m_pData, vecVector.m_pData, m_sizeSize * sizeof( T ) );
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
vector( vector &&vecVector ) :
|
|
m_sizeSize( vecVector.m_sizeSize ), m_sizeCapacity( vecVector.m_sizeCapacity ), m_pData( vecVector.m_pData )
|
|
{
|
|
vecVector.m_sizeSize = 0;
|
|
vecVector.m_sizeCapacity = 0;
|
|
vecVector.m_pData = nullptr;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
~vector()
|
|
{
|
|
delete [] m_pData;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
vector& operator=( const vector &vecRHS )
|
|
{
|
|
m_sizeSize = vecRHS.m_sizeSize;
|
|
m_sizeCapacity = vecRHS.m_sizeCapacity;
|
|
m_pData = new T[m_sizeCapacity];
|
|
memcpy( m_pData, vecRHS.m_pData, m_sizeSize * sizeof( T ) );
|
|
|
|
return *this;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
vector& operator=( vector &&vecRHS )
|
|
{
|
|
m_sizeSize = vecRHS.m_sizeSize;
|
|
m_sizeCapacity = vecRHS.m_sizeCapacity;
|
|
delete [] m_pData;
|
|
m_pData = vecRHS.m_pData;
|
|
|
|
vecRHS.m_sizeSize = 0;
|
|
vecRHS.m_sizeCapacity = 0;
|
|
vecRHS.m_pData = nullptr;
|
|
|
|
return *this;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
T* data()
|
|
{
|
|
return m_pData;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
const T* data() const
|
|
{
|
|
return m_pData;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void clear()
|
|
{
|
|
delete [] m_pData;
|
|
m_pData = nullptr;
|
|
m_sizeSize = 0;
|
|
m_sizeCapacity = 0;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void push_back( T const &Data )
|
|
{
|
|
if( m_sizeSize == m_sizeCapacity )
|
|
{
|
|
extend();
|
|
}
|
|
|
|
m_pData[m_sizeSize++] = Data;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
void resize( size_t sizeSize, T val = T() )
|
|
{
|
|
if( sizeSize != m_sizeSize )
|
|
{
|
|
m_sizeCapacity = sizeSize;
|
|
|
|
T *pCopyData = new T[m_sizeCapacity];
|
|
memcpy( pCopyData, m_pData, ( ( sizeSize < m_sizeSize ) ? ( sizeSize ) : ( m_sizeSize ) ) * sizeof( T ) );
|
|
|
|
delete [] m_pData;
|
|
m_pData = pCopyData;
|
|
|
|
if( sizeSize > m_sizeSize )
|
|
{
|
|
for( size_t i = m_sizeSize; i < sizeSize; ++i )
|
|
{
|
|
m_pData[i] = val;
|
|
}
|
|
}
|
|
|
|
m_sizeSize = sizeSize;
|
|
}
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
size_t size() const
|
|
{
|
|
return m_sizeSize;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
bool empty() const
|
|
{
|
|
if( m_sizeSize == 0 )
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
const T& operator[]( size_t sizeIndex ) const
|
|
{
|
|
return m_pData[sizeIndex];
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
T& operator[]( size_t sizeIndex )
|
|
{
|
|
return m_pData[sizeIndex];
|
|
}
|
|
};
|
|
|
|
#endif |