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

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

Go to the documentation of this file.
00001 // AList.cpp - Class interface for a Array Based List
00002 
00003 #ifndef __J2K__DataType__AList_CPP__
00004 #define __J2K__DataType__AList_CPP__
00005 
00006 #include <j2k/DataType/Link/AList.hpp>
00007 
00008 #define LIST_SIZE  100
00009 
00010 AList::AList()                            // Default Constructor
00011   : msize( LIST_SIZE ), nbList( 0 ), curr( 0 ) 
00012 {
00013   listarray = new Elem[ LIST_SIZE ];
00014 }
00015 
00016 AList::AList( ULONG sz )                  // Constructor with Size
00017   : msize( sz ), nbList( 0 ), curr( 0 ) 
00018 {
00019   listarray = new Elem[ sz ];
00020 }
00021 
00022 AList::~AList()                           // Destructor: return array space
00023 {
00024   delete [] listarray;
00025 }
00026 
00027 void AList::clear()                       // Remove all Elems from list
00028 {
00029   nbList = 0;                             // Simply reinitialize values
00030   curr = 0;
00031 }
00032 
00033 // Insert Elem at current position
00034 void AList::insert( Elem item ) 
00035 {
00036   // Array must not be full and curr must be a legal position
00037   assert( nbList < msize );
00038   assert( curr <= nbList );
00039 
00040   for( ULONG i = nbList; i > curr; i-- )  // Shift Elems up to make room
00041   {  
00042     listarray[ i ] = listarray[ i-1 ];
00043   }
00044 
00045   listarray[ curr ] = item;
00046   nbList++;                               // Increment current list size
00047 }
00048 
00049 void AList::append( Elem item )           // Insert Elem at tail of list
00050 {
00051   assert( nbList < msize );               // List must not be full
00052   listarray[ nbList ] = item;            
00053   nbList++;                               // Increment list size
00054 }
00055 
00056 Elem AList::remove()                      // Remove and return current Elem
00057 {
00058   assert( !isEmpty()  );                  // Must be an Elem to remove
00059   assert( isInList()  );  
00060 
00061   Elem temp = listarray[curr];            // Store removed Elem
00062 
00063   register ULONG i;
00064   for( i = curr; i < nbList-1; i++)       // Shift elements down
00065   {
00066     listarray[ i ] = listarray[ i + 1 ];
00067   }
00068 
00069   nbList--;                               // Decrement current list size
00070 
00071   return temp;  // This is OK ?!? or by copy constructor?
00072 }
00073                             
00074 inline void AList::setFirst()             // Set curr to first position
00075 {
00076   curr = 0;
00077 }
00078 
00079 inline void AList::prev()                 // Move curr to previous position
00080 {
00081   if ( curr <= 0 ) return;
00082   curr--;
00083 }
00084 
00085 inline void AList::next()                 // Move curr to next position
00086 {
00087   if ( curr >= msize-1 ) return;
00088   curr++;
00089 }
00090 
00091 inline void AList::setPos( ULONG pos )    // Set curr to specified position
00092 {
00093   if ( pos > msize-1 ) return;            // Position must be valid
00094   curr = pos;
00095 }
00096 
00097 inline long AList::getPos()               // Set curr to specified position
00098 {
00099   if ( isEmpty() ) return -1;
00100   return curr;
00101 }
00102 
00103 
00104 inline ULONG AList::length() const        // Return current length of list
00105 {
00106   return nbList;
00107 }
00108 
00109 inline ULONG AList::getMaxSize() const   
00110 {
00111   return msize;
00112 }
00113 
00114 inline void AList::setValue( Elem item )  // Set current Elem's value
00115 {
00116   assert( isInList() );                   // Curr must be at valid position
00117   listarray[ curr ] = item;
00118 }
00119 
00120 inline Elem AList::currValue() const      // Return current Elem's value
00121 {
00122   assert( isInList() );                   // Curr must be at valid position
00123   return listarray[ curr ];               // This is OK ?!?
00124 }
00125 
00126 inline BOOL AList::isEmpty() const        // Return TRUE if List is empty
00127 {
00128   return ( nbList == 0  &&  curr == 0 );
00129 }
00130 
00131 inline BOOL AList::isInList() const       // TRUE if curr is within List
00132 {
00133   return ( nbList != 0  &&  curr >= 0  &&  curr < nbList );
00134 }
00135 
00136 long AList::find( Elem item )             // Find Elem in the list
00137 {
00138   while( isInList() ) 
00139   {
00140     if ( listarray[ curr ] == item ) return getPos();
00141     next();
00142   }
00143 
00144   return -1;
00145 }
00146 
00147 void AList::display() const 
00148 {
00149   register ULONG i = 0;
00150 
00151   printf( "===========================================\n" );
00152 
00153   for( ; i < nbList; i++ ) {
00154     printf( "%d \n ", listarray[i] );
00155   }
00156 
00157   printf( "===========================================\n" );
00158 }
00159 
00160 #endif // End of AList.cpp

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