Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

RmArray.h

00001 //=============================================================================
00002 // filename: RmArray.h                                                         
00003 //                                                                             
00004 //           ATI Research, Inc.                                                
00005 //           3D Application Research Group                                     
00006 //                                                                             
00007 // Description: RenderMonkey's Templated Array Class                           
00008 //                                                                             
00009 //=============================================================================
00010 //   (C) 2004 ATI Research, Inc.  All rights reserved.                         
00011 //=============================================================================
00012 
00013 #ifndef _RM_CORE_ARRAY_H_
00014 #define _RM_CORE_ARRAY_H_
00015 
00016 #include <Core/RmTypes.h>
00017 
00018 //-----------------------------------------------------------------------------
00025 //
00034 //-----------------------------------------------------------------------------
00035 template <class Type>
00036 class RmArray
00037 {
00038 public : 
00039    //--------------------------------------------------------------------------
00046    //--------------------------------------------------------------------------
00047    class const_iterator
00048    {
00049       friend RmArray<Type>;
00050 
00051    public :
00052       //-----------------------------------------------------------------------
00057       //-----------------------------------------------------------------------
00058       const_iterator() : 
00059           m_index(0) 
00060       {
00061       }; // End of Constructor
00062 
00063       //-----------------------------------------------------------------------
00068       //-----------------------------------------------------------------------
00069       const_iterator( RmArray *pArray, int nIndex ) : 
00070           m_pArray(pArray),
00071           m_index(nIndex)
00072       {
00073       }; // End of Constructor
00074 
00075       //-----------------------------------------------------------------------
00080       //-----------------------------------------------------------------------
00081       ~const_iterator() {};
00082 
00083       //-----------------------------------------------------------------------
00084       // Operators
00085       //-----------------------------------------------------------------------
00086 
00087       //-----------------------------------------------------------------------
00093       //-----------------------------------------------------------------------
00094       const_iterator& operator++ ()
00095       {
00096          m_index++;
00097          return *this;
00098       }; // End of operator ++
00099 
00100       //-----------------------------------------------------------------------
00106       //-----------------------------------------------------------------------
00107       const_iterator operator++ ( int )
00108       {
00109          const_iterator itr(m_pArray,m_index);
00110          m_index++;
00111 
00112          return itr;
00113       }; // End of operator ++
00114 
00115       //-----------------------------------------------------------------------
00121       //-----------------------------------------------------------------------
00122       const_iterator& operator-- ()
00123       {
00124          m_index--;
00125          return *this;
00126       }; // End of operator ++
00127 
00128       //-----------------------------------------------------------------------
00134       //-----------------------------------------------------------------------
00135       const_iterator operator-- ( int )
00136       {
00137          const_iterator itr(m_pArray,m_index);
00138          m_index--;
00139 
00140          return itr;
00141       }; // End of operator ++
00142 
00143       //-----------------------------------------------------------------------
00149       //-----------------------------------------------------------------------
00150       const Type& operator * () const
00151       {
00152          assert(m_pArray!=NULL);
00153          return (*m_pArray)[m_index];
00154       }; // End of operator *
00155 
00156       //-----------------------------------------------------------------------
00163       //-----------------------------------------------------------------------
00164       bool operator== ( const const_iterator &src )
00165       {
00166          if (m_index == src.m_index)
00167             return true;
00168 
00169          return false;
00170       }; // End of operator ==
00171 
00172       //-----------------------------------------------------------------------
00179       //-----------------------------------------------------------------------
00180       bool operator!= ( const const_iterator &src )
00181       {
00182          if (m_index != src.m_index)
00183             return true;
00184 
00185          return false;
00186       }; // End of operator ==
00187 
00188       //-----------------------------------------------------------------------
00195       //-----------------------------------------------------------------------
00196       bool operator > ( const const_iterator &src )
00197       {
00198          return (m_index>src.m_index);
00199       }; // End of operator >
00200 
00201       //-----------------------------------------------------------------------
00208       //-----------------------------------------------------------------------
00209       bool operator < ( const const_iterator &src )
00210       {
00211          return (m_index<src.m_index);
00212       }; // End of operator <
00213 
00214       //-----------------------------------------------------------------------
00222       //-----------------------------------------------------------------------
00223       bool operator >= ( const const_iterator &src )
00224       {
00225          return (m_index>src.m_index);
00226       }; // End of operator >=
00227 
00228       //-----------------------------------------------------------------------
00236       //-----------------------------------------------------------------------
00237       bool operator <= ( const const_iterator &src )
00238       {
00239          return (m_index<=src.m_index);
00240       }; // End of operator <=
00241 
00242    protected :
00243       RmArray            *m_pArray; 
00244       int                 m_index;  
00245    }; // End of const_iterator
00246 
00247    //--------------------------------------------------------------------------
00252    //--------------------------------------------------------------------------
00253    class iterator : public const_iterator
00254    {
00255       friend RmArray<Type>;
00256 
00257    public :
00258       //-----------------------------------------------------------------------
00263       //-----------------------------------------------------------------------
00264       iterator() : const_iterator()
00265       {
00266       }; // End of Constructor
00267 
00268       //-----------------------------------------------------------------------
00273       //-----------------------------------------------------------------------
00274       iterator( RmArray *pArray, int nIndex ) : const_iterator(pArray,nIndex)
00275       {
00276       }; // End of Constructor
00277 
00278       //-----------------------------------------------------------------------
00283       //-----------------------------------------------------------------------
00284       ~iterator() {};
00285 
00286       //-----------------------------------------------------------------------
00287       // Operator
00288       //-----------------------------------------------------------------------
00289 
00290       //-----------------------------------------------------------------------
00296       //-----------------------------------------------------------------------
00297       iterator& operator++ ()
00298       {
00299          m_index++;
00300          return *this;
00301       }; // End of operator ++
00302 
00303       //-----------------------------------------------------------------------
00309       //-----------------------------------------------------------------------
00310       iterator operator++ ( int )
00311       {
00312          iterator itr(m_pArray,m_index);
00313          m_index++;
00314 
00315          return itr;
00316       }; // End of operator ++
00317 
00318       //-----------------------------------------------------------------------
00324       //-----------------------------------------------------------------------
00325       iterator& operator-- ()
00326       {
00327          m_index--;
00328          return *this;
00329       }; // End of operator ++
00330 
00331       //-----------------------------------------------------------------------
00337       //-----------------------------------------------------------------------
00338       iterator operator-- ( int )
00339       {
00340          iterator itr(m_pArray,m_index);
00341          m_index--;
00342 
00343          return itr;
00344       }; // End of operator ++
00345 
00346       //-----------------------------------------------------------------------
00352       //-----------------------------------------------------------------------
00353       Type& operator * ()
00354       {
00355          assert(m_pArray!=NULL);
00356          return (*m_pArray)[m_index];
00357       }; // End of operator *
00358 
00359       //-----------------------------------------------------------------------
00366       //-----------------------------------------------------------------------
00367       bool operator== ( const iterator &src )
00368       {
00369          if (m_index == src.m_index)
00370             return true;
00371 
00372          return false;
00373       }; // End of operator ==
00374 
00375       //-----------------------------------------------------------------------
00382       //-----------------------------------------------------------------------
00383       bool operator!= ( const iterator &src )
00384       {
00385          if (m_index != src.m_index)
00386             return true;
00387 
00388          return false;
00389       }; // End of operator ==
00390 
00391       //-----------------------------------------------------------------------
00399       //-----------------------------------------------------------------------
00400       bool operator > ( const iterator &src )
00401       {
00402          return (m_index>src.m_index);
00403       }; // End of operator >
00404 
00405       //-----------------------------------------------------------------------
00412       //-----------------------------------------------------------------------
00413       bool operator < ( const iterator &src )
00414       {
00415          return (m_index<src.m_index);
00416       }; // End of operator <
00417 
00418       //-----------------------------------------------------------------------
00426       //-----------------------------------------------------------------------
00427       bool operator >= ( const iterator &src )
00428       {
00429          return (m_index>src.m_index);
00430       }; // End of operator >=
00431 
00432       //-----------------------------------------------------------------------
00440       //-----------------------------------------------------------------------
00441       bool operator <= ( const iterator &src )
00442       {
00443          return (m_index<=src.m_index);
00444       }; // End of operator <=
00445 
00446    }; // End of iterator
00447 
00448 public :
00449    //--------------------------------------------------------------------------
00454    //--------------------------------------------------------------------------
00455    RmArray() : 
00456       m_nNumItems(0),
00457       m_nNumReserved(0),
00458       m_pTable(NULL),
00459       m_incrementSize(1)
00460    {
00461    }; // End of Constructor
00462 
00463    //--------------------------------------------------------------------------
00469    //--------------------------------------------------------------------------
00470    RmArray( const RmArray<Type> &src ) : 
00471       m_nNumItems(0),
00472       m_nNumReserved(0),
00473       m_pTable(NULL)
00474    {
00475       if (src.capacity()>0)
00476       {
00477          AllocTable(src.capacity(),false);
00478          memcpy( m_pTable, src.m_pTable, sizeof(Type)*m_nNumReserved );
00479 
00480          m_nNumItems = src.size();
00481       } // End if
00482    }; // End of Constructor
00483 
00484    //--------------------------------------------------------------------------
00489    //--------------------------------------------------------------------------
00490    ~RmArray()
00491    {
00492       clear();
00493    }; // End of Destructor
00494 
00495    //--------------------------------------------------------------------------
00496    // Information Accessor
00497    //--------------------------------------------------------------------------
00498    
00499    //--------------------------------------------------------------------------
00505    //--------------------------------------------------------------------------
00506    int  size() const { return m_nNumItems; };
00507 
00508    //--------------------------------------------------------------------------
00514    //--------------------------------------------------------------------------
00515    bool empty() const { return (m_nNumItems==0); };
00516 
00517    //--------------------------------------------------------------------------
00523    //--------------------------------------------------------------------------
00524    int  capacity() const { return m_nNumReserved; };
00525 
00526    //--------------------------------------------------------------------------
00535    //--------------------------------------------------------------------------
00536    bool reserve( int reserveSize )
00537    {
00538       if (AllocTable(reserveSize,true)==false)
00539          return false;
00540 
00541       if (m_nNumReserved<m_nNumItems)
00542          m_nNumItems = m_nNumReserved;
00543 
00544       return true;
00545    }; // End of reserve
00546 
00547    //--------------------------------------------------------------------------
00554    //--------------------------------------------------------------------------
00555    bool resize( int newSize )
00556    {
00557       if (newSize>=m_nNumReserved)
00558       {
00559          if (AllocTable(newSize,true)==false)
00560             return false;
00561       } // End if
00562 
00563       m_nNumItems = newSize;
00564       return true;
00565    }; // End of resize
00566 
00567    //--------------------------------------------------------------------------
00573    //--------------------------------------------------------------------------
00574    bool clear()
00575    {
00576       DeallocTable();
00577       return true;
00578    }; // End of reserve
00579 
00580    //--------------------------------------------------------------------------
00587    //--------------------------------------------------------------------------
00588    bool push_back( Type data )
00589    {
00590       if (m_nNumItems>=m_nNumReserved)
00591       {
00592          if (AllocTable(m_nNumReserved+m_incrementSize,true)==false)
00593             return false;
00594       } // End if
00595 
00596       m_pTable[m_nNumItems] = data;
00597       m_nNumItems++;
00598       return true;
00599    }; // End of push_back
00600 
00601    //--------------------------------------------------------------------------
00608    //--------------------------------------------------------------------------
00609    bool push_front( Type data )
00610    {
00611       if (m_nNumItems>=m_nNumReserved)
00612       {
00613          if (AllocTable(m_nNumReserved+1,true)==false)
00614             return false;
00615       } // End if
00616 
00617       shiftData(0,1);
00618       m_pTable[0] = data;
00619       m_nNumItems++;
00620 
00621       return true;
00622    }; // End of push_front
00623 
00624    //--------------------------------------------------------------------------
00631    //--------------------------------------------------------------------------
00632    bool remove( Type data )
00633    {
00634       // may need to optimize this later
00635       int i = m_nNumItems-1;
00636       while (i>=0)
00637       {
00638          // Iterate from back and if we find a match, shift data
00639          // to remove data
00640          if (m_pTable[i]==data)
00641          {
00642             shiftData(i+1,-1);            
00643             m_nNumItems--;
00644             i-= 2;
00645          } // End if
00646          else
00647          {
00648             i--;
00649          } // End else
00650       } // End while
00651 
00652       return true;
00653    }; // End of remove
00654 
00655    //--------------------------------------------------------------------------
00662    //--------------------------------------------------------------------------
00663    iterator erase( iterator &itr )
00664    {
00665       assert(itr.m_index<m_nNumItems);
00666       shiftData(itr.m_index+1,-1);
00667 
00668       iterator nextItr(this,itr.m_index);
00669       m_nNumItems--;
00670 
00671       return nextItr;
00672    }; // End of erase
00673 
00674    //--------------------------------------------------------------------------
00675    // Iterator Functions
00676    //--------------------------------------------------------------------------
00677 
00678    //--------------------------------------------------------------------------
00684    //--------------------------------------------------------------------------
00685    iterator begin()
00686    {
00687       iterator itr(this,0);
00688       return itr;
00689    }; // End of begin
00690 
00691    //--------------------------------------------------------------------------
00697    //--------------------------------------------------------------------------
00698    const_iterator begin() const
00699    {
00700       const_iterator itr(this,0);
00701       return itr;
00702    }; // End of begin
00703 
00704    //--------------------------------------------------------------------------
00710    //--------------------------------------------------------------------------
00711    iterator end()
00712    {
00713       iterator itr(this,m_nNumItems);
00714       return itr;
00715    }; // End of end
00716 
00717    //--------------------------------------------------------------------------
00723    //--------------------------------------------------------------------------
00724    const_iterator end() const
00725    {
00726       const_iterator itr(this,m_nNumItems);
00727       return itr;
00728    }; // End of end
00729 
00730    //--------------------------------------------------------------------------
00731    // As Row Table
00732    //--------------------------------------------------------------------------
00733 
00734    //--------------------------------------------------------------------------
00740    //--------------------------------------------------------------------------
00741    operator Type*       () { return m_pTable; };
00742 
00743    //--------------------------------------------------------------------------
00749    //--------------------------------------------------------------------------
00750    operator const Type* () const { return m_pTable; };
00751 
00752    //--------------------------------------------------------------------------
00753    // Indexer functions
00754    //--------------------------------------------------------------------------
00755    
00756    //--------------------------------------------------------------------------
00763    //--------------------------------------------------------------------------
00764    Type& operator[] ( int nIndex )
00765    {
00766       assert(nIndex<m_nNumItems);
00767       return m_pTable[nIndex];
00768    }; // End of operator[]
00769 
00770    //--------------------------------------------------------------------------
00777    //--------------------------------------------------------------------------
00778    const Type& operator[] ( int nIndex ) const
00779    {
00780       assert(nIndex<m_nNumItems);
00781       return m_pTable[nIndex];
00782    }; // End of operator[]
00783 
00784    //--------------------------------------------------------------------------
00791    //--------------------------------------------------------------------------
00792    void operator = ( const RmArray<Type> &src )
00793    {
00794       if (src.capacity()>0)
00795       {
00796          AllocTable(src.capacity(),false);
00797          memcpy( m_pTable, src.m_pTable, sizeof(Type)*m_nNumReserved );
00798 
00799          m_nNumItems = src.size();
00800       } // End if
00801    }; // End of operator =
00802 
00803    //--------------------------------------------------------------------------
00810    //--------------------------------------------------------------------------
00811    void SetIncrementSize( int size )
00812    {
00813       assert(size>0);
00814 
00815       m_incrementSize = size;
00816    }; // End of SetIncrementSize
00817 
00818    //--------------------------------------------------------------------------
00824    //--------------------------------------------------------------------------
00825    int GetIncrementSize() const { return m_incrementSize; };
00826 
00827 private :
00828    int        m_nNumItems;     
00829    int        m_nNumReserved;  
00830    Type      *m_pTable;        
00831 
00832    //--------------------------------------------------------------------------
00833    // Allocation/Deallocation of array
00834    //--------------------------------------------------------------------------
00835    
00836    //--------------------------------------------------------------------------
00844    //--------------------------------------------------------------------------
00845    bool AllocTable( int newSize, bool bPreserve )
00846    {
00847       if (m_nNumReserved==newSize)
00848          return true;
00849 
00850       if (newSize==0)
00851       {
00852          DeallocTable();
00853          return true;
00854       } // End if
00855 
00856       int copySize = m_nNumItems;
00857       if (copySize>newSize)
00858          copySize = newSize;
00859 
00860       Type *pNewTable = (Type*)RmAllocateBuffer(newSize*sizeof(Type));
00861       if (pNewTable==NULL)
00862          return false;
00863 
00864       int i;
00865       for (i=0;i<newSize;i++)
00866       {
00867          pNewTable[i] = Type();
00868       } // End if
00869 
00870       if (bPreserve) // Do the copy, if we have to preserve data
00871       {
00872          for (i=0;i<copySize;i++)
00873          {
00874             pNewTable[i] = m_pTable[i];
00875          } // End for
00876       } // End if
00877 
00878       DeallocTable();
00879       m_pTable       = pNewTable;
00880       m_nNumItems    = copySize;
00881       m_nNumReserved = newSize; 
00882 
00883       return true;
00884    }; // End of AllocTable
00885 
00886    //--------------------------------------------------------------------------
00892    //--------------------------------------------------------------------------
00893    void DeallocTable()
00894    {
00895       if (m_pTable!=NULL)
00896       {
00897          RmDeallocateBuffer((RM_BYTE*)m_pTable);
00898          m_nNumItems = 0;
00899          m_nNumReserved = 0;
00900          m_pTable = NULL;
00901       } // End if
00902    }; // End of DeallocTable
00903 
00904    //--------------------------------------------------------------------------
00916    //--------------------------------------------------------------------------
00917    bool shiftData( int startIndex, int direction )
00918    {
00919       if (direction>0)
00920       {
00921          assert((m_nNumItems+direction)<=m_nNumReserved);
00922          int i;
00923          for (i=m_nNumItems-1;i>=startIndex;i--)
00924          {
00925             m_pTable[i+direction] = m_pTable[i];
00926          } // End for
00927       } // End if
00928       else
00929       {
00930          assert((m_nNumItems+direction)>=0);
00931          int i;
00932          for (i=startIndex;i<m_nNumItems;i++)
00933          {
00934             m_pTable[i+direction] = m_pTable[i];
00935          } // End for
00936       } // End else
00937 
00938       return true;
00939    } // End of shiftData
00940 
00941 private :
00942    int m_incrementSize; 
00943 }; // End of RmArray
00944 
00945 
00946 #endif

Generated on Fri Feb 25 16:08:33 2005 for RenderMonkey SDK by doxygen 1.3.6