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