Implemented sortByStateId which is called for table-based code generators. The
authorthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Tue, 24 Apr 2007 17:13:42 +0000 (17:13 +0000)
committerthurston <thurston@052ea7fc-9027-0410-9066-f65837a77df0>
Tue, 24 Apr 2007 17:13:42 +0000 (17:13 +0000)
frontend will normally sort, however it may be possible that the intermediate
file gets edited, in which case a sort may be necessary.

git-svn-id: http://svn.complang.org/ragel/trunk@192 052ea7fc-9027-0410-9066-f65837a77df0

redfsm/redfsm.cpp

index 6a55b22..51a2e58 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "redfsm.h"
 #include "avlmap.h"
+#include "mergesort.h"
 #include <iostream>
 #include <sstream>
 
@@ -166,9 +167,35 @@ void RedFsmAp::sortStateIdsByFinal()
        }
 }
 
+struct CmpStateById
+{
+       static int compare( RedStateAp *st1, RedStateAp *st2 )
+       {
+               if ( st1->id < st2->id )
+                       return -1;
+               else if ( st1->id > st2->id )
+                       return 1;
+               else
+                       return 0;
+       }
+};
+
 void RedFsmAp::sortByStateId()
 {
-       /* FIXME: Implement. */
+       /* Make the array. */
+       int pos = 0;
+       RedStateAp **ptrList = new RedStateAp*[stateList.length()];
+       for ( RedStateList::Iter st = stateList; st.lte(); st++, pos++ )
+               ptrList[pos] = st;
+       
+       MergeSort<RedStateAp*, CmpStateById> mergeSort;
+       mergeSort.sort( ptrList, stateList.length() );
+
+       stateList.abandon();
+       for ( int st = 0; st < pos; st++ )
+               stateList.append( ptrList[st] );
+
+       delete[] ptrList;
 }
 
 /* Find the final state with the lowest id. */