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 >
C/C++ Source or Header  |  2010-09-13  |  11KB  |  425 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //    License (GPLv2+):
  3. //    This program is free software; you can redistribute it and/or modify
  4. //    it under the terms of the GNU General Public License as published by
  5. //    the Free Software Foundation; either version 2 of the License, or
  6. //    (at your option) any later version.
  7. //
  8. //    This program is distributed in the hope that it will be useful, but
  9. //    WITHOUT ANY WARRANTY; without even the implied warranty of
  10. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. //    General Public License for more details.
  12. //
  13. //    You should have received a copy of the GNU General Public License
  14. //    along with this program; if not, write to the Free Software
  15. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. /////////////////////////////////////////////////////////////////////////////
  17.  
  18. #if !defined(__SIMPLEARRAY_H)
  19. #define __SIMPLEARRAY_H
  20.  
  21. #define ARR_EMPTY -1
  22.  
  23. #include <string.h>
  24.  
  25. #define bitval(base,pos) ((base)[(pos)/8]&(1<<((pos)%8)))
  26. #define CLIENT_BORDER_WIDTH 2
  27. #define ANSI_SET ANSI_FIXED_FONT
  28. #define OEM_SET  OEM_FIXED_FONT
  29. #define LITTLEENDIAN_MODE    0
  30. #define BIGENDIAN_MODE 1
  31. #define TIMERID 1
  32. #define TIMERDURATION 10
  33. #define MRUMAX 9
  34. #define BMKMAX 9
  35. #define BMKTEXTMAX 256
  36. #define TPL_TYPE_MAXLEN 16
  37. #define TPL_NAME_MAXLEN 128
  38. #define FINDDLG_BUFLEN (64*1024)
  39.  
  40. template<class T> class SimpleArray
  41. {
  42. public:
  43.     operator T*()
  44.     {
  45.         return m_pT;
  46.     }
  47.     T& operator[](int nIndex) {return m_pT[nIndex];}
  48.  
  49. SimpleArray()
  50. {
  51.     m_pT = NULL;
  52.     m_nSize = 0;
  53.     m_nUpperBound = ARR_EMPTY;
  54.     m_nGrowBy = 1;
  55. }
  56.  
  57. //-------------------------------------------------------------------
  58. SimpleArray(int nNewSize, int nGrowBy)
  59. : m_nGrowBy(nGrowBy), m_nUpperBound(ARR_EMPTY), m_nSize(nNewSize)
  60. {
  61.     m_pT = new T[m_nSize];
  62. }
  63.  
  64. //-------------------------------------------------------------------
  65. SimpleArray(T* ptArray, int upbound, int size)
  66. : m_nGrowBy(1), m_pT(ptArray), m_nUpperBound(upbound), m_nSize(size)
  67. {
  68. #ifdef _DEBUG
  69.     if(m_pT == NULL || m_nUpperBound<0 || m_nSize<=0 || m_nUpperBound >= m_nSize)
  70.     {
  71.         m_nSize = 0;
  72.         m_nUpperBound = ARR_EMPTY;
  73.     }
  74. #endif
  75. }
  76.  
  77. //-------------------------------------------------------------------
  78. SimpleArray(SimpleArray& spaArg)
  79.     : m_nSize(spaArg.m_nSize), m_nUpperBound(spaArg.m_nUpperBound),
  80.     m_nGrowBy(spaArg.m_nGrowBy)
  81. {
  82.     m_pT = new T[m_nSize];
  83.     int k;
  84.     for(k = 0; k <= m_nUpperBound; k++)
  85.         m_pT[k] = spaArg.m_pT[k];
  86. }
  87.  
  88. //-------------------------------------------------------------------
  89. ~SimpleArray()
  90. {
  91.     if(m_pT != NULL) delete [] m_pT;
  92. }
  93.  
  94. //-------------------------------------------------------------------
  95. BOOL InsertAtGrow(int nIndex, T argT, int nCount = 1)
  96. {
  97.     if(nIndex<0 || nCount<1)
  98.         return FALSE;
  99.     int i;
  100.     if(nIndex > m_nUpperBound)
  101.     {
  102.         for(i = 0; i < nCount; i++)
  103.             SetAtGrow(nIndex + i, argT);
  104.         return TRUE;
  105.     }
  106.     else
  107.     {
  108.         if(m_nSize < m_nUpperBound + 1 + nCount)
  109.         {
  110.             if (AddSpace(nCount) == FALSE)
  111.                 return FALSE;
  112.         }
  113.         for(i = m_nUpperBound + nCount; i > nIndex; i--)
  114.         {
  115.             m_pT[i] = m_pT[i-nCount];
  116.         }
  117.         for(i = 0; i < nCount; i++)
  118.             m_pT[nIndex + i] = argT;
  119.         m_nUpperBound += nCount;
  120.         return TRUE;
  121.     }
  122. }
  123.  
  124. //-------------------------------------------------------------------
  125. BOOL InsertAtGrow (int nIndex, T* pT, int nSrcIndex, int nCount)
  126. {
  127.     if(nIndex<0 || nCount<1)
  128.         return FALSE;
  129.     int i;
  130.     if(nIndex > m_nUpperBound)
  131.     {
  132.         for(i = 0; i < nCount; i++)
  133.         {
  134.             if (SetAtGrowRef(nIndex + i, pT[nSrcIndex+i]) == FALSE)
  135.                 return FALSE;
  136.         }
  137.         return TRUE;
  138.     }
  139.     else
  140.     {
  141.         if(m_nSize < m_nUpperBound + 1 + nCount)
  142.         {
  143.             if (AddSpace(nCount) == FALSE)
  144.                 return FALSE;
  145.         }
  146.         for(i = m_nUpperBound + nCount; i > nIndex; i--)
  147.         {
  148.             m_pT[i] = m_pT[i-nCount];
  149.         }
  150.         for(i = 0; i < nCount; i++)
  151.             m_pT[nIndex + i] = pT[nSrcIndex+i];
  152.         m_nUpperBound += nCount;
  153.         return TRUE;
  154.     }
  155. }
  156.  
  157.  
  158. //-------------------------------------------------------------------
  159. BOOL InsertAt( int nIndex, T argT, int nCount = 1)
  160. {
  161.     // Valid index?
  162.     if( nIndex < 0 || nIndex > m_nUpperBound )
  163.         return FALSE;
  164.  
  165.     int i;
  166.     // Is there enough space after m_nUpperBound for inserting?
  167.     if( ( m_nSize - 1 ) - m_nUpperBound >= nCount )
  168.     {
  169.         // Yes, no need to allocate more memory.
  170.         // Push up the elements at the current position.
  171.         for( i = m_nUpperBound + nCount; i >= nIndex + nCount; i-- )
  172.             m_pT[ i ] = m_pT[ i - nCount ];
  173.         // Copy in the new data.
  174.         for( i = 0; i < nCount; i++ )
  175.             m_pT[ nIndex + i ] = argT;
  176.         // Adjust m_nUpperBound to new size.
  177.         m_nUpperBound += nCount;
  178.     }
  179.     else
  180.     {
  181.         // No, need to allocate more memory.
  182.         for (i = m_nSize - 1; i >= nIndex + nCount; i--)
  183.             m_pT[i] = m_pT[i - nCount];
  184.         if (m_nSize - nIndex < nCount)
  185.             nCount = m_nSize - nIndex;
  186.         for (i = 0; i < nCount; i++)
  187.             m_pT[nIndex + i] = argT;
  188.         m_nUpperBound = m_nSize - 1;        
  189.     }
  190.     return TRUE;
  191. }
  192.  
  193. //-------------------------------------------------------------------
  194. BOOL RemoveAt(int nIndex, int nCount = 1)
  195. {
  196.     if(nIndex < 0 || nIndex > m_nUpperBound || nCount < 1) return FALSE;
  197.     if(nCount > m_nUpperBound - nIndex)
  198.     {
  199.         m_nUpperBound = nIndex - 1;
  200.         return TRUE;
  201.     }
  202.     int i;
  203.     for(i = nIndex; i <= m_nUpperBound - nCount; i++)
  204.     {
  205.         m_pT[i] = m_pT[i + nCount];
  206.     }
  207.     m_nUpperBound -= nCount;
  208.     return TRUE;
  209. }
  210.  
  211. //-------------------------------------------------------------------
  212. void SetAtGrow(int nIndex, T argT)
  213. {
  214.     if(nIndex < 0) return;
  215.     if(nIndex > m_nSize - 1)
  216.         AddSpace(nIndex - m_nSize + 1);
  217.     m_pT[nIndex] = argT;
  218.     if(nIndex > m_nUpperBound) m_nUpperBound = nIndex;
  219. }
  220.  
  221. //-------------------------------------------------------------------
  222. BOOL SetAtGrowRef(int nIndex, T& argT)
  223. {
  224.     if(nIndex < 0)
  225.         return FALSE;
  226.     if(nIndex > m_nSize - 1)
  227.     {
  228.         if (AddSpace(nIndex - m_nSize + 1) == FALSE)
  229.             return FALSE;
  230.     }
  231.     m_pT[nIndex] = argT;
  232.     if(nIndex > m_nUpperBound)
  233.         m_nUpperBound = nIndex;
  234.     return TRUE;
  235. }
  236.  
  237. //-------------------------------------------------------------------
  238. void SetAt(int nIndex, T argT)
  239. {
  240.     if(nIndex >= 0 && nIndex < m_nSize)
  241.     {
  242.         m_pT[nIndex] = argT;
  243.         if(nIndex > m_nUpperBound)
  244.             m_nUpperBound = nIndex;
  245.     }
  246.     else
  247.         return;
  248. }
  249.  
  250. //-------------------------------------------------------------------
  251. void SetAtRef(int nIndex, T& argT)
  252. {
  253.     if(nIndex >= 0 && nIndex < m_nSize)
  254.     {
  255.         m_pT[nIndex] = argT;
  256.         if(nIndex > m_nUpperBound)
  257.             m_nUpperBound = nIndex;
  258.     }
  259.     else
  260.         return;
  261. }
  262.  
  263. //-------------------------------------------------------------------
  264. T GetAt(int nIndex)
  265. {
  266.     return m_pT[nIndex];
  267. }
  268.  
  269. //-------------------------------------------------------------------
  270. BOOL AddSpace(int nExtend)
  271. {
  272.     int newsize = m_nSize + (((nExtend-1) / m_nGrowBy) + 1) * m_nGrowBy;
  273.     T* pT = new T[newsize];
  274.     if (pT != NULL)
  275.     {
  276.         int i;
  277.         for(i = 0; i < m_nSize; i++)
  278.             pT[i] = m_pT[i];
  279.         if(m_pT != NULL)
  280.             delete [] m_pT;
  281.         m_pT = pT;
  282.         m_nSize = newsize;
  283.         return TRUE;
  284.     }
  285.     else
  286.         return FALSE;
  287. }
  288.  
  289. //-------------------------------------------------------------------
  290. int GetGrowBy()
  291. {
  292.     return m_nGrowBy;
  293. }
  294.  
  295. //-------------------------------------------------------------------
  296. int GetSize()
  297. {
  298.     return m_nSize;
  299. }
  300.  
  301. //-------------------------------------------------------------------
  302. int GetUpperBound()
  303. {
  304.     return m_nUpperBound;
  305. }
  306.  
  307. //-------------------------------------------------------------------
  308. int GetLength()
  309. {
  310.     return m_nUpperBound+1;
  311. }
  312.  
  313. //-------------------------------------------------------------------
  314. BOOL SetSize(int nNewSize, int nGrowBy = 0 )
  315. {
  316.     if(nNewSize < 0)
  317.         return FALSE;
  318.     if (nNewSize == m_nSize)
  319.         return TRUE;
  320.     if( nNewSize == 0 )
  321.     {
  322.         ClearAll();
  323.         return TRUE;
  324.     }
  325.  
  326.     T* pT = new T[nNewSize];
  327.     if (pT == NULL)
  328.         return FALSE;
  329.     int i;
  330.     if(m_nUpperBound < nNewSize)
  331.     {
  332.         for(i = 0; i <= m_nUpperBound; i++)
  333.             pT[i] = m_pT[i];
  334.     }
  335.     else
  336.     {
  337.         for(i = 0; i < nNewSize; i++)
  338.             pT[i] = m_pT[i];
  339.         m_nUpperBound = nNewSize - 1;
  340.     }
  341.  
  342.     if(m_pT != NULL)
  343.         delete [] m_pT;
  344.     m_pT = pT;
  345.     m_nSize = nNewSize;
  346.     if(nGrowBy > 0)
  347.         m_nGrowBy = nGrowBy;
  348.     return TRUE;
  349. }
  350.  
  351. //-------------------------------------------------------------------
  352. void SetGrowBy(int nGrowBy)
  353. {
  354.     if(nGrowBy > 0) m_nGrowBy = nGrowBy;
  355. }
  356.  
  357. //-------------------------------------------------------------------
  358. void ClearAll()
  359. {
  360.     if(m_pT != NULL)
  361.         delete [] m_pT;
  362.     m_pT = NULL;
  363.     m_nSize = 0;
  364.     m_nUpperBound = ARR_EMPTY;
  365. }
  366.  
  367. //-------------------------------------------------------------------
  368. T& GetRefAt(int nIndex)
  369. {
  370.     return m_pT[nIndex];
  371. }
  372.  
  373. //-------------------------------------------------------------------
  374. void SetUpperBound(int upbnd)
  375. {
  376.     if(upbnd < m_nSize)
  377.         m_nUpperBound = upbnd;
  378. }
  379.  
  380. //-------------------------------------------------------------------
  381. BOOL ExpandToSize()
  382. {
  383.     m_nUpperBound = m_nSize - 1;
  384.     return TRUE;
  385. }
  386.  
  387. //-------------------------------------------------------------------
  388. BOOL CopyFrom( int index, T* pSrc, int srclen )
  389. {
  390.     if( m_nSize - index >= srclen )
  391.     {
  392.         // Enough space to copy into.
  393.         int i;
  394.         for( i = 0; i < srclen; i++ )
  395.             m_pT[ index + i ] = pSrc[ i ];
  396.         if( index + srclen - 1 > m_nUpperBound )
  397.             m_nUpperBound = index + srclen - 1;
  398.         return TRUE;
  399.     }
  400.     else
  401.         return FALSE;
  402. }
  403.  
  404. //-------------------------------------------------------------------
  405. T* GetBuffer( int index, int srclen )
  406. {
  407.     if( m_nSize - index >= srclen )
  408.     {
  409.         // Enough space to copy into.
  410.         if( index + srclen - 1 > m_nUpperBound )
  411.             m_nUpperBound = index + srclen - 1;
  412.         return m_pT + index;
  413.     }
  414.     else
  415.         return NULL;
  416. }
  417. protected:
  418.     T* m_pT;
  419.     int m_nSize;
  420.     int m_nUpperBound;
  421.     int m_nGrowBy;
  422. };
  423.  
  424. #endif // #if !defined(__SIMPLEARRAY_H)
  425.