home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.funduc.com
/
2014.08.ftp.funduc.com.tar
/
ftp.funduc.com
/
fshedcode-072212.zip
/
simparr.h
< prev
next >
Wrap
C/C++ Source or Header
|
2010-09-13
|
11KB
|
425 lines
/////////////////////////////////////////////////////////////////////////////
// License (GPLv2+):
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
/////////////////////////////////////////////////////////////////////////////
#if !defined(__SIMPLEARRAY_H)
#define __SIMPLEARRAY_H
#define ARR_EMPTY -1
#include <string.h>
#define bitval(base,pos) ((base)[(pos)/8]&(1<<((pos)%8)))
#define CLIENT_BORDER_WIDTH 2
#define ANSI_SET ANSI_FIXED_FONT
#define OEM_SET OEM_FIXED_FONT
#define LITTLEENDIAN_MODE 0
#define BIGENDIAN_MODE 1
#define TIMERID 1
#define TIMERDURATION 10
#define MRUMAX 9
#define BMKMAX 9
#define BMKTEXTMAX 256
#define TPL_TYPE_MAXLEN 16
#define TPL_NAME_MAXLEN 128
#define FINDDLG_BUFLEN (64*1024)
template<class T> class SimpleArray
{
public:
operator T*()
{
return m_pT;
}
T& operator[](int nIndex) {return m_pT[nIndex];}
SimpleArray()
{
m_pT = NULL;
m_nSize = 0;
m_nUpperBound = ARR_EMPTY;
m_nGrowBy = 1;
}
//-------------------------------------------------------------------
SimpleArray(int nNewSize, int nGrowBy)
: m_nGrowBy(nGrowBy), m_nUpperBound(ARR_EMPTY), m_nSize(nNewSize)
{
m_pT = new T[m_nSize];
}
//-------------------------------------------------------------------
SimpleArray(T* ptArray, int upbound, int size)
: m_nGrowBy(1), m_pT(ptArray), m_nUpperBound(upbound), m_nSize(size)
{
#ifdef _DEBUG
if(m_pT == NULL || m_nUpperBound<0 || m_nSize<=0 || m_nUpperBound >= m_nSize)
{
m_nSize = 0;
m_nUpperBound = ARR_EMPTY;
}
#endif
}
//-------------------------------------------------------------------
SimpleArray(SimpleArray& spaArg)
: m_nSize(spaArg.m_nSize), m_nUpperBound(spaArg.m_nUpperBound),
m_nGrowBy(spaArg.m_nGrowBy)
{
m_pT = new T[m_nSize];
int k;
for(k = 0; k <= m_nUpperBound; k++)
m_pT[k] = spaArg.m_pT[k];
}
//-------------------------------------------------------------------
~SimpleArray()
{
if(m_pT != NULL) delete [] m_pT;
}
//-------------------------------------------------------------------
BOOL InsertAtGrow(int nIndex, T argT, int nCount = 1)
{
if(nIndex<0 || nCount<1)
return FALSE;
int i;
if(nIndex > m_nUpperBound)
{
for(i = 0; i < nCount; i++)
SetAtGrow(nIndex + i, argT);
return TRUE;
}
else
{
if(m_nSize < m_nUpperBound + 1 + nCount)
{
if (AddSpace(nCount) == FALSE)
return FALSE;
}
for(i = m_nUpperBound + nCount; i > nIndex; i--)
{
m_pT[i] = m_pT[i-nCount];
}
for(i = 0; i < nCount; i++)
m_pT[nIndex + i] = argT;
m_nUpperBound += nCount;
return TRUE;
}
}
//-------------------------------------------------------------------
BOOL InsertAtGrow (int nIndex, T* pT, int nSrcIndex, int nCount)
{
if(nIndex<0 || nCount<1)
return FALSE;
int i;
if(nIndex > m_nUpperBound)
{
for(i = 0; i < nCount; i++)
{
if (SetAtGrowRef(nIndex + i, pT[nSrcIndex+i]) == FALSE)
return FALSE;
}
return TRUE;
}
else
{
if(m_nSize < m_nUpperBound + 1 + nCount)
{
if (AddSpace(nCount) == FALSE)
return FALSE;
}
for(i = m_nUpperBound + nCount; i > nIndex; i--)
{
m_pT[i] = m_pT[i-nCount];
}
for(i = 0; i < nCount; i++)
m_pT[nIndex + i] = pT[nSrcIndex+i];
m_nUpperBound += nCount;
return TRUE;
}
}
//-------------------------------------------------------------------
BOOL InsertAt( int nIndex, T argT, int nCount = 1)
{
// Valid index?
if( nIndex < 0 || nIndex > m_nUpperBound )
return FALSE;
int i;
// Is there enough space after m_nUpperBound for inserting?
if( ( m_nSize - 1 ) - m_nUpperBound >= nCount )
{
// Yes, no need to allocate more memory.
// Push up the elements at the current position.
for( i = m_nUpperBound + nCount; i >= nIndex + nCount; i-- )
m_pT[ i ] = m_pT[ i - nCount ];
// Copy in the new data.
for( i = 0; i < nCount; i++ )
m_pT[ nIndex + i ] = argT;
// Adjust m_nUpperBound to new size.
m_nUpperBound += nCount;
}
else
{
// No, need to allocate more memory.
for (i = m_nSize - 1; i >= nIndex + nCount; i--)
m_pT[i] = m_pT[i - nCount];
if (m_nSize - nIndex < nCount)
nCount = m_nSize - nIndex;
for (i = 0; i < nCount; i++)
m_pT[nIndex + i] = argT;
m_nUpperBound = m_nSize - 1;
}
return TRUE;
}
//-------------------------------------------------------------------
BOOL RemoveAt(int nIndex, int nCount = 1)
{
if(nIndex < 0 || nIndex > m_nUpperBound || nCount < 1) return FALSE;
if(nCount > m_nUpperBound - nIndex)
{
m_nUpperBound = nIndex - 1;
return TRUE;
}
int i;
for(i = nIndex; i <= m_nUpperBound - nCount; i++)
{
m_pT[i] = m_pT[i + nCount];
}
m_nUpperBound -= nCount;
return TRUE;
}
//-------------------------------------------------------------------
void SetAtGrow(int nIndex, T argT)
{
if(nIndex < 0) return;
if(nIndex > m_nSize - 1)
AddSpace(nIndex - m_nSize + 1);
m_pT[nIndex] = argT;
if(nIndex > m_nUpperBound) m_nUpperBound = nIndex;
}
//-------------------------------------------------------------------
BOOL SetAtGrowRef(int nIndex, T& argT)
{
if(nIndex < 0)
return FALSE;
if(nIndex > m_nSize - 1)
{
if (AddSpace(nIndex - m_nSize + 1) == FALSE)
return FALSE;
}
m_pT[nIndex] = argT;
if(nIndex > m_nUpperBound)
m_nUpperBound = nIndex;
return TRUE;
}
//-------------------------------------------------------------------
void SetAt(int nIndex, T argT)
{
if(nIndex >= 0 && nIndex < m_nSize)
{
m_pT[nIndex] = argT;
if(nIndex > m_nUpperBound)
m_nUpperBound = nIndex;
}
else
return;
}
//-------------------------------------------------------------------
void SetAtRef(int nIndex, T& argT)
{
if(nIndex >= 0 && nIndex < m_nSize)
{
m_pT[nIndex] = argT;
if(nIndex > m_nUpperBound)
m_nUpperBound = nIndex;
}
else
return;
}
//-------------------------------------------------------------------
T GetAt(int nIndex)
{
return m_pT[nIndex];
}
//-------------------------------------------------------------------
BOOL AddSpace(int nExtend)
{
int newsize = m_nSize + (((nExtend-1) / m_nGrowBy) + 1) * m_nGrowBy;
T* pT = new T[newsize];
if (pT != NULL)
{
int i;
for(i = 0; i < m_nSize; i++)
pT[i] = m_pT[i];
if(m_pT != NULL)
delete [] m_pT;
m_pT = pT;
m_nSize = newsize;
return TRUE;
}
else
return FALSE;
}
//-------------------------------------------------------------------
int GetGrowBy()
{
return m_nGrowBy;
}
//-------------------------------------------------------------------
int GetSize()
{
return m_nSize;
}
//-------------------------------------------------------------------
int GetUpperBound()
{
return m_nUpperBound;
}
//-------------------------------------------------------------------
int GetLength()
{
return m_nUpperBound+1;
}
//-------------------------------------------------------------------
BOOL SetSize(int nNewSize, int nGrowBy = 0 )
{
if(nNewSize < 0)
return FALSE;
if (nNewSize == m_nSize)
return TRUE;
if( nNewSize == 0 )
{
ClearAll();
return TRUE;
}
T* pT = new T[nNewSize];
if (pT == NULL)
return FALSE;
int i;
if(m_nUpperBound < nNewSize)
{
for(i = 0; i <= m_nUpperBound; i++)
pT[i] = m_pT[i];
}
else
{
for(i = 0; i < nNewSize; i++)
pT[i] = m_pT[i];
m_nUpperBound = nNewSize - 1;
}
if(m_pT != NULL)
delete [] m_pT;
m_pT = pT;
m_nSize = nNewSize;
if(nGrowBy > 0)
m_nGrowBy = nGrowBy;
return TRUE;
}
//-------------------------------------------------------------------
void SetGrowBy(int nGrowBy)
{
if(nGrowBy > 0) m_nGrowBy = nGrowBy;
}
//-------------------------------------------------------------------
void ClearAll()
{
if(m_pT != NULL)
delete [] m_pT;
m_pT = NULL;
m_nSize = 0;
m_nUpperBound = ARR_EMPTY;
}
//-------------------------------------------------------------------
T& GetRefAt(int nIndex)
{
return m_pT[nIndex];
}
//-------------------------------------------------------------------
void SetUpperBound(int upbnd)
{
if(upbnd < m_nSize)
m_nUpperBound = upbnd;
}
//-------------------------------------------------------------------
BOOL ExpandToSize()
{
m_nUpperBound = m_nSize - 1;
return TRUE;
}
//-------------------------------------------------------------------
BOOL CopyFrom( int index, T* pSrc, int srclen )
{
if( m_nSize - index >= srclen )
{
// Enough space to copy into.
int i;
for( i = 0; i < srclen; i++ )
m_pT[ index + i ] = pSrc[ i ];
if( index + srclen - 1 > m_nUpperBound )
m_nUpperBound = index + srclen - 1;
return TRUE;
}
else
return FALSE;
}
//-------------------------------------------------------------------
T* GetBuffer( int index, int srclen )
{
if( m_nSize - index >= srclen )
{
// Enough space to copy into.
if( index + srclen - 1 > m_nUpperBound )
m_nUpperBound = index + srclen - 1;
return m_pT + index;
}
else
return NULL;
}
protected:
T* m_pT;
int m_nSize;
int m_nUpperBound;
int m_nGrowBy;
};
#endif // #if !defined(__SIMPLEARRAY_H)