Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. #define AR_CHUNK_SIZE   64
  2.  
  3. //
  4. template<class TYPE>
  5. class MCArray
  6. {
  7. protected:
  8.         TYPE * _dataPtr;
  9.         int _elementsCount;
  10.         int _capacity;
  11.  
  12. public:
  13.         MCArray();
  14.         virtual ~MCArray();
  15.         virtual int Add( const TYPE &element );
  16.         virtual int AddExclusive( const TYPE &element );
  17.         virtual TYPE & GetAt( int Ndx );
  18.         virtual TYPE & operator [] ( int Ndx );
  19.         virtual int Find( int startNdx, const TYPE & element );
  20.         virtual int RemoveAt( int Ndx );
  21.         virtual void Clear(void);
  22.         virtual int GetCount(void);
  23. };
  24.  
  25. //
  26.  
  27.  
  28. //
  29. template<class TYPE>
  30. MCArray<TYPE>::MCArray()
  31. {
  32.         // óñòàíàâëèâàåì ïåðåìåííûå
  33.         this->_dataPtr = NULL;
  34.         this->_capacity = 0;
  35.         this->_elementsCount = 0;
  36. }
  37.  
  38.  
  39. //
  40. template<class TYPE>
  41. MCArray<TYPE>::~MCArray()
  42. {
  43.         //
  44.         this->_capacity = 0;
  45.         this->_elementsCount = 0;
  46.         //
  47.         if ( this->_dataPtr != NULL )
  48.         {
  49.                 delete this->_dataPtr;
  50.         }
  51. }
  52.  
  53.  
  54. //
  55. template<class TYPE>
  56. int MCArray<TYPE>::AddExclusive( const TYPE &element )
  57. {
  58.         //
  59.         if ( this->Find( 0, element ) < 0 )
  60.                 return this->Add( element );
  61.         else
  62.                 return -1;
  63. }
  64.  
  65.  
  66. //
  67. template<class TYPE>
  68. int MCArray<TYPE>::Add( const TYPE &element )
  69. {
  70.         TYPE * dPtr;
  71.  
  72.         // åñòü ëè ìåñòî?
  73.         if ( this->_elementsCount >= this->_capacity )
  74.         {
  75.                 // çàíèìàåì åù¸ ïàìÿòè
  76.                 dPtr = new TYPE [this->_capacity + AR_CHUNK_SIZE];
  77.                 // ïðîâåðêà
  78.                 if ( dPtr == NULL )
  79.                 {
  80.                         //
  81.                         return -1;
  82.                 }
  83.  
  84.                 if ( this->_capacity > 0 )
  85.                 {
  86.                         // ñêîïèðóåì ñóùåñòâóþùèå äàííûå íà íîâîå ìåñòî
  87.                         memcpy( dPtr, this->_dataPtr, sizeof(TYPE) * this->_capacity );
  88.                         // óäàëèì ñòàðóþ êîïèþ äàííûõ
  89.                         delete this->_dataPtr;
  90.                 }
  91.                 // ñêîððåêòèðóåì ðàçìåð
  92.                 this->_capacity += AR_CHUNK_SIZE;
  93.                 // ñêîððåêòèðóåì óêàçàòåëü íà äàííûå
  94.                 this->_dataPtr = dPtr;
  95.         }
  96.  
  97.         // êîïèðóåì ýëåìåíò â ìàññèâ
  98.         this->_dataPtr[this->_elementsCount] = element;
  99.         //memcpy( this->_dataPtr + this->_elementsCount, &element, sizeof(TYPE) );
  100.  
  101.         // óâåëè÷èâàåì ñ÷¸ò÷èê ýëåìåíòîâ
  102.         return  ++this->_elementsCount;
  103. }
  104.  
  105.  
  106. //
  107. template<class TYPE>
  108. TYPE & MCArray<TYPE>::GetAt( int Ndx )
  109. {
  110.         //assert( Ndx >= 0 && Ndx < this->_elementsCount );
  111.         return this->_dataPtr[Ndx];
  112. }
  113.  
  114.  
  115. //
  116. template<class TYPE>
  117. TYPE & MCArray<TYPE>::operator [] ( int Ndx )
  118. {
  119.         return this->GetAt( Ndx );
  120. }
  121.  
  122.  
  123. //
  124. template<class TYPE>
  125. int MCArray<TYPE>::Find( int startNdx, const TYPE & element )
  126. {
  127.         int i;
  128.  
  129.         if ( startNdx < 0 || startNdx >= this->_elementsCount )
  130.         {
  131.                 return -1;
  132.         }
  133.  
  134.         for ( i = startNdx; i < this->_elementsCount; i++ )
  135.         {
  136.                 if ( element == this->_dataPtr[i] )
  137.                 {
  138.                         return i;
  139.                 }
  140.         }
  141.  
  142.         return -1;
  143. }
  144.  
  145.  
  146. //
  147. template<class TYPE>
  148. int MCArray<TYPE>::RemoveAt( int Ndx )
  149. {
  150.         int mn;
  151.  
  152.         if ( Ndx < 0 || Ndx >= this->_elementsCount )
  153.         {
  154.                 return 0;
  155.         }
  156.  
  157.         mn = this->_elementsCount - Ndx;
  158.  
  159.         if ( mn != 1 )
  160.         {
  161.                 memcpy( this->_dataPtr + Ndx, this->_dataPtr + Ndx + 1, sizeof(TYPE) * ( mn - 1 ) );
  162.         }
  163.  
  164.         this->_elementsCount--;
  165.         return 1;
  166. }
  167.  
  168.  
  169. //
  170. template<class TYPE>
  171. void MCArray<TYPE>::Clear()
  172. {
  173.         this->_elementsCount = 0;
  174. }
  175.  
  176. //
  177. template<class TYPE>
  178. int MCArray<TYPE>::GetCount()
  179. {
  180.         return this->_elementsCount;
  181. }
  182.