TAPs 0.7.7.3
TAPsHEVertexList.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsHEVertexList.hpp
00003 
00004 Class HEVertexList is a doubly linked list for HEVertex (half-edge node).
00005 
00006 SUKITTI PUNAK   (04/05/2005)
00007 UPDATE          (03/25/2010)
00008 ******************************************************************************/
00009 #include "TAPsHEVertexList.hpp"
00010 // Using Inclusion Model (i.e. definitions are included in declarations)
00011 //                       (this name.cpp is included in name.hpp)
00012 // Each friend is defined directly inside its declaration.
00013 
00014 BEGIN_NAMESPACE_TAPs
00015 //=============================================================================
00016 // Constructor(s)
00017 //-----------------------------------------------------------------------------
00018 template <typename T>
00019 HEVertexList<T>::HEVertexList ()
00020     : m_iLength( 0 ),
00021       pHead( NULL ), pTail( NULL ), pCurrent( NULL )
00022 {}
00023 //=============================================================================
00024 // Destructor
00025 //-----------------------------------------------------------------------------
00026 template <typename T>
00027 HEVertexList<T>::~HEVertexList ()
00028 {
00029     HEVertex<T> *tmp;
00030     while ( pHead != NULL ) {
00031         pCurrent = pHead;
00032         tmp = Remove();
00033         if ( tmp != NULL ) delete tmp;
00034     }
00035     m_iLength = 0;
00036 }
00037 //=============================================================================
00038 // Operations
00039 //-----------------------------------------------------------------------------
00040 // Insert after pCurrent
00041 template <typename T>
00042 void HEVertexList<T>::InsertBefore ( HEVertex<T> * const n ) {
00043     // case empty list
00044     if ( pHead == NULL ) {
00045         pCurrent = pHead = pTail = n;
00046         n->Next( NULL );
00047         n->Prev( NULL );
00048         ++m_iLength;
00049     }
00050     // case pCurrent == pHead
00051     else if ( pCurrent == pHead ) {
00052         Prepend( n );
00053     }
00054     // case non-empty list
00055     else {
00056         pCurrent = pCurrent->Prev();
00057         InsertAfter( n );
00058     }
00059 }
00060 //-----------------------------------------------------------------------------
00061 // Insert after pCurrent
00062 template <typename T>
00063 void HEVertexList<T>::InsertAfter ( HEVertex<T> * const n )
00064 {
00065     // case empty list
00066     if ( pHead == NULL ) {
00067         pCurrent = pHead = pTail = n;
00068         n->Next( NULL );
00069         n->Prev( NULL );
00070         ++m_iLength;
00071     }
00072     // case pCurrent == pTail
00073     else if ( pCurrent == pTail ) {
00074         Append( n );
00075         //n->Prev( pCurrent );
00076         //n->Next( NULL );
00077         //pCurrent->Next( n );
00078         //pCurrent = pTail = n;
00079     }
00080     // case pCurrent != pTail
00081     else {
00082         n->Prev( pCurrent );
00083         n->Next( pCurrent->Next() );
00084         n->Next()->Prev( n );
00085         pCurrent->Next( n );
00086         pCurrent = n;
00087         ++m_iLength;
00088     }
00089 }
00090 //-----------------------------------------------------------------------------
00091 // Insert at head
00092 template <typename T>
00093 void HEVertexList<T>::Prepend ( HEVertex<T> * const n )
00094 {
00095     // case empty list
00096     if ( pHead == NULL ) {
00097         pCurrent = pHead = pTail = n;
00098         n->Next( NULL );
00099         n->Prev( NULL );
00100         ++m_iLength;
00101     }
00102     // case non-empty list
00103     else {
00104         n->Prev( NULL );
00105         n->Next( pHead );
00106         pHead->Prev( n );
00107         pHead = n;
00108         ++m_iLength;
00109     }
00110 }
00111 //-----------------------------------------------------------------------------
00112 // Insert at tail
00113 template <typename T>
00114 void HEVertexList<T>::Append ( HEVertex<T> * const n )
00115 {
00116     // case empty list
00117     if ( pHead == NULL ) {
00118         pCurrent = pHead = pTail = n;
00119         n->Next( NULL );
00120         n->Prev( NULL );
00121         ++m_iLength;
00122     }
00123     // case non-empty list
00124     else {
00125         n->Prev( pTail );
00126         n->Next( NULL );
00127         pTail = n;
00128         n->Prev()->Next( n );
00129         ++m_iLength;
00130     }
00131 }
00132 //-----------------------------------------------------------------------------
00133 // Remove at pCurrent
00134 template <typename T>
00135 HEVertex<T> * HEVertexList<T>::Remove ()
00136 {
00137     return Remove( pCurrent );
00138 }
00139 //-----------------------------------------------------------------------------
00140 // Remove pVertex
00141 template <typename T>
00142 HEVertex<T> * HEVertexList<T>::Remove ( HEVertex<T> * pVertex )
00143 {
00144     // case empty list
00145     if ( pVertex == NULL ) return NULL;
00146     //---------------------------------------------------------------
00147     HEVertex<T> *tmp = pVertex;
00148     //-----------------------------------------------------
00149     // case one element list
00150     if ( pHead == pTail && pVertex == pHead ) {
00151         //std::cout << "TAPsHEVertexList.cpp::Remove(): case one element list\n";
00152         pVertex = pHead = pTail = NULL; // make list empty
00153         m_iLength = 0;                  // size is zero
00154         return tmp;
00155     }
00156     //-----------------------------------------------------
00157     // case pVertex == pHead
00158     else if ( pHead == pVertex ) {
00159         //std::cout << "TAPsHEVertexList.cpp::Remove(): case pVertex == pHead\n";
00160         pVertex = pHead = pHead->Next();    // change current and head to next item
00161         pHead->Prev( NULL );                // set head prev point to null
00162     }
00163     //-----------------------------------------------------
00164     // case pVertex == pTail
00165     else if ( pTail == pVertex ) {
00166         //std::cout << "TAPsHEVertexList.cpp::Remove(): case pVertex == pTail\n";
00167         pVertex = pTail = pTail->Prev();    // change current and tail to previous item
00168         pTail->Next( NULL );                // set tail next point to null
00169     }
00170     //-----------------------------------------------------
00171     // case pVertex is between pHead and pTail
00172     else {
00173         //std::cout << "TAPsHEVertexList.cpp::Remove(): case pVertex is between pHead and pTail\n";
00174         pVertex = pVertex->Next();
00175         tmp->Prev()->Next( pVertex );
00176         pVertex->Prev( tmp->Prev() );
00177     }
00178     //-----------------------------------------------------
00179     tmp->Prev( NULL );      // isolate tmp by setting its prev and next to null
00180     tmp->Next( NULL );
00181     --m_iLength;            // reduce the list size by one
00182     return tmp;             // return tmp
00183 }
00184 //-----------------------------------------------------------------------------
00185 // Delete pVertex
00186 template <typename T>
00187 void HEVertexList<T>::Delete ( HEVertex<T> * pVertex )
00188 {
00189     HEVertex<T> * removeItem = Remove( pVertex );
00190     if ( removeItem )   delete removeItem;
00191 }
00192 //-----------------------------------------------------------------------------
00193 //=============================================================================
00194 END_NAMESPACE_TAPs
00195 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00196 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines