Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

vaarray.hpp

Go to the documentation of this file.
00001 #if !defined(_VAARRAY_HPP)
00002 #define _VAARRAY_HPP
00003 //============================================================================= 
00004 //
00005 // XDFLengine library
00006 //
00007 //----------------------------------------------------------------------------- 
00008 //  VAARRAY.HPP
00009 //----------------------------------------------------------------------------- 
00015 //_____________________________________________________________________________
00016 //
00017 //  Copyright (C) 2003 Guillaume Baurand. All Rights Reserved.
00018 //
00019 //  This file is part of the XDFLengine project.
00020 //
00021 //  The XDFLengine is free software; you can redistribute it and/or modify
00022 //  it under the terms of the GNU General Public License as published by
00023 //  the Free Software Foundation; either version 2 of the License, or
00024 //  (at your option) any later version.
00025 //
00026 //  This program is distributed in the hope that it will be useful,
00027 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00028 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00029 //  GNU General Public License for more details.
00030 //
00031 //  You should have received a copy of the GNU General Public License
00032 //  along with this program; if not, write to the Free Software
00033 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
00034 //  USA.
00035 //
00036 //  For more information, 
00037 //      contact  : guillaume@baurand.net 
00038 //      or visit : http://xdflengine.sourceforge.net
00039 //
00040 //============================================================================= 
00041 
00042 #   include "config/commonincs.hpp"
00043 
00044 
00045 
00046 BEGIN_XDFLENGINE_NS
00047 
00048 //============================================================================= 
00049 template<class T_CLASS >
00050 class XDFLENGINE_EXPORT VAelem
00051 {
00052     public:
00053     T_CLASS             m_pObject;
00054     VAelem*             m_pBefore;
00055     VAelem*             m_pAfter;
00056     char*               m_pszKey;
00057 } ;
00058 
00059 //============================================================================= 
00060 
00061 
00062 
00063 
00064 
00065 //============================================================================= 
00066 //  CLASS VAARRAY
00067 //----------------------------------------------------------------------------- 
00069 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00070 template<class T_CLASS >
00071 class XDFLENGINE_EXPORT VAarray
00072 {
00073 
00074     private: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00075 
00076         VAelem<T_CLASS >*               m_pFirstElem;
00077         VAelem<T_CLASS >*               m_pLastElem;
00078         VAelem<T_CLASS >*               m_pCurElem;
00079         //mutable   VAelem<T_CLASS >*       m_pRefElem;
00080         unsigned int                    m_uiSize;
00081         
00082         
00083     private: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00084 
00085     //_________________________________________________________________________
00086     //  CREATEELEM
00087     //-------------------------------------------------------------------------
00088     
00089     VAelem<T_CLASS>* createElem( T_CLASS  p_pObject=0, const char* p_pszKey=0) const
00090     {
00091         //---------------------------------------------------------------------
00092         VAelem<T_CLASS>*    l_pElem=0;
00093         //---------------------------------------------------------------------
00094 
00095         l_pElem = new VAelem<T_CLASS>;
00096         l_pElem->m_pObject = p_pObject;
00097         l_pElem->m_pBefore = 0;
00098         l_pElem->m_pAfter = 0;
00099         l_pElem->m_pszKey = importCharBuffer( 0, p_pszKey);
00100 
00101         return l_pElem;     
00102     }
00103 
00104     //_________________________________________________________________________
00105     //  DELETEELEM
00106     //-------------------------------------------------------------------------
00107     
00108     void deleteElem( VAelem<T_CLASS>* p_pElem) const
00109     {
00110         p_pElem->m_pszKey = releaseCharBuffer( p_pElem->m_pszKey);
00111         delete p_pElem;
00112     }
00113 
00114     //_________________________________________________________________________
00115     //  DELETEELEM
00116     //-------------------------------------------------------------------------
00117     
00118     VAelem<T_CLASS>* removeElem( VAelem<T_CLASS>* p_pElem) 
00119     {
00120         //if( m_pRefElem == p_pElem) m_pRefElem=p_pElem->m_pAfter;
00121         if( m_pCurElem == p_pElem) m_pCurElem=p_pElem->m_pAfter;
00122 
00123         if( p_pElem->m_pBefore) p_pElem->m_pBefore->m_pAfter = p_pElem->m_pAfter;
00124         else m_pFirstElem = p_pElem->m_pAfter;
00125 
00126         if( p_pElem->m_pAfter) p_pElem->m_pAfter->m_pBefore = p_pElem->m_pBefore;
00127         else m_pLastElem = p_pElem->m_pBefore;
00128 
00129         m_uiSize--;
00130 
00131         p_pElem->m_pAfter=0;
00132         p_pElem->m_pBefore=0;
00133 
00134         return p_pElem;
00135     }
00136 
00137     //_________________________________________________________________________
00138     //  INSERTELEM
00139     //-------------------------------------------------------------------------
00140     
00141     void insertElem( VAelem<T_CLASS>* p_pElem, VAelem<T_CLASS>* p_pRefElem=0, bool p_fBefore=false)
00142     {
00143         if(!p_pRefElem) p_pRefElem = m_pLastElem;
00144         if(!p_pRefElem)
00145         {
00146             m_pLastElem = p_pElem;
00147             m_pFirstElem = p_pElem;
00148         }
00149         else
00150         {
00151             if( p_fBefore)
00152             {
00153                 p_pElem->m_pBefore = 0;
00154                 if( p_pRefElem->m_pBefore)
00155                 {
00156                     p_pRefElem->m_pBefore->m_pAfter = p_pElem;
00157                     p_pElem->m_pBefore = p_pRefElem->m_pBefore;
00158                 }
00159                 else
00160                 {
00161                     m_pFirstElem=p_pElem;
00162                 }
00163                 p_pRefElem->m_pBefore = p_pElem;
00164                 p_pElem->m_pAfter = p_pRefElem;
00165             }
00166             else
00167             {
00168                 p_pElem->m_pAfter = 0;
00169                 if( p_pRefElem->m_pAfter)
00170                 {
00171                     p_pRefElem->m_pAfter->m_pBefore=p_pElem;
00172                     p_pElem->m_pAfter=p_pRefElem->m_pAfter;
00173                 }
00174                 else
00175                 {
00176                     m_pLastElem=p_pElem;
00177                 }
00178                 p_pRefElem->m_pAfter=p_pElem;
00179                 p_pElem->m_pBefore = p_pRefElem;
00180             }
00181         }
00182 
00183         m_uiSize++;
00184     }  
00185 
00186 
00187     //_________________________________________________________________________
00188     //  GETELEM
00189     //-------------------------------------------------------------------------
00190     
00191     VAelem<T_CLASS>* getElem ( unsigned int p_uiIndex, VAelem<T_CLASS>* p_pRefElem=0) const
00192     {
00193         //---------------------------------------------------------------------
00194         unsigned int i;
00195         VAelem<T_CLASS>* p_pCurElem;
00196         //---------------------------------------------------------------------
00197 
00198         if( ! p_pRefElem ) p_pRefElem = m_pFirstElem;
00199         if( empty() ) return 0;
00200         if( p_uiIndex > m_uiSize-1 ) return 0;
00201 
00202         p_pCurElem = m_pFirstElem;
00203         for( i=0 ; i<p_uiIndex ; i++) p_pCurElem = p_pCurElem->m_pAfter;        
00204         return p_pCurElem;
00205     }
00206 
00207     //_________________________________________________________________________
00208     //  GETELEM
00209     //------------------------------------------------------------------------- 
00210     VAelem<T_CLASS>* getElem ( const char* p_pszKey, unsigned int p_uiIndex, VAelem<T_CLASS>* p_pRefElem=0) const
00211     {
00212         //---------------------------------------------------------------------
00213         unsigned int l_uiCurIndex;
00214         VAelem<T_CLASS>* l_pCurElem;
00215         //unsigned int l_uiItrts=0;
00216         //---------------------------------------------------------------------
00217         //if( ! m_pRefElem ) m_pRefElem = m_pFirstElem;
00218         if( ! p_pRefElem ) p_pRefElem = m_pFirstElem ; //m_pRefElem;
00219         if( empty() ) return 0;
00220         if( p_uiIndex > m_uiSize-1 ) return 0;
00221 
00222         l_pCurElem = p_pRefElem;
00223         l_uiCurIndex = p_uiIndex;
00224         while( true)
00225         {
00226             if( l_pCurElem->m_pszKey )
00227             {
00228                 if ( strcmp( l_pCurElem->m_pszKey, p_pszKey)==0 ) 
00229                 {
00230                     if( !l_uiCurIndex)
00231                     {
00232                         //m_pRefElem = l_pCurElem;
00233                         return l_pCurElem;
00234                     }
00235                     else l_uiCurIndex--;
00236                     //l_uiItrts=0;
00237                 }
00238             }
00239             if(l_pCurElem->m_pAfter) l_pCurElem = l_pCurElem->m_pAfter; 
00240             else return 0; //l_pCurElem = m_pFirstElem;
00241             //l_uiItrts++;
00242             //if(l_uiItrts >= m_uiSize) return 0;
00243         }
00244     }
00245 
00246     //_________________________________________________________________________
00247     //  COUNTITEM
00248     //-------------------------------------------------------------------------
00249     unsigned int countElem(  const char* p_pszKey) const
00250     {
00251         //---------------------------------------------------------------------
00252         unsigned int l_uiCount=0;
00253         VAelem<T_CLASS>* l_pCurElem;
00254         //---------------------------------------------------------------------
00255 
00256         l_uiCount=0;
00257         l_pCurElem = m_pFirstElem; 
00258         while(true)
00259         {
00260             if(! l_pCurElem) return l_uiCount;
00261             if( strcmp( l_pCurElem->m_pszKey, p_pszKey) ==0 ) l_uiCount++;
00262             l_pCurElem = l_pCurElem->m_pAfter;
00263         }
00264     }
00265 
00266 
00267     public: //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00268 
00269 
00270 // constructor and destructor
00271 
00272     //_________________________________________________________________________
00273     //  CREATEELEM
00274     //-------------------------------------------------------------------------
00275     
00276     VAarray<T_CLASS>()
00277     {
00278         m_pFirstElem=0;
00279         m_pLastElem=0;
00280         m_uiSize=0;
00281         //m_pRefElem=0;
00282         //DEBUG_CREATE(VAarray)
00283     }
00284 
00285     //_________________________________________________________________________
00286     //  CREATEELEM
00287     //-------------------------------------------------------------------------
00288     
00289     ~VAarray<T_CLASS>()
00290     {
00291         //DEBUG_DEL(VAarray)
00292         flush();
00293     }
00294 
00295 
00296 // LIFO stack related functions
00297 
00298     //_________________________________________________________________________
00299     //  CREATEELEM
00300     //-------------------------------------------------------------------------
00301     
00302     T_CLASS  top() const
00303     {
00304         if( m_pLastElem) return m_pLastElem->m_pObject;
00305         else return 0;
00306     }
00307 
00308     //_________________________________________________________________________
00309     //  CREATEELEM
00310     //-------------------------------------------------------------------------
00311     
00312     T_CLASS  bottom() const
00313     {
00314         if( m_pFirstElem) return m_pFirstElem->m_pObject;
00315         else return 0;
00316     }
00317         
00318     //_________________________________________________________________________
00319     //  CREATEELEM
00320     //-------------------------------------------------------------------------
00321     
00322     T_CLASS  pop()
00323     {
00324         //---------------------------------------------------------------------
00325         T_CLASS  l_pRetObject=0;
00326         //---------------------------------------------------------------------
00327 
00328         if( m_pLastElem)
00329         {
00330             l_pRetObject = m_pLastElem->m_pObject;
00331             deleteElem( removeElem( m_pLastElem));
00332         }
00333         return l_pRetObject;
00334     }
00335 
00336     //_________________________________________________________________________
00337     //  CREATEELEM
00338     //-------------------------------------------------------------------------
00339     
00340     void push( T_CLASS  p_pObject)
00341     {
00342         insertElem( createElem( p_pObject));
00343     }
00344 
00345     //_________________________________________________________________________
00346     //  CREATEELEM
00347     //-------------------------------------------------------------------------
00348     
00349     bool empty() const
00350     {
00351         if(m_uiSize==0) return true;
00352         else return false;
00353 
00354     }
00355 
00356 
00357 // FIFO queue related function
00358 
00359     //_________________________________________________________________________
00360     //  FIRST
00361     //-------------------------------------------------------------------------
00362     
00363     T_CLASS  first() const
00364     {
00365         return top();
00366     }
00367 
00368     //_________________________________________________________________________
00369     //  LAST
00370     //-------------------------------------------------------------------------
00371     
00372     T_CLASS  last() const
00373     {
00374         return bottom();
00375     }
00376 
00377     //_________________________________________________________________________
00378     //  ENQUEUE
00379     //-------------------------------------------------------------------------
00380     
00381     void enqueue( T_CLASS  p_pObject)
00382     {
00383         insertElem( createElem( p_pObject), m_pFirstElem, true);
00384     }
00385 
00386     //_________________________________________________________________________
00387     //  DEQUEUE
00388     //-------------------------------------------------------------------------
00389     
00390     T_CLASS  dequeue()
00391     {
00392         return pop();
00393     }
00394 
00395 
00396 // vector related functions
00397 
00398     //_________________________________________________________________________
00399     //  SIZE
00400     //-------------------------------------------------------------------------
00401     
00402     unsigned int size() const
00403     {
00404         return m_uiSize;
00405     }
00406 
00407     //_________________________________________________________________________
00408     //  GET
00409     //-------------------------------------------------------------------------
00410     
00411     T_CLASS  get( unsigned int p_uiIndex) const
00412     {
00413         //---------------------------------------------------------------------
00414         VAelem<T_CLASS>* l_pElem = 0;
00415         //---------------------------------------------------------------------
00416 
00417         l_pElem=getElem( p_uiIndex);
00418         if(l_pElem) return l_pElem->m_pObject;
00419         return 0;
00420     }
00421 
00422     //_________________________________________________________________________
00423     //  INSERT
00424     //-------------------------------------------------------------------------
00425     
00426     void insert( T_CLASS  p_pObject, unsigned int p_uiIndex, bool p_fBefore)
00427     {
00428         //---------------------------------------------------------------------
00429         VAelem<T_CLASS>* l_pElem = 0;
00430         //---------------------------------------------------------------------
00431 
00432         insertElem( createElem( p_pObject), getElem( p_uiIndex), p_fBefore);
00433     }
00434 
00435     //_________________________________________________________________________
00436     //  REMOVE
00437     //-------------------------------------------------------------------------
00438     
00439     T_CLASS  remove( unsigned int p_uiIndex)
00440     {
00441         //---------------------------------------------------------------------
00442         VAelem<T_CLASS>*        l_pElem = 0;
00443         T_CLASS     l_pObject = 0;
00444         //---------------------------------------------------------------------
00445 
00446         l_pElem = getElem( p_uiIndex);
00447         if(l_pElem)
00448         {
00449             l_pObject = l_pElem->m_pObject;
00450             deleteElem( removeElem( l_pElem));
00451         }
00452         return l_pObject;
00453     }
00454 
00455 
00456 // map related functions
00457 
00458     //_________________________________________________________________________
00459     //  GET
00460     //-------------------------------------------------------------------------
00461     
00462     T_CLASS  get( const char*   p_pszKey, unsigned int p_uiIndex=0) const
00463     {
00464         //---------------------------------------------------------------------
00465         VAelem<T_CLASS>*        l_pElem = 0;
00466         //---------------------------------------------------------------------
00467 
00468         l_pElem = getElem( p_pszKey, p_uiIndex);
00469         if( l_pElem) return l_pElem->m_pObject;
00470         else return 0;
00471     }
00472 
00473     //_________________________________________________________________________
00474     //  ADD
00475     //-------------------------------------------------------------------------
00476     
00477     void add( T_CLASS  p_pObject, const char*   p_pszKey)
00478     {
00479         insertElem( createElem( p_pObject, p_pszKey));
00480     }
00481 
00482     //_________________________________________________________________________
00483     //  REMOVE
00484     //-------------------------------------------------------------------------
00485     
00486     T_CLASS  remove( const char*    p_pszKey, unsigned int p_uiIndex=0)
00487     {
00488         //---------------------------------------------------------------------
00489         VAelem<T_CLASS>*        l_pElem = 0;
00490         T_CLASS     l_pObject = 0;
00491         //---------------------------------------------------------------------
00492 
00493         l_pElem = getElem( p_pszKey, p_uiIndex);
00494         if( l_pElem)
00495         {
00496             l_pObject = l_pElem->m_pObject;
00497             deleteElem( removeElem( l_pElem));
00498         }
00499         return l_pObject;
00500     }
00501 
00502 // sequence
00503 
00504 
00505     //_________________________________________________________________________
00506     //  moveFirst
00507     //-------------------------------------------------------------------------
00508     void moveFirst()
00509     {
00510         m_pCurElem=m_pFirstElem;
00511     }
00512 
00513 
00514     //_________________________________________________________________________
00515     //  moveLast
00516     //-------------------------------------------------------------------------
00517     void moveLast()
00518     {
00519         m_pCurElem=m_pLastElem;
00520     }
00521 
00522 
00523     //_________________________________________________________________________
00524     //  moveNext
00525     //-------------------------------------------------------------------------
00526     void moveNext()
00527     {
00528         if(m_pCurElem) m_pCurElem = m_pCurElem->m_pAfter;
00529     }
00530 
00531 
00532     //_________________________________________________________________________
00533     //  movePrevious
00534     //-------------------------------------------------------------------------
00535     void movePrevious()
00536     {
00537         if(m_pCurElem)  m_pCurElem = m_pCurElem->m_pBefore;
00538     }
00539 
00540 
00541     //_________________________________________________________________________
00542     //  current
00543     //-------------------------------------------------------------------------
00544     T_CLASS current()
00545     {
00546         if(m_pCurElem) return m_pCurElem->m_pObject;
00547         else return 0;
00548     }
00549 
00550 
00551 // keys management
00552 
00553     //_________________________________________________________________________
00554     //  GETKEY
00555     //-------------------------------------------------------------------------
00556     
00557     const char* getKey( unsigned int p_uiIndex) const
00558     {
00559         //---------------------------------------------------------------------
00560         VAelem<T_CLASS>*        l_pElem = 0;
00561         //---------------------------------------------------------------------
00562 
00563         l_pElem = getElem( p_uiIndex);
00564         if( l_pElem) return exportCharBuffer(l_pElem->m_pszKey);
00565         else return 0;
00566     }
00567 
00568     //_________________________________________________________________________
00569     //  SETKEY
00570     //-------------------------------------------------------------------------
00571     
00572     void setKey( unsigned int p_uiIndex, const char* p_pszNewKey)
00573     {
00574         //---------------------------------------------------------------------
00575         VAelem<T_CLASS>*        l_pElem = 0;
00576         //---------------------------------------------------------------------
00577 
00578         l_pElem = getElem( p_uiIndex);
00579         if( l_pElem)
00580         {
00581             importCharBuffer(l_pElem->m_pszKey, p_pszNewKey);;
00582         }
00583     }
00584 
00585     //_________________________________________________________________________
00586     //  count
00587     //-------------------------------------------------------------------------
00588     unsigned int count( const char* p_pszKey) const
00589     {
00590         return countElem( p_pszKey);
00591     }
00592 
00593 // flush
00594 
00595     //_________________________________________________________________________
00596     //  FLUSH
00597     //-------------------------------------------------------------------------
00598     
00599     void flush()
00600     {
00601         while(!empty()) deleteElem( removeElem( m_pLastElem));
00602     }
00603 
00604 
00605     //_____________________________________________________________________
00606     //  IMPORTCHARBUFFER
00607     //--------------------------------------------------------------------- 
00608     char* importCharBuffer( char* p_pszOldBuffer,const char* p_pszNewBuffer) const
00609     {
00610         char* l_pszBuffer;
00611 
00612         if( p_pszOldBuffer) 
00613         {
00614             if( strlen( p_pszOldBuffer) >= strlen( p_pszNewBuffer) )
00615             {
00616                 strcpy( p_pszOldBuffer, p_pszNewBuffer);
00617                 l_pszBuffer = p_pszOldBuffer;
00618             }
00619             else
00620             {
00621                 l_pszBuffer = (char*) realloc( p_pszOldBuffer, strlen(p_pszNewBuffer)+1);
00622                 strcpy( l_pszBuffer, p_pszNewBuffer);
00623             }
00624         }
00625         else
00626         {
00627             if(p_pszNewBuffer)
00628             {
00629                 l_pszBuffer = (char*) malloc( strlen(p_pszNewBuffer)+1);
00630                 strcpy( l_pszBuffer, p_pszNewBuffer);
00631             }
00632             else l_pszBuffer = 0;
00633         }
00634 
00635         return l_pszBuffer;
00636     }
00637 
00638 
00639 };
00640 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00641 //=============================================================================
00642 
00643 
00644 
00645 
00646 END_XDFLENGINE_NS
00647 
00648 #endif

Generated on Sat Oct 4 13:20:02 2003 for XDFLengine by doxygen1.3-rc2