?? xarray.h
字號:
{
if(m_Data != NULL) delete[] m_Data;
m_Length = 0;
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]static CopyArray
[Title]復制數組
////////////////////////////////////////////////////////////////////////////////
[param]
<XArray<T> &>srcArray 原數組
<int>srcIndex 原數組開始的下標
<XArray<T> &>destArray 目的數組
<int>destIndex 目的數組開始的下標
<int>copySize 復制的數組元素
[Exception]
Exception 如果發生越界錯誤,則拋出Exception異常
[Description]
用memcpy代替了原來的復制算法
復制數組的數據,從srcArray復制從srcIndex開始的數據,到destArray從destIndex開始的位置,
復制的大小為copySize
如果發生數據越界,將會拋出異常
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
void XArray<T>::CopyArray(const XArray<T> & srcArray,int srcIndex, XArray<T> & destArray, int destIndex, int copySize)
{
//檢查越界
if( copySize < 0) throw Exception("要復制的大小小于零!");
if( srcIndex<0 || srcIndex + copySize > srcArray.m_Length ) throw Exception("要復制內容,超出源數組大小!");
if( destIndex<0 || destIndex + copySize > destArray.m_Length ) throw Exception("要復制內容,越出目的數組大小!");
if( &srcArray == &destArray)
{
if( destIndex > srcIndex )
{
//如果要復制的數據,在目的數據前面,那么就在最后開始復制,避免重復制
T * src = srcArray.m_Data + srcIndex + copySize - 1;
T * dest = destArray.m_Data + destIndex + copySize -1;
for(int i=0; i<copySize; i++)
{
*dest = *src;
dest--;
src--;
}
}
else if( destIndex < srcIndex )
{
T * src = srcArray.m_Data + srcIndex;
T * dest = destArray.m_Data + destIndex;
for(int i=0; i<copySize; i++)
{
*dest = *src;
dest++;
src++;
}
}
}
else
{
T * src = srcArray.m_Data + srcIndex;
T * dest = destArray.m_Data + destIndex;
for(int i=0; i<copySize; i++)
{
*dest = *src;
dest++;
src++;
}
}
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]NewArray
[Title]創建成新數組。
////////////////////////////////////////////////////////////////////////////////
[param]
<XArray<T> &>srcArray 源數組。
<int>srcIndex 源數組開始的下標
<int>copySize 要復制源數組的元素個數
[Exception]
Exception 如果發生錯誤,則拋出異常
EOutOfMemory 如果new出內存失敗,則拋出EOutOfMemory異常
[Description]
從指定的數組中,將自已更新為新的數組,新數組的大小,為指定COPY的大小
注:如果指定的數組就是當前數組本身,NewArray方法會正常COPY,不會有問題
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
void XArray<T>::NewArray(const XArray<T> & srcArray,int srcIndex,int copySize)//XArray<T> & src,int sPos,int copySize)
{
//檢查越界
if( copySize < 0) throw Exception("要復制的大小小于零!");
if(copySize == 0)
{
if(m_Data != NULL) {
delete[] m_Data;
m_Data = NULL;
}
m_Length = 0;
return;
}
if( srcIndex<0 || srcIndex + copySize > srcArray.m_Length ) throw Exception("要復制內容,超出源數組大小!");
if( &srcArray == this )//如果就是為當前數組對象,則要新申請一塊內存
{
T * tmp = new T[copySize];
T * src = srcArray.m_Data + srcIndex;
T * dest = tmp;
//打開CodeGuard的時候,這里可能會提示指針計算越界,但實際不會引用越界指針
for(int i=0; i<copySize; i++)
{
*dest =*src;
dest++;
src++;
}
delete[] m_Data;
m_Data = tmp;
m_Length = copySize;
}
else//如果不是當前數組對象
{
//重定義數據
if( m_Length < copySize) //如果已有數組容量小于要COPY的容量,則重新申請一塊內存
{
if(m_Data != NULL) delete[] m_Data;
m_Data = new T[copySize];
if(m_Data == NULL) throw EOutOfMemory(EXCEPTION_OUT_OF_MEMORY);
}
T * src = srcArray.m_Data + srcIndex;
T * dest = m_Data;
//打開CodeGuard的時候,這里可能會提示指針計算越界,但實際不會引用越界指針
for(int i=0; i<copySize; i++)
{
*dest = *src;
dest++;
src++;
}
m_Length = copySize;
}
return;
}
//------------------------------------------------------------------------------
/*
////////////////////////////////////////////////////////////////////////////////
[Name]XDynamicArray
[Title]XDynamicArray<T>的構造函數
////////////////////////////////////////////////////////////////////////////////
[Param]
<int default=0>InitLength 初始數組的大小
<int default=0>iPrepareLength 預申請的元素個數
[Exception]
EOutOfMemory 如果申請內存,則拋出該異常
[Description]
構造函數,需要指定大小,如果未指定,則默認大小為0
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
XDynamicArray<T>::XDynamicArray(int InitLength,int iPrepareLength)
:m_Data(NULL),m_Length(0)
{
//每次分配的預分配的個數是16的整數倍
if( InitLength < 0 ) InitLength = 0;
if( iPrepareLength < InitLength ) iPrepareLength = InitLength;
if( iPrepareLength < 16 ) iPrepareLength = 16;
if( (iPrepareLength % 16) != 0 ) iPrepareLength = iPrepareLength - iPrepareLength % 16 + 16;
m_Data = new XArray<T>(iPrepareLength);
if( m_Data == NULL ) throw EOutOfMemory(EXCEPTION_OUT_OF_MEMORY);
m_Length = InitLength;
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]XDynamicArray
[Title]XDynamicArray<T>的拷貝構造函數
////////////////////////////////////////////////////////////////////////////////
[Param]
<const XDynamicArray<T> &>srcArray 源數組
[Exception]
EOutOfMemory 如果申請內存失敗,則拋出該異常
[Description]
構造拷貝函數
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
XDynamicArray<T>::XDynamicArray(const XDynamicArray<T> & srcArray)
:m_Data(NULL),m_Length(srcArray.m_Length)
{
m_Data = new XArray<T>( *srcArray.m_Data );
if( m_Data == NULL ) throw EOutOfMemory(EXCEPTION_OUT_OF_MEMORY);
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]XDynamicArray
[Title]XDynamicArray<T>的構造函數
////////////////////////////////////////////////////////////////////////////////
[Param]
<const XArray<T> &>srcArray 源數組
<int default=0>iPrepareLength 預申請的元素個數
[Exception]
EOutOfMemory 如果申請內存失敗,則拋出該異常
[Description]
構造函數,以靜態數組為參數
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
XDynamicArray<T>::XDynamicArray(const XArray<T> & srcArray,int iPrepareLength)
:m_Data(NULL),m_Length(0)
{
int SrcLength = srcArray.GetLength();
if( iPrepareLength < SrcLength ) iPrepareLength = SrcLength;
if( iPrepareLength < 16 ) iPrepareLength = 16;
if( (iPrepareLength % 16) != 0 ) iPrepareLength = iPrepareLength - iPrepareLength % 16 + 16;
m_Data = new XArray<T>(iPrepareLength);
if( m_Data == NULL ) throw EOutOfMemory(EXCEPTION_OUT_OF_MEMORY);
XArray<T>::CopyArray( srcArray, 0, *m_Data, 0, SrcLength);
m_Length = SrcLength;
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]XDynamicArray
[Title]XDynamicArray<T>的構造函數
////////////////////////////////////////////////////////////////////////////////
[Param]
<const T *> data 指向初始化數據的指針
<int> InitLength 初始化數據的個數
<int default=0>iPrepareLength 預申請的元素個數
[Exception]
EOutOfMemory 如果申請內存失敗,則拋出該異常
[Description]
構造函數,以靜態數組為參數
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
XDynamicArray<T>::XDynamicArray(const T * data,int InitLength,int iPrepareLength)
{
if( InitLength < 0 ) InitLength = 0;
if( iPrepareLength < InitLength ) iPrepareLength = InitLength;
if( iPrepareLength < 16 ) iPrepareLength = 16;
if( (iPrepareLength % 16) != 0 ) iPrepareLength = iPrepareLength - iPrepareLength % 16 + 16;
m_Data = new XArray<T>(iPrepareLength);
if( m_Data == NULL ) throw EOutOfMemory(EXCEPTION_OUT_OF_MEMORY);
m_Length = InitLength;
if( InitLength > 0 ) //如果需要copy data
{
T * dest = (T *)m_Data->Data();
for(int i = 0; i< InitLength; i++)
{
*dest = *data;
dest++;
data++;
}
}
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]expandCapacity
[Title]擴展所需的空間
////////////////////////////////////////////////////////////////////////////////
[param]
<int>minimumCapacity 所需的最小空間
[Description]
計算并分配空間
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
void XDynamicArray<T>::expandCapacity(int minimumCapacity)
{
int newCapacity;
if( m_Data->GetLength() > 0 ) newCapacity = m_Data->GetLength() * 2;
else newCapacity = 16;
if (newCapacity < 0)
{
newCapacity = 0x7fffff;//最大32位正整數
}
else if (minimumCapacity > newCapacity)
{
newCapacity = minimumCapacity;
}
//確認分配的元素個數為16的整數倍
if( (newCapacity % 16) != 0 ) newCapacity = newCapacity - newCapacity % 16 + 16;
XArray<T> * newArray = new XArray<T>( newCapacity );
if( m_Length > 0 ) XArray<T>::CopyArray( *m_Data, 0, *newArray, 0, m_Length);
delete m_Data;
m_Data = newArray;
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]~XDynamicArray
[Title]XDynamicArray<T>的析構函數
////////////////////////////////////////////////////////////////////////////////
[Description]
析構函數
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
XDynamicArray<T>::~XDynamicArray()
{
delete m_Data;
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]CopyArray
[Title]復制數組
////////////////////////////////////////////////////////////////////////////////
[param]
<XDynamicArray<T> &>srcArray 原數組
<int>srcIndex 原數組開始的下標
<XDynamicArray<T> &>destArray 目的數組
<int>destIndex 目的數組開始的下標
<int>copySize 復制的數組元素
[Exception]
Exception 如果發生錯誤,則拋出Exception異常
[Description]
復制數組的數據,從src復制從srcIndex開始的數據,到dest開始的位置,復制的大小為copySize
如果發生數據越界,將會拋出異常
[Version]1.0
[Author]Rex Winter
[Date]2005-4-22
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
void XDynamicArray<T>::CopyArray(const XDynamicArray<T> & srcArray,int srcIndex, XDynamicArray<T> & destArray, int destIndex, int copySize)
{
//檢查越界
if( (destIndex == 0) && (copySize > destArray.m_Length) ) return destArray.NewArray( srcArray, srcIndex, copySize);
if( copySize < 0 ) throw Exception("要復制的大小小于零!");
if( (srcIndex < 0) || (srcIndex + copySize > srcArray.m_Length) ) throw Exception("要復制內容,超出源數組大小!");
if( destIndex < 0 ) throw Exception("要復制內容,越出目的數組大小!");
destArray.ensureCapacity( destIndex + copySize ); //確定最小要的空間
XArray<T>::CopyArray( *srcArray.m_Data, srcIndex, *destArray.m_Data, destIndex, copySize);
if( (destIndex + copySize) > destArray.m_Length ) destArray.m_Length = destIndex + copySize;
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]operator []
[Title]取得數組元素。
////////////////////////////////////////////////////////////////////////////////
[param]
<int>Index 對應的數組的下標
[Return]
<T&>返回數組元素的引用。
[Exception]
XExceptionArrayOutOfRange 如果下標越界,則拋出XExceptionArrayOutOfRange異常
[Description]
取得數組元素,并返回這個元素的引用。
[Version]1.0
[Author]Rex Winter
[Date]2005-4-26
////////////////////////////////////////////////////////////////////////////////
*/
template<class T>
T & XDynamicArray<T>::operator[](int Index)
{
if( Index<0 || Index >= m_Length ) throw XExceptionArrayOutOfRange(Index, m_Length);
return m_Data->operator[](Index);
}
/*
////////////////////////////////////////////////////////////////////////////////
[Name]operator [] const
[Title]取得數組元素。
////////////////////////////////////////////////////////////////////////////////
[param]
<int>Index 對應的數組的下標
[Return]
<const T&>返回數組元素的引用。但這個返回值不能修改的
[Exception]
XExceptionArrayOutOfRange 如果下標越界,則拋出XExceptionArrayOutOfRange異常
[Description]
取得數組元素,并返回這個元素的引用。
這個方法,注明為const類型,表示為不可修改。
[Version]1.0
[Author]Rex Winter
[Date]2005-4-26
////////////////////////////////////////////////////////////////////////////////
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -