changeset 400:78dd9b35559b

* transform operator<
author Chris Cannam
date Thu, 03 Apr 2008 08:30:25 +0000
parents 0f0f08c22552
children d030801113b6
files transform/Transform.cpp transform/Transform.h
diffstat 2 files changed, 61 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/transform/Transform.cpp	Fri Mar 28 17:51:13 2008 +0000
+++ b/transform/Transform.cpp	Thu Apr 03 08:30:25 2008 +0000
@@ -107,7 +107,7 @@
 }
 
 bool
-Transform::operator==(const Transform &t)
+Transform::operator==(const Transform &t) const
 {
     return 
         m_id == t.m_id &&
@@ -122,6 +122,42 @@
         m_sampleRate == t.m_sampleRate;
 }
 
+bool
+Transform::operator<(const Transform &t) const
+{
+    if (m_id != t.m_id) {
+        return m_id < t.m_id;
+    }
+    if (m_parameters != t.m_parameters) {
+        return mapLessThan<QString, float>(m_parameters, t.m_parameters);
+    }
+    if (m_configuration != t.m_configuration) {
+        return mapLessThan<QString, QString>(m_configuration, t.m_configuration);
+    }
+    if (m_program != t.m_program) {
+        return m_program < t.m_program;
+    }
+    if (m_stepSize != t.m_stepSize) {
+        return m_stepSize < t.m_stepSize;
+    }
+    if (m_blockSize != t.m_blockSize) {
+        return m_blockSize < t.m_blockSize;
+    }
+    if (m_windowType != t.m_windowType) {
+        return m_windowType < t.m_windowType;
+    }
+    if (m_startTime != t.m_startTime) {
+        return m_startTime < t.m_startTime;
+    }
+    if (m_duration != t.m_duration) {
+        return m_duration < t.m_duration;
+    }
+    if (m_sampleRate != t.m_sampleRate) {
+        return m_sampleRate < t.m_sampleRate;
+    }
+    return false;
+}
+
 void
 Transform::setIdentifier(TransformId id)
 {
--- a/transform/Transform.h	Fri Mar 28 17:51:13 2008 +0000
+++ b/transform/Transform.h	Thu Apr 03 08:30:25 2008 +0000
@@ -22,6 +22,8 @@
 
 #include <QString>
 
+#include <map>
+
 typedef QString TransformId;
 
 class QXmlAttributes;
@@ -55,7 +57,13 @@
      * Compare two Transforms.  They only compare equal if every data
      * element matches.
      */
-    bool operator==(const Transform &);
+    bool operator==(const Transform &) const;
+    
+    /**
+     * Order two Transforms, so that they can be used as keys in
+     * containers.
+     */
+    bool operator<(const Transform &) const;
 
     void setIdentifier(TransformId id);
     TransformId getIdentifier() const;
@@ -139,6 +147,21 @@
     (QString identifier,
      QString &type, QString &soName, QString &label, QString &output);
 
+    template <typename A, typename B>
+    bool mapLessThan(const std::map<A, B> &a, const std::map<A, B> &b) const {
+        // Return true if a is "less than" b.  Ordering doesn't have
+        // to be meaningful, just consistent.
+        typename std::map<A, B>::const_iterator i;
+        typename std::map<A, B>::const_iterator j;
+        for (i = a.begin(), j = b.begin(); i != a.end(); ++i) {
+            if (j == b.end()) return false; // a is longer than b
+            if (i->first != j->first) return i->first < j->first;
+            if (i->second != j->second) return i->second < j->second;
+        }
+        if (j != b.end()) return true; // a is shorter than b
+        return false; // equal
+    }
+
     ParameterMap m_parameters;
     ConfigurationMap m_configuration;
     QString m_pluginVersion;