add operator< to ConservedPath
[mussa.git] / alg / conserved_path.cpp
index 52e0c39b5a3fc8d5c686a651ddd278e0bd5af173..1edb374065c2027d3cf1a4e37dec80b17ebea9c6 100644 (file)
@@ -54,6 +54,32 @@ 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 win_size" << path.window_size