Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/DataType/Link/SList.cpp

Go to the documentation of this file.
00001 // SList.cpp - Class implementation for a Single-Linked list node with freelist
00002 
00003 #ifndef __J2K__SList_CPP__
00004 #define __J2K__SList_CPP__
00005 
00006 #include <j2k/DataType/Link/SList.hpp>
00007 
00008 /////////////////////////////////////////////////////////////////////////////
00009 /// WARNING:  curr->next  is the OFFICIAL current element !               ///
00010 /// This is to avoid prev(), when adding or removing elements !           ///
00011 /////////////////////////////////////////////////////////////////////////////
00012 
00013 inline SList::SList()                     // Constructor -- Ignore sz
00014  : nbElements( 0 ), pos( 0 )
00015 {
00016   head = tail = curr = new SLink();       // Create header node (NULL, NULL)
00017 }
00018 
00019 SList::~SList()                           // Destructor
00020 {
00021   while( head != NULL )                   // Return links to freelist storage
00022   {
00023     curr = head;
00024     head = head->next;
00025     delete curr;
00026   }
00027 }
00028 
00029 void SList::clear()                       // Remove all Elems from list
00030 {
00031   while( head->next != NULL )             // Return links to free store
00032   {
00033     curr       = head->next;              //   (keep header node)
00034     head->next = curr->next;
00035     delete curr;
00036   }
00037 
00038   curr = tail = head;                     // Reinitialize
00039   pos  = 0;
00040   nbElements = 0;
00041 }
00042 
00043 void SList::setFirst()                    // Set curr to first position
00044 {
00045   curr = head; 
00046   pos  = 0;
00047 }
00048 
00049 // Insert Elem at current position
00050 void SList::insert( Elem item ) 
00051 {
00052   assert( curr != NULL );                     // Must be pointing to list Elem
00053   curr->next = new SLink( item, curr->next );
00054 
00055   if ( tail == curr )                         // Appended new Elem
00056   {
00057     tail = curr->next;
00058   }
00059 
00060   nbElements++;
00061 }
00062 
00063 inline void SList::append( Elem  item )       // Insert Elem at tail
00064 {
00065   curr = tail = tail->next = new SLink( item, NULL );
00066   nbElements++;
00067   pos++;
00068 }
00069 
00070 inline void SList::enqueue( Elem item ) 
00071 {
00072   append( item );
00073 }
00074 
00075 inline Elem  SList::dequeue()                 // Dequeue Elem from head
00076 {
00077   curr = head;
00078   return remove();
00079 }
00080 
00081 Elem  SList::remove()                     // Remove/return Elem
00082 {
00083   assert( isInList() );                   // Must be valid position
00084   Elem   temp  = curr->next->element;     // Remember value
00085   SLink* ltemp = curr->next;              // Remember link node
00086   curr->next   = ltemp->next;             // Remove from list
00087   if (tail == ltemp) tail = curr;         // Removed last: set tail
00088   delete ltemp;                           // Send link to free store
00089 
00090   nbElements--;
00091   return temp;                            // Return value removed
00092 }
00093 
00094 inline void SList::next()                 // Move curr to next position
00095 {
00096   if (curr != NULL) 
00097   {
00098     curr = curr->next;
00099     pos++;
00100   }
00101 }
00102 
00103 // VERY COSTLY ROUTINE ( If you use it alot use DList instead of SList )
00104 void SList::prev()                        // Move curr to previous position
00105 {
00106   SLink* temp = head;
00107   if ((curr == NULL) || (curr == head))   // No previous Elem
00108   {
00109     curr = NULL;
00110     return;                               // So, just return
00111   }
00112 
00113   while( ( temp != NULL ) && ( temp->next != curr ) ) 
00114   {
00115     temp = temp->next;
00116   }
00117 
00118   curr = temp;
00119   pos--;
00120 }
00121 
00122 long SList::size() const 
00123 {
00124   return nbElements;
00125 }
00126 
00127 long SList::getPos() const 
00128 {
00129   return pos;
00130 }
00131 
00132 
00133 ULONG SList::length() const          // Return current length of list
00134 {
00135   register ULONG cnt = 0;
00136   SLink* temp = head->next;
00137 
00138   for ( ; temp != NULL; temp = temp->next )
00139   {
00140      cnt++;                        // Count the number of Elems
00141   }
00142 
00143   return cnt;
00144 }
00145 
00146 void SList::setPos( ULONG p )     // Set curr to specified position
00147 {
00148   curr = head;
00149   register ULONG i = 0;
00150   pos = 0;
00151 
00152   for( ; ( curr != NULL ) && ( i < p ); i++, pos++ )
00153   {
00154     curr = curr->next;
00155   }
00156 }
00157 
00158 void SList::setValue( Elem val )    // Set current Elem's value
00159 {
00160   assert( isInList() ); 
00161   curr->next->element = val; 
00162 }
00163 
00164 Elem SList::currValue() const       // Return value of current Elem
00165 {
00166   assert( isInList() ); 
00167   return curr->next->element; 
00168 }
00169 
00170 Elem SList::getValue() const        // Return value of current Elem
00171 {
00172   assert( isInList() ); 
00173   return curr->next->element; 
00174 }
00175 
00176 inline BOOL SList::isInList() const   // TRUE if curr is within list
00177 {
00178   return ( (curr != NULL)  &&  (curr->next != NULL) );
00179 }
00180 
00181 // This is ok?
00182 long SList::find( Elem  item )            // Find value
00183 {
00184   assert( !isEmpty() ); 
00185 
00186   SLink* temp = curr;
00187   ULONG  counter = 0;
00188 
00189   if ( curr == head  ||  curr == NULL ) 
00190   {
00191     curr = head->next;
00192     pos = 0;
00193   }
00194 
00195   for ( ; isInList(); curr = curr->next, pos++ ) 
00196   {
00197     if ( curr->element == item ) 
00198     {
00199       return pos;
00200     }
00201   }
00202 
00203   curr = temp;
00204   return -1;                              // Not found
00205 }
00206 
00207 inline BOOL SList::isEmpty() const        // Return TRUE if list is empty
00208 {
00209   return ( head->next == NULL || nbElements < 1 );
00210 }
00211 
00212 
00213 void SList::display() 
00214 {
00215   if ( curr == head )  return;   // Empty List
00216   
00217   SLink* temp = curr;
00218   curr = head->next;
00219 
00220   printf( "===========================================\n" );
00221 
00222   while ( curr != NULL )             // Stop if we reach the end
00223   {
00224     printf( "%d \t", curr->element );
00225 
00226     if ( curr->next == NULL ) 
00227     {
00228       printf( "NULL \n" );
00229     } 
00230     else 
00231     {
00232       printf( "%d \n", curr->next->element );
00233     }
00234   
00235     curr = curr->next;
00236   }
00237 
00238   printf( "===========================================\n" );
00239 
00240   curr = temp;
00241 }
00242 
00243 #endif // End of SList.cpp

Generated on Sun Oct 14 18:46:18 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001