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

dag_sort.C

Go to the documentation of this file.
00001 // dag_sort.C
00002 //
00003 // 2/11/98 WAH:  Broke away from Scheduler package; minor patching up.
00004 
00005 /* Comparsion routines for sorting */
00006 
00007 /***************** BEGIN *****************/
00008 
00009 #if defined (_STL_SORTING_)
00010 // Dummy method, to test the use of compare functions
00011 static int CompareNodeIds( BigListElement NodeOne, BigListElement NodeTwo )
00012 {
00013    int rc;
00014 
00015    derr( ">> dag::CompareNodeIds\n" );
00016    rc = NodeOne.GetOp()->GetOpId() > NodeTwo.GetOp()->GetOpId();
00017    derr( "   Op 1-" << NodeOne.GetOp()->GetOpId() << ", Op 2-"
00018          << NodeTwo.GetOp()->GetOpId() << "\n" );
00019    derr( "<< dag::CompareNodeIds\n" );
00020    return rc;
00021 }
00022 
00023 static int CompareNodeHeights( BigListElement NodeOne, BigListElement NodeTwo )
00024 { return NodeOne.GetHeight() > NodeTwo.GetHeight(); }
00025 
00026 static int CompareNodeValues( BigListElement NodeOne, BigListElement NodeTwo )
00027 { return NodeOne.Value > NodeTwo.Value; }
00028 
00029 #else
00030 
00031 static int CompareNodeIds( BigListElement *NodeOne, BigListElement *NodeTwo )
00032 {
00033    int rc;
00034 
00035    derr( ">> dag::CompareNodeIds\n" );
00036    rc = NodeOne->GetOp()->GetOpId() > NodeTwo->GetOp()->GetOpId();
00037    derr( "   Op 1-" << NodeOne->GetOp()->GetOpId() << ", Op 2-"
00038          << NodeTwo->GetOp()->GetOpId() << "\n" );
00039    derr( "<< dag::CompareNodeIds\n" );
00040    return rc;
00041 }
00042 
00043 static int CompareNodeHeights( BigListElement *NodeOne,
00044                                BigListElement *NodeTwo )
00045 { return NodeOne->GetHeight() > NodeTwo->GetHeight(); }
00046 
00047 static int CompareNodeValues( BigListElement *NodeOne,
00048                               BigListElement *NodeTwo )
00049 { return NodeOne->Value > NodeTwo->Value; }
00050 
00051 #endif
00052 
00053 
00054 
00055 // SetVectorPtrs
00056 void dag::SetVectorPtrs( vector < SmallListElement > *List )
00057 {
00058    int i, index, size, OpId;
00059 
00060 
00061    derr( ">> SetVectorPtrs\n" );
00062    size = List->size();
00063    for ( i = 0; i < size; i++ ) {
00064 
00065       SmallListElement *small_element = &(*List)[i];
00066 
00067       OpId = small_element->GetOp()->GetOpId();
00068       derr( "   Find Op " << OpId << "..." );
00069 #if defined (_STL_LIST_USED_)
00070       index = FindOpInList( &Vector, OpId );
00071 #else if defined (_STL_VECTOR_USED_)
00072       index = FindOpInList( &MasterList, OpId );
00073 #endif
00074       derr( "found at index " << index << "\n" );
00075 #if defined (_STL_LIST_USED_)
00076       small_element->SetDagEntry( &(Vector[ index ]) );
00077 #else if defined (_STL_VECTOR_USED_)
00078       small_element->SetDagEntry( &(MasterList[ index ]) );
00079 #endif
00080       derr( "   Entry ptr set "
00081             << small_element->GetDagEntry()->GetOp()->GetOpId() << "\n" );
00082    }
00083    derr( "<< SetVectorPtrs\n" );
00084 }
00085 
00086 
00087 // SetSublistPtrs:
00088 void dag::SetSublistPtrs( VectorList *V )
00089 {
00090    VectorList::iterator VectorFront, VectorBack;
00091 
00092 
00093    // Reset the ptrs from the SmallListElement lists into the MainList
00094    derr( ">> SetSubListPtrs\n" );
00095    for ( VectorFront = V->begin(), VectorBack = V->end();
00096          VectorFront != VectorBack; VectorFront++ ) {
00097       derr( "   Op " << (*VectorFront).GetOp()->GetOpId()
00098             << ", Set Predecessor Ptrs\n" );
00099       SetVectorPtrs( (*VectorFront).Predecessors );
00100       derr( "   Set Successor Ptrs\n" );
00101       SetVectorPtrs( (*VectorFront).Successors );
00102    }
00103    derr( "<< SetSubListPtrs\n" );
00104 }
00105 
00106 
00107 // CopyListToVector: Since the current STL version doesn't support passing
00108 //    an arbitrary sort function to a list, the list has to be copied to a
00109 //    vector, which is then sorted
00110 void dag::CopyListToVector( MainList::iterator ListFront,
00111                             MainList::iterator ListBack )
00112 {
00113    VectorList::iterator VectorFront, VectorBack;
00114    BigListElement Element;
00115 
00116 
00117    FunctionName = "CopyListToVector() w/iterators";
00118    derr( ">> CopyListToVector w/iterators\n" );
00119    for ( ; ListFront != ListBack; ListFront++ ) {
00120 
00121       BigListElement *Element1;
00122 
00123       Element1 = new BigListElement( *ListFront );
00124 #if defined (_STL_LIST_USED_)
00125       Vector.push_back( *Element1 );
00126 #endif
00127 #if defined (_DAG_DEBUG_)
00128       Element1->PrintElement();
00129 #endif
00130    }
00131    derr( "<< CopyListToVector w/iterators\n" );
00132 }
00133 
00134 #if 1
00135 // CopyVectorToVector:
00136 void dag::CopyVectorToVector( VectorList *v1, VectorList *v2 )
00137 {
00138    VectorList::iterator VectorFront, VectorBack;
00139 
00140 
00141    FunctionName = "CopyVectorToVector()";
00142    derr( ">> CopyVectorToVector\n" );
00143    v2->reserve( v1->size() );
00144    for ( VectorFront = v1->begin(), VectorBack = v1->end();
00145          VectorFront != VectorBack; VectorFront++ ) {
00146 
00147       BigListElement *Element1;
00148 
00149       Element1 = new BigListElement( *VectorFront );
00150       v2->push_back( *Element1 );
00151    }
00152 
00153    // Reset the ptrs from the SmallListElement lists into the MainList
00154    SetSublistPtrs( v2 );
00155    derr( "<< CopyVectorToVector\n" );
00156 }
00157 #endif
00158 
00159 
00160 #if defined (_STL_LIST_USED_)
00161 // CopyListToVector: Since the current STL version doesn't support passing
00162 //    an arbitrary sort function to a list, the list has to be copied to a
00163 //    vector, which is then sorted
00164 void dag::CopyListToVector()
00165 {
00166    MainList::iterator ListFront, ListBack;
00167    VectorList::iterator VectorFront, VectorBack;
00168    BigListElement Element;
00169 
00170 
00171    FunctionName = "CopyListToVector()()";
00172    derr( ">> CopyListToVector\n" );
00173 
00174    // Assume that new insertions into the list will expand the vector
00175    // by no more than 100%.
00176    Vector.reserve( 2 * MasterList.size() );
00177    for ( ListFront = MasterList.begin(), ListBack = MasterList.end();
00178          ListFront != ListBack; ListFront++ ) {
00179 
00180       BigListElement *Element1;
00181 
00182       Element1 = new BigListElement( *ListFront );
00183       // cerr << "** Copy constructing Op " << (*ListFront).GetOp()->GetOpId()
00184       //      << "\n";
00185       Vector.push_back( *Element1 );
00186    }
00187 
00188    // Reset the ptrs from the SmallListElement lists into the MainList
00189    SetSublistPtrs( &Vector );
00190    derr( ">> About to DestructBigList\n" );
00191    DestructBigList( &MasterList );
00192    derr( "<< Done w/DestructBigList\n" );
00193    VectorActive = 1;
00194    derr( "<< CopyListToVector\n" );
00195 }
00196 #endif
00197 
00198 
00199 // BubbleSort
00200 void dag::BubbleSort( vector < BigListElement >::iterator Begin,
00201                       vector < BigListElement >::iterator End,
00202                       int (*Compare)( BigListElement *, BigListElement * ) )
00203 {
00204    int i;
00205    BigListElement *Element, Element1;
00206    vector < BigListElement >::iterator Front, Back, SubFront, SubBack, Max;
00207 
00208 
00209    derr( ">> BubbleSort\n" );
00210    FunctionName = "BubbleSort() w/iterators";
00211 
00212    // Until all nodes are examined, continue
00213    for ( i = 0, Front = Begin, Back = End; Front != Back; Front++, i++ ) {
00214 
00215       // Set the current list element as the maximum element
00216       Max = Front;
00217 
00218       // Compare all elements from this one down to the end
00219       // Keep an iterator to the largest one found
00220       for ( SubFront = Front; SubFront != Back; SubFront++ ) {
00221          if ( Compare( &(*SubFront), &(*Max) ) == 1 )
00222             Max = SubFront;
00223       }
00224       derr( "   ** New Max: Op " << (*Max).GetOp()->GetOpId() << "\n" );
00225 
00226       // Place the new max element into the n-th place in the list. If
00227       // the max element is the same as before the list was examined,
00228       // there is no need for the copy.
00229       if ( Max != Front ) {
00230          derr( "   Swapping Ops " << (*Max).GetOp()->GetOpId()
00231                << " and " << (*Front).GetOp()->GetOpId() << "\n" );
00232          Element = new BigListElement( *Max );
00233 //       delete Max;
00234          *Max = *Front;
00235          *Front = *Element;
00236       }
00237    }
00238    derr( "<< BubbleSort\n" );
00239 }
00240 
00241 
00242 // BubbleSort
00243 void dag::BubbleSort( int (*Compare)( BigListElement *, BigListElement * ) )
00244 {
00245    int i, size;
00246    BigListElement *Element, Element1;
00247    vector < BigListElement >::iterator Front, Back, SubFront, SubBack, Max;
00248 
00249 
00250    derr( ">> BubbleSort\n" );
00251    FunctionName = "BubbleSort() w/ptrs";
00252 
00253    // Until all nodes are examined, continue
00254 #if defined (_STL_LIST_USED_)
00255    for ( size = Vector.size(), i = 0, Front = Vector.begin(),
00256          Back = Vector.end(); Front != Back; Front++, i++ ) {
00257 #else if defined (_STL_VECTOR_USED_)
00258    for ( size = MasterList.size(), i = 0, Front = MasterList.begin(),
00259          Back = MasterList.end(); Front != Back; Front++, i++ ) {
00260 #endif
00261 
00262       // Set the current list element as the maximum element
00263       Max = Front;
00264 
00265       // Compare all elements from this one down to the end
00266       // Keep an iterator to the largest one found
00267       for ( SubFront = Front; SubFront != Back; SubFront++ ) {
00268          if ( Compare( &(*SubFront), &(*Max) ) == 1 )
00269             Max = SubFront;
00270       }
00271       derr( "   ** New Max: Op " << (*Max).GetOp()->GetOpId() << "\n" );
00272 
00273       // Place the new max element into the n-th place in the list. If
00274       // the max element is the same as before the list was examined,
00275       // there is no need for the copy.
00276       if ( Max != Front ) {
00277          derr( "   Swapping Ops " << (*Max).GetOp()->GetOpId()
00278                << " and " << (*Front).GetOp()->GetOpId() << "\n" );
00279          Element = new BigListElement( *Max );
00280 //       delete Max;
00281 #if defined (_STL_LIST_USED_)
00282          *Max = Vector[ i ];
00283          Vector[ i ] = *Element;
00284 #else if defined (_STL_VECTOR_USED_)
00285          *Max = MasterList[ i ];
00286          MasterList[ i ] = *Element;
00287 #endif
00288       }
00289    }
00290    derr( "<< BubbleSort\n" );
00291 }
00292 
00293 
00294 // SortByOpId: sorts the MasterList by the Op Id of each node
00295 void dag::SortByOpId()
00296 {
00297    vector < BigListElement >::iterator Front, Back;
00298 
00299 
00300    derr( ">> SortByOpId\n" );
00301 #if defined (_STL_LIST_USED_)
00302 #if defined (_STL_SORTING_)
00303    stable_sort( Vector.begin(), Vector.end(), CompareNodeIds );
00304 #else
00305    BubbleSort( Vector.begin(), Vector.end(), CompareNodeIds );
00306 
00307    // Reset the ptrs from the SmallListElement lists into the MainList
00308    SetSublistPtrs( &Vector );
00309 #endif
00310 #else if defined (_STL_VECTOR_USED_)
00311 #if defined (_STL_SORTING_)
00312    sort( MasterList.begin(), MasterList.end(), CompareNodeIds );
00313 #else
00314    BubbleSort( MasterList.begin(), MasterList.end(), CompareNodeIds );
00315 
00316    // Reset the ptrs from the SmallListElement lists into the MainList
00317    SetSublistPtrs( &MasterList );
00318 #endif
00319 #endif
00320 
00321    // Sort the sub-lists
00322 #if defined (_STL_LIST_USED_)
00323    for ( Front = Vector.begin(), Back = Vector.end(); Front != Back;
00324          Front++ ) {
00325 #else if defined (_STL_VECTOR_USED_)
00326    for ( Front = MasterList.begin(), Back = MasterList.end(); Front != Back;
00327          Front++ ) {
00328 #endif
00329       (*Front).SortSuccessorsByOpId();
00330       (*Front).SortPredecessorsByOpId();
00331    }
00332 
00333    derr( "<< SortByOpId\n" );
00334 }
00335 
00336 
00337 // SortByHeight: sorts the MasterList by the height of each node
00338 void dag::SortByHeight()
00339 {
00340    vector < BigListElement >::iterator Front, Back;
00341 
00342 
00343    derr( ">> SortByHeight\n" );
00344 #if defined (_STL_LIST_USED_)
00345 #if defined (_STL_SORTING_)
00346    stable_sort( Vector.begin(), Vector.end(), CompareNodeHeights );
00347 #else
00348    BubbleSort( Vector.begin(), Vector.end(), CompareNodeHeights );
00349 
00350    // Reset the ptrs from the SmallListElement lists into the MainList
00351    SetSublistPtrs( &Vector );
00352 #endif
00353 #else if defined (_STL_VECTOR_USED_)
00354 #if defined (_STL_SORTING_)
00355    sort( MasterList.begin(), MasterList.end(), CompareNodeHeights );
00356 #else
00357    BubbleSort( MasterList.begin(), MasterList.end(), CompareNodeHeights );
00358 
00359    // Reset the ptrs from the SmallListElement lists into the MainList
00360    SetSublistPtrs( &MasterList );
00361 #endif
00362 #endif
00363 
00364    // Sort the sub-lists
00365 #if defined (_STL_LIST_USED_)
00366    for ( Front = Vector.begin(), Back = Vector.end(); Front != Back;
00367          Front++ ) {
00368       (*Front).SortSuccessorsByHeight();
00369       (*Front).SortPredecessorsByHeight();
00370    }
00371 #else if defined (_STL_VECTOR_USED_)
00372    for ( Front = MasterList.begin(), Back = MasterList.end(); Front != Back;
00373          Front++ ) {
00374       (*Front).SortSuccessorsByHeight();
00375       (*Front).SortPredecessorsByHeight();
00376    }
00377 #endif
00378 
00379    derr( "<< SortByHeight\n" );
00380 }
00381 
00382 
00383 // SortByValue: sorts the MasterList by the Value field of each node
00384 void dag::SortByValue()
00385 {
00386    vector < BigListElement >::iterator Front, Back;
00387 
00388 
00389    derr( ">> SortByValue\n" );
00390 #if defined (_STL_LIST_USED_)
00391 #if defined (_STL_SORTING_)
00392    sort( Vector.begin(), Vector.end(), CompareNodeValues );
00393 #else
00394    BubbleSort( Vector.begin(), Vector.end(), CompareNodeValues );
00395 
00396    // Reset the ptrs from the SmallListElement lists into the MainList
00397    SetSublistPtrs( &Vector );
00398 #endif
00399 #else if defined (_STL_VECTOR_USED_)
00400 #if defined (_STL_SORTING_)
00401    sort( MasterList.begin(), MasterList.end(), CompareNodeValues );
00402 #else
00403    BubbleSort( MasterList.begin(), MasterList.end(), CompareNodeValues );
00404 
00405    // Reset the ptrs from the SmallListElement lists into the MainList
00406    SetSublistPtrs( &MasterList );
00407 #endif
00408 #endif
00409 
00410    // Sort the sub-lists
00411 #if defined (_STL_LIST_USED_)
00412    for ( Front = Vector.begin(), Back = Vector.end(); Front != Back;
00413          Front++ ) {
00414 #else if defined (_STL_VECTOR_USED_)
00415    for ( Front = MasterList.begin(), Back = MasterList.end(); Front != Back;
00416          Front++ ) {
00417 #endif
00418       (*Front).SortSuccessorsByValue();
00419       (*Front).SortPredecessorsByValue();
00420    }
00421    derr( "<< SortByValue\n" );
00422 }
00423 
00424 /****************** END ******************/

Generated on Mon Jul 21 20:24:08 2003 for TINKER LEGO DOC by doxygen 1.3.2