]> ruin.nu Git - germs.git/commitdiff
Small optimization in sort()
authorMichael Andreen <harv@ruin.nu>
Mon, 29 Oct 2007 13:20:47 +0000 (13:20 +0000)
committerMichael Andreen <harv@ruin.nu>
Mon, 29 Oct 2007 13:20:47 +0000 (13:20 +0000)
safeActions() is guaranteed to only return actions that reduces the inversion
distance by one, so we don't have to compute the inversion distance at
each step.

src/genesorter.cpp

index 81b9da56635b03bf265de647bf349ac91f917079..3198543fae391f4805211406a5f8f76ccf28c889 100644 (file)
@@ -41,7 +41,8 @@ struct ScoreCmp {
 GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go, const Model& m){
        ActionList al;
        GeneOrder temp(go);
-       while(inversionDistance(temp) > 0){
+       size_t dist = inversionDistance(temp);
+       while(dist > 0){
                //cout << "Distance: " << inversionDistance(temp) << " : ";
                //copy(temp.begin(), temp.end(), ostream_iterator<int>(cout, " "));
                //cout << endl;
@@ -54,6 +55,7 @@ GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go, const Model& m){
                        SortAction sa = pq.top().second;
                        sa(temp);
                        al.push_back(sa);
+                       --dist;
                }else
                        return ActionList(); //TODO: Need to handle hurdles.
        }