changeset 20:03927f6acee2

Use a proper comparator for AgentList. The naive operator< wasn't being used because AgentList stores pointers, not values, so it was comparing by address. This was a pretty serious bug
author Chris Cannam
date Wed, 28 Aug 2013 16:50:05 +0100
parents f66ed426a14f
children d98bc465a116
files AgentList.h
diffstat 1 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/AgentList.h	Mon Oct 17 13:08:19 2011 +0100
+++ b/AgentList.h	Wed Aug 28 16:50:05 2013 +0100
@@ -37,6 +37,14 @@
 protected:
     Container list;
 
+    static bool agentComparator(const Agent *a, const Agent *b) {
+        if (a->beatInterval == b->beatInterval) {
+            return a->idNumber < b->idNumber; // ensure stable ordering
+        } else {
+            return a->beatInterval < b->beatInterval;
+        }
+    }
+
 public:
     // expose some vector methods
     //!!! can we remove these again once the rest of AgentList is implemented?
@@ -78,7 +86,21 @@
 
     /** Sorts the AgentList by increasing beatInterval. */
     void sort() {
-	std::sort(list.begin(), list.end());
+#ifdef DEBUG_BEATROOT
+        std::cerr << "sort: before: ";
+        for (iterator i = list.begin(); i != list.end(); ++i) {
+            std::cerr << (*i)->idNumber << " ";
+        }
+        std::cerr << std::endl;
+#endif
+	std::sort(list.begin(), list.end(), agentComparator);
+#ifdef DEBUG_BEATROOT
+        std::cerr << "sort: after: ";
+        for (iterator i = list.begin(); i != list.end(); ++i) {
+            std::cerr << (*i)->idNumber << " ";
+        }
+        std::cerr << std::endl;
+#endif
     } // sort()
 
     /** Removes the given item from the list.