Mercurial > hg > beatroot-vamp
comparison AgentList.h @ 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 | 33d0b18b2509 |
children | 633ec097fa56 |
comparison
equal
deleted
inserted
replaced
19:f66ed426a14f | 20:03927f6acee2 |
---|---|
34 typedef std::vector<Agent *> Container; | 34 typedef std::vector<Agent *> Container; |
35 typedef Container::iterator iterator; | 35 typedef Container::iterator iterator; |
36 | 36 |
37 protected: | 37 protected: |
38 Container list; | 38 Container list; |
39 | |
40 static bool agentComparator(const Agent *a, const Agent *b) { | |
41 if (a->beatInterval == b->beatInterval) { | |
42 return a->idNumber < b->idNumber; // ensure stable ordering | |
43 } else { | |
44 return a->beatInterval < b->beatInterval; | |
45 } | |
46 } | |
39 | 47 |
40 public: | 48 public: |
41 // expose some vector methods | 49 // expose some vector methods |
42 //!!! can we remove these again once the rest of AgentList is implemented? | 50 //!!! can we remove these again once the rest of AgentList is implemented? |
43 bool empty() const { return list.empty(); } | 51 bool empty() const { return list.empty(); } |
76 if (sort) this->sort(); | 84 if (sort) this->sort(); |
77 } // add()/2 | 85 } // add()/2 |
78 | 86 |
79 /** Sorts the AgentList by increasing beatInterval. */ | 87 /** Sorts the AgentList by increasing beatInterval. */ |
80 void sort() { | 88 void sort() { |
81 std::sort(list.begin(), list.end()); | 89 #ifdef DEBUG_BEATROOT |
90 std::cerr << "sort: before: "; | |
91 for (iterator i = list.begin(); i != list.end(); ++i) { | |
92 std::cerr << (*i)->idNumber << " "; | |
93 } | |
94 std::cerr << std::endl; | |
95 #endif | |
96 std::sort(list.begin(), list.end(), agentComparator); | |
97 #ifdef DEBUG_BEATROOT | |
98 std::cerr << "sort: after: "; | |
99 for (iterator i = list.begin(); i != list.end(); ++i) { | |
100 std::cerr << (*i)->idNumber << " "; | |
101 } | |
102 std::cerr << std::endl; | |
103 #endif | |
82 } // sort() | 104 } // sort() |
83 | 105 |
84 /** Removes the given item from the list. | 106 /** Removes the given item from the list. |
85 * @param ptr Points to the Agent which is removed from the list | 107 * @param ptr Points to the Agent which is removed from the list |
86 */ | 108 */ |