Update mussa to build on ubuntu 10.04 with qt 4.6.2 +boost 1.40.0.1
[mussa.git] / alg / conserved_path.cpp
index a3778daae38edec6b5cc3b21139d56b3b4b80151..1edb374065c2027d3cf1a4e37dec80b17ebea9c6 100644 (file)
@@ -1,30 +1,34 @@
 #include "alg/conserved_path.hpp"
-#include <stdexcept>
+#include "mussa_exceptions.hpp"
 
 using namespace std;
 
 /////////////////////
 
 ConservedPath::ConservedPath()
-  : score(0.0),
+  : window_size(0),
+    score(0.0),
     track_indexes()
 {
 }
 
 ConservedPath::ConservedPath(const ConservedPath::ConservedPath& other)
-  : score(other.score),
+  : window_size(other.window_size),
+    score(other.score),
     track_indexes(other.track_indexes)
 {
 }
 
-ConservedPath::ConservedPath(double s, ConservedPath::path_type path)
-  : score(s),
+ConservedPath::ConservedPath(size_t win_size, double s, ConservedPath::path_type path)
+  : window_size(win_size),
+    score(s),
     track_indexes(path)
 {
 }
 
 void ConservedPath::clear()
 {
+  window_size = 0;
   score = 0.0;
   track_indexes.clear();
 }
@@ -50,9 +54,36 @@ bool operator!=(const ConservedPath::ConservedPath& a,
   return not (a == b);
 }
 
+bool operator<(const ConservedPath& a, const ConservedPath &b)
+{
+  ConservedPath::const_iterator a_i = a.begin();
+  ConservedPath::const_iterator b_i = b.begin();
+  for(; a_i != a.end() and b_i != b.end(); ++a_i, ++b_i)
+  {
+    if (*a_i < *b_i) {
+      return true;
+    } else if (*a_i > *b_i) {
+      return false;
+    }
+    // else they're equal and we continue to loop
+  }
+  // now handle mismatched lengths
+  if (a_i == a.end() and b_i == b.end()) {
+    return false; // since the two paths are equal
+  } else if (a_i == a.end()) {
+    return true; // a is shorter than b
+  } else if (b_i == b.end()) {
+    return false; // b is shorter than a
+  } else {
+    // something went horribly wrong
+    throw std::runtime_error("ConservedPath operator< had a fatal logic error");
+  }
+}
+
 std::ostream& operator<<(std::ostream& out, const ConservedPath& path)
 {
-  out << "<path score=" << path.score 
+  out << "<path win_size" << path.window_size 
+      << " score=" << path.score 
       << " len=" << path.track_indexes.size();
   if (path.track_indexes.size() > 0)
   {
@@ -114,7 +145,7 @@ ConservedPath::const_iterator ConservedPath::end() const
 bool ConservedPath::nextTo(const ConservedPath& next) const
 {
   if (size() != next.size() ) {
-    throw runtime_error("paths must be the same length");
+    throw conserved_path_size_mismatch("paths must be the same length");
   }    
   ConservedPath::const_iterator this_itor = begin();
   ConservedPath::const_iterator next_itor = next.begin();
@@ -150,7 +181,7 @@ std::vector<ConservedPath::path_element> ConservedPath::normalizedIndexes() cons
        ++this_itor)
   {
     if (*this_itor < 0) {
-      paths.push_back(-*this_itor);
+      paths.push_back(-(*this_itor)-window_size);
     } else {
       paths.push_back(*this_itor);
     }
@@ -158,51 +189,7 @@ std::vector<ConservedPath::path_element> ConservedPath::normalizedIndexes() cons
   return paths;
 }
 
-
-/////////////////////
-ExtendedConservedPath::ExtendedConservedPath()
-  : ConservedPath(),
-    window_size(0)
-{
-}
-
-ExtendedConservedPath::ExtendedConservedPath(const ExtendedConservedPath& other)
-  : ConservedPath(other), 
-    window_size(other.window_size)
-{
-}
-
-ExtendedConservedPath::ExtendedConservedPath(int win_size, ConservedPath other)
-  : ConservedPath(other), 
-    window_size(win_size)
-{
-}
-
-ExtendedConservedPath::ExtendedConservedPath(int win_size, 
-                                             double s, 
-                                             ConservedPath::path_type p)
-  : ConservedPath(s, p),
-    window_size(win_size)
-{
-}
-
-std::vector<ConservedPath::path_element> ExtendedConservedPath::normalizedIndexes() const
-{
-  vector<path_element> paths;
-  for (ConservedPath::const_iterator this_itor = begin(); 
-       this_itor != end(); 
-       ++this_itor)
-  {
-    if (*this_itor < 0) {
-      paths.push_back(-(*this_itor) - window_size);
-    } else {
-      paths.push_back((*this_itor));
-    }
-  }
-  return paths;
-}
-
-ExtendedConservedPath& ExtendedConservedPath::extend(int growth)
+ConservedPath& ConservedPath::extend(int growth)
 {
   window_size += growth;
   
@@ -219,10 +206,3 @@ ExtendedConservedPath& ExtendedConservedPath::extend(int growth)
   return *this;
 }
 
-std::ostream& operator<<(std::ostream& out, const ExtendedConservedPath& path)
-{
-  out << "<extended_path size=" << path.window_size;
-  out << (ConservedPath&)path;
-  return out;
-}
-