Exported libraries as submodules

This commit is contained in:
BlackMark 2016-05-20 15:46:20 +02:00
parent b1bd03eeb6
commit b9eb6285df
11 changed files with 135 additions and 802 deletions

9
.gitmodules vendored Normal file
View File

@ -0,0 +1,9 @@
[submodule "usart/cppalloc"]
path = usart/cppalloc
url = git@blackmark.me:cppalloc.git
[submodule "usart/string"]
path = usart/string
url = git@blackmark.me:string.git
[submodule "usart/vector"]
path = usart/vector
url = git@blackmark.me:vector.git

1
usart/cppalloc Submodule

@ -0,0 +1 @@
Subproject commit 72c6a6cc88d6cafecf372fe968d3befae037ad9e

View File

@ -1,25 +0,0 @@
#include "cppalloc.h"
//////////////////////////////////////////////////////////////////////////
void* operator new( size_t sizeSize )
{
return malloc( sizeSize );
}
//////////////////////////////////////////////////////////////////////////
void operator delete( void* pAddress )
{
free( pAddress );
}
//////////////////////////////////////////////////////////////////////////
void* operator new[]( size_t sizeSize )
{
return malloc( sizeSize );
}
//////////////////////////////////////////////////////////////////////////
void operator delete[]( void* pAddress )
{
free( pAddress );
}

View File

@ -1,18 +0,0 @@
/*
* Copyright (c) by BlackMark 2015
* Date 24/11/2015
* Version 1.3
*/
#ifndef CPPALLOC_H
#define CPPALLOC_H
#include <stdlib.h>
void* operator new( size_t sizeSize );
void operator delete( void* pAddress );
void* operator new[]( size_t sizeSize );
void operator delete[]( void* pAddress );
#endif

1
usart/string Submodule

@ -0,0 +1 @@
Subproject commit 23189911c6929685e962be9866ff6127938da6a8

View File

@ -1,448 +0,0 @@
#include "string.h"
//////////////////////////////////////////////////////////////////////////
string::string() :
m_sizeLength( 0 ), m_sizeCapacity( 1 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::string( const string &strString ) :
m_sizeLength( strString.m_sizeLength ), m_sizeCapacity( strString.m_sizeCapacity ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
memcpy( m_szString, strString.m_szString, m_sizeCapacity * sizeof( char ) );
}
//////////////////////////////////////////////////////////////////////////
string::string( const char* szString ) :
m_sizeLength( strlen( szString ) ), m_sizeCapacity( strlen( szString ) + 1 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
memcpy( m_szString, szString, m_sizeLength * sizeof( char ) );
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::string( char chCharacter ) :
m_sizeLength( 1 ), m_sizeCapacity( 2 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
m_szString[0] = chCharacter;
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::string( size_t sizeLength, char chCharacter ) :
m_sizeLength( sizeLength ), m_sizeCapacity( sizeLength + 1 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
for( size_t i = 0; i < sizeLength; ++i )
{
m_szString[i] = chCharacter;
}
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::~string()
{
delete [] m_szString;
}
//////////////////////////////////////////////////////////////////////////
string &string::append( const string& strString )
{
if( m_sizeLength + strString.m_sizeLength >= m_sizeCapacity )
{
char* szCopyString = new char[m_sizeLength + strString.m_sizeLength + 1];
if( !szCopyString )
{
return *this;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
m_sizeCapacity = m_sizeLength + strString.m_sizeLength + 1;
m_szString = szCopyString;
}
memcpy( m_szString + m_sizeLength, strString.m_szString, strString.m_sizeLength * sizeof( char ) );
m_sizeLength = m_sizeLength + strString.m_sizeLength;
m_szString[m_sizeLength] = '\0';
return *this;
}
//////////////////////////////////////////////////////////////////////////
char& string::at( size_t sizeIndex )
{
if( sizeIndex >= m_sizeLength )
{
return m_szString[m_sizeLength - 1];
}
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
const char& string::at( size_t sizeIndex ) const
{
if( sizeIndex >= m_sizeLength )
{
return m_szString[m_sizeLength - 1];
}
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
size_t string::capacity() const
{
return m_sizeCapacity;
}
//////////////////////////////////////////////////////////////////////////
void string::clear()
{
m_sizeLength = 0;
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
const char* string::c_str() const
{
return m_szString;
}
//////////////////////////////////////////////////////////////////////////
const char* string::data() const
{
return m_szString;
}
//////////////////////////////////////////////////////////////////////////
bool string::empty() const
{
if( m_sizeLength == 0 )
{
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
size_t string::find( const string &strString, size_t sizePos /* = 0 */ ) const
{
if( strString.empty() || sizePos >= m_sizeLength )
{
return npos;
}
for( size_t i = sizePos; i < m_sizeLength; ++i )
{
if( i + strString.length() > m_sizeLength )
{
return npos;
}
if( m_szString[i] == strString[0] )
{
size_t sizeFound;
for( sizeFound = 1; sizeFound < strString.size(); ++sizeFound )
{
if( m_szString[i + sizeFound] != strString[sizeFound] )
{
break;
}
}
if( sizeFound == strString.size() )
{
return i;
}
}
}
return npos;
}
//////////////////////////////////////////////////////////////////////////
size_t string::length() const
{
return m_sizeLength;
}
//////////////////////////////////////////////////////////////////////////
void string::push_back( char chCharacter )
{
if( m_sizeLength + 1 >= m_sizeCapacity )
{
char* szCopyString = new char[m_sizeLength + 2];
if( !szCopyString )
{
return;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
m_sizeCapacity = m_sizeLength + 2;
m_szString = szCopyString;
}
m_szString[m_sizeLength++] = chCharacter;
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
void string::reserve( size_t sizeCapacity /* = 0 */ )
{
if( sizeCapacity > m_sizeCapacity )
{
char* szCopyString = new char[sizeCapacity];
if( !szCopyString )
{
return;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
szCopyString[m_sizeLength] = '\0';
m_sizeCapacity = sizeCapacity;
m_szString = szCopyString;
}
}
//////////////////////////////////////////////////////////////////////////
void string::resize( size_t sizeLength, char chCharacter )
{
if( sizeLength < m_sizeLength )
{
m_sizeLength = sizeLength;
m_szString[m_sizeLength] = '\0';
}
else if( sizeLength > m_sizeLength )
{
if( sizeLength + 1 > m_sizeCapacity )
{
char* szCopyString = new char[sizeLength + 1];
if( !szCopyString )
{
return;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
for( size_t i = m_sizeLength; i < sizeLength; ++i )
{
szCopyString[i] = chCharacter;
}
szCopyString[sizeLength] = '\0';
m_sizeLength = sizeLength;
m_sizeCapacity = m_sizeLength + 1;
m_szString = szCopyString;
}
else
{
for( size_t i = m_sizeLength; i < sizeLength; ++i )
{
m_szString[i] = chCharacter;
}
m_szString[sizeLength] = '\0';
m_sizeLength = sizeLength;
}
}
}
//////////////////////////////////////////////////////////////////////////
size_t string::rfind( const string &strString, size_t sizePos /* = npos */ ) const
{
if( sizePos == npos )
{
sizePos = m_sizeLength - 1;
}
if( strString.empty() || sizePos >= m_sizeLength )
{
return npos;
}
for( size_t i = sizePos + 1; i-- > 0; )
{
if( i - ( strString.length() - 1 ) > m_sizeLength )
{
return npos;
}
if( m_szString[i] == strString[strString.length() - 1] )
{
size_t sizeFound;
for( sizeFound = strString.length() - 1; sizeFound-- > 0; )
{
if( m_szString[i - ( strString.length() - ( sizeFound + 1 ) )] != strString[sizeFound] )
{
break;
}
}
if( sizeFound == npos )
{
return i - ( strString.length() - 1 );
}
}
}
return npos;
}
//////////////////////////////////////////////////////////////////////////
size_t string::size() const
{
return m_sizeLength;
}
//////////////////////////////////////////////////////////////////////////
string string::substr( size_t sizePos /* = 0 */, size_t sizeLength /* = npos */ ) const
{
if( sizePos >= m_sizeLength )
{
return string();
}
string strSubString;
for( size_t i = sizePos; i < sizePos + sizeLength; ++i )
{
if( i >= m_sizeLength )
{
break;
}
strSubString += m_szString[i];
}
return strSubString;
}
//////////////////////////////////////////////////////////////////////////
string& string::operator=( const string &strRHS )
{
char* szCopyString = new char[strRHS.m_sizeCapacity];
if( !szCopyString )
{
return *this;
}
m_sizeLength = strRHS.m_sizeLength;
m_sizeCapacity = strRHS.m_sizeCapacity;
memcpy( szCopyString, strRHS.m_szString, m_sizeCapacity * sizeof( char ) );
delete [] m_szString;
m_szString = szCopyString;
return *this;
}
//////////////////////////////////////////////////////////////////////////
char &string::operator[]( size_t sizeIndex )
{
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
const char &string::operator[]( size_t sizeIndex ) const
{
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
string string::operator+( const string &strRHS ) const
{
string strReturn = *this;
strReturn.append( strRHS );
return strReturn;
}
//////////////////////////////////////////////////////////////////////////
string &string::operator+=( const string &strRHS )
{
append( strRHS );
return *this;
}
//////////////////////////////////////////////////////////////////////////
bool string::operator==( const string &strRHS ) const
{
if( size() != strRHS.size() )
{
return false;
}
for( size_t i = 0; i < size(); ++i )
{
if( m_szString[i] != strRHS.m_szString[i] )
{
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////////////
bool string::operator!=( const string &strRHS ) const
{
return !( *this == strRHS );
}

View File

@ -1,55 +0,0 @@
/*
* Copyright (c) by BlackMark 2015
* Date 26/11/2015
* Version 1.4
*/
#ifndef STRING_H
#define STRING_H
#include <string.h>
class string
{
private:
size_t m_sizeLength;
size_t m_sizeCapacity;
char* m_szString;
public:
static const size_t npos = -1;
string();
string( const string &strString );
string( const char* szString );
string( char chCharacter );
string( size_t sizeLength, char chCharacter );
~string();
string& append( const string &strString );
char& at( size_t sizeIndex );
const char& at( size_t sizeIndex ) const;
size_t capacity() const;
void clear();
const char* c_str() const;
const char* data() const;
bool empty() const;
size_t find( const string &strString, size_t sizePos = 0 ) const;
size_t length() const;
void push_back( char chCharacter );
void reserve( size_t sizeCapacity = 0 );
void resize( size_t sizeLength, char chCharacter = '\0' );
size_t rfind( const string &strString, size_t sizePos = npos ) const;
size_t size() const;
string substr( size_t sizePos = 0, size_t sizeLength = npos ) const;
string& operator=( const string &strRHS );
char& operator[]( size_t sizeIndex );
const char& operator[]( size_t sizeIndex ) const;
string operator+( const string &strRHS ) const;
string& operator+=( const string &strRHS );
bool operator==( const string &strRHS ) const;
bool operator!=( const string &strRHS ) const;
};
#endif

View File

@ -80,24 +80,49 @@
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
</ListValues>
</avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcccpp.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.compiler.directories.IncludePaths>
<avrgcccpp.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
</ListValues>
</avrgcccpp.compiler.directories.IncludePaths>
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++11</avrgcccpp.compiler.miscellaneous.OtherFlags>
<avrgcccpp.linker.general.UseVprintfLibrary>True</avrgcccpp.linker.general.UseVprintfLibrary>
<avrgcccpp.linker.libraries.Libraries><ListValues><Value>libm</Value><Value>libprintf_flt</Value></ListValues></avrgcccpp.linker.libraries.Libraries>
<avrgcccpp.assembler.general.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.assembler.general.IncludePaths>
<avrgcc.compiler.symbols.DefSymbols><ListValues><Value>NDEBUG</Value></ListValues></avrgcc.compiler.symbols.DefSymbols>
<avrgcccpp.linker.libraries.Libraries>
<ListValues>
<Value>libm</Value>
<Value>libprintf_flt</Value>
</ListValues>
</avrgcccpp.linker.libraries.Libraries>
<avrgcccpp.assembler.general.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
</ListValues>
</avrgcccpp.assembler.general.IncludePaths>
<avrgcc.compiler.symbols.DefSymbols>
<ListValues>
<Value>NDEBUG</Value>
</ListValues>
</avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
<avrgcccpp.compiler.symbols.DefSymbols><ListValues><Value>NDEBUG</Value></ListValues></avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.symbols.DefSymbols>
<ListValues>
<Value>NDEBUG</Value>
</ListValues>
</avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
</AvrGccCpp>
</ToolchainSettings>
@ -115,25 +140,50 @@
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
</ListValues>
</avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcccpp.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.compiler.directories.IncludePaths>
<avrgcccpp.compiler.directories.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
</ListValues>
</avrgcccpp.compiler.directories.IncludePaths>
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++11</avrgcccpp.compiler.miscellaneous.OtherFlags>
<avrgcccpp.linker.general.UseVprintfLibrary>True</avrgcccpp.linker.general.UseVprintfLibrary>
<avrgcccpp.linker.libraries.Libraries><ListValues><Value>libm</Value><Value>libprintf_flt</Value></ListValues></avrgcccpp.linker.libraries.Libraries>
<avrgcccpp.assembler.general.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.assembler.general.IncludePaths>
<avrgcc.compiler.symbols.DefSymbols><ListValues><Value>DEBUG</Value></ListValues></avrgcc.compiler.symbols.DefSymbols>
<avrgcccpp.linker.libraries.Libraries>
<ListValues>
<Value>libm</Value>
<Value>libprintf_flt</Value>
</ListValues>
</avrgcccpp.linker.libraries.Libraries>
<avrgcccpp.assembler.general.IncludePaths>
<ListValues>
<Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value>
</ListValues>
</avrgcccpp.assembler.general.IncludePaths>
<avrgcc.compiler.symbols.DefSymbols>
<ListValues>
<Value>DEBUG</Value>
</ListValues>
</avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
<avrgcccpp.compiler.symbols.DefSymbols><ListValues><Value>DEBUG</Value></ListValues></avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.symbols.DefSymbols>
<ListValues>
<Value>DEBUG</Value>
</ListValues>
</avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level>
<avrgcccpp.compiler.optimization.DebugLevel>Default (-g2)</avrgcccpp.compiler.optimization.DebugLevel>
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
@ -145,19 +195,19 @@
<Compile Include="Clock.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="cppalloc.cpp">
<Compile Include="cppalloc\cppalloc.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="cppalloc.h">
<Compile Include="cppalloc\cppalloc.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="main.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="string.cpp">
<Compile Include="string\string.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="string.h">
<Compile Include="string\string.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="usart.cpp">
@ -166,9 +216,14 @@
<Compile Include="usart.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="vector.h">
<Compile Include="vector\vector.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="cppalloc" />
<Folder Include="vector" />
<Folder Include="string" />
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>

View File

@ -13,8 +13,8 @@
#include <stdint.h>
#include <ctype.h>
#include "Clock.h"
#include "string.h"
#include "vector.h"
#include "string/string.h"
#include "vector/vector.h"
#if defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
#define USART_SPI

1
usart/vector Submodule

@ -0,0 +1 @@
Subproject commit 42aed5c824ed24b019ddbe0142454ae5a9375470

View File

@ -1,188 +0,0 @@
/*
* 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