]> ruin.nu Git - germs.git/blobdiff - src/threadgenesorter.cpp
Basic implementation of threaded sorting
[germs.git] / src / threadgenesorter.cpp
index a45988c21dcc4a14f7717a9ef7b07d9757f81179..2276a2fdd271f05a04ae286831fb30ce9d0a20fc 100644 (file)
@@ -21,6 +21,9 @@
 #include "threadgenesorter.h"
 #include "sortaction.h"
 
+#include "genealgorithms.h"
+
+#include <set>
 using namespace std;
 
 /**
@@ -131,6 +134,7 @@ void ThreadGeneSorter::sorter(const GeneOrder& go){
 }
 
 void ThreadGeneSorter::worker(){
+       try{
        while(!_done){
                Mutex m(&_queuelock);
 
@@ -146,6 +150,33 @@ void ThreadGeneSorter::worker(){
                SortUnit su = _queue.top();
                _queue.pop();
                m.unlock();
+
+               size_t dist = inversionDistance(su._go);
+               if (dist == 0){
+                       Mutex m(&_solutionslock);
+                       _solutions.push(pair<double,ActionList>(su._score,su._al));
+                       pthread_cond_broadcast(&_addedSolution);
+                       continue;;
+               }
+
+               ActionList act = safeActions(su._go);
+               if (act.size() > 0){
+                       set<SortAction> safe(act.begin(), act.end());
+                       for (set<SortAction>::iterator sa = safe.begin(); sa != safe.end(); ++sa){
+                               GeneOrder go(su._go);
+                               ActionList al(su._al);
+                               al.push_back(*sa);
+                               (*sa)(go);
+                               double score = su._score + _model.score(*sa,su._go);
+                               Mutex m(&_queuelock);
+                               _queue.push(SortUnit(score,go,al));
+                       }
+                       pthread_cond_broadcast(&_addedTask);
+               } //TODO: Hurdles..
+
+       }
+       }catch(const bad_alloc& e){
+               _done = true;
        }
 }