changeset 16:33d0b18b2509

Allocate Agents separately on the heap and make AgentList contain pointers only (much quicker)
author Chris Cannam
date Wed, 12 Oct 2011 17:01:57 +0100
parents 887c629502a9
children 47e1917c88fc
files Agent.cpp Agent.h AgentList.cpp AgentList.h Induction.cpp
diffstat 5 files changed, 42 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/Agent.cpp	Wed Oct 12 10:55:52 2011 +0100
+++ b/Agent.cpp	Wed Oct 12 17:01:57 2011 +0100
@@ -81,11 +81,13 @@
         std::cerr << "Ag#" << idNumber << ": time " << e.time << ", err " << err << " for beats " << beats << std::endl;
 #endif
 	if ((beats > 0) && (-preMargin <= err) && (err <= postMargin)) {
-	    if (fabs(err) > innerMargin) {	// Create new agent that skips this
+	    if (fabs(err) > innerMargin) {
 #ifdef DEBUG_BEATROOT
                 std::cerr << "Ag#" << idNumber << ": creating another new agent" << std::endl;
 #endif
-		a.add(clone());	//  event (avoids large phase jump)
+                // Create new agent that skips this event (avoids
+                // large phase jump)
+		a.add(clone());
             }
 	    accept(e, err, (int)beats);
 	    return true;
@@ -102,9 +104,11 @@
         EventList::iterator ni = ei;
 	prevBeat = (++ni)->time;
     }
-    for ( ; ei != events.end(); ) {
+    while (ei != events.end()) {
         EventList::iterator ni = ei;
-	nextBeat = (++ni)->time;
+        ++ni;
+        if (ni == events.end()) break;
+	nextBeat = ni->time;
 	beats = nearbyint((nextBeat - prevBeat) / beatInterval - 0.01); //prefer slow
 	currentInterval = (nextBeat - prevBeat) / beats;
 	for ( ; (nextBeat > start) && (beats > 1.5); beats--) {
--- a/Agent.h	Wed Oct 12 10:55:52 2011 +0100
+++ b/Agent.h	Wed Oct 12 17:01:57 2011 +0100
@@ -136,9 +136,9 @@
 	beatTime = -1.0;
     } // constructor
 
-    Agent clone() const {
-        Agent a(*this);
-        a.idNumber = idCounter++;
+    Agent *clone() const {
+        Agent *a = new Agent(*this);
+        a->idNumber = idCounter++;
         return a;
     }
 
--- a/AgentList.cpp	Wed Oct 12 10:55:52 2011 +0100
+++ b/AgentList.cpp	Wed Oct 12 17:01:57 2011 +0100
@@ -23,30 +23,31 @@
 {
     sort();
     for (iterator itr = begin(); itr != end(); ++itr) {
-        if (itr->phaseScore < 0.0) // already flagged for deletion
+        if ((*itr)->phaseScore < 0.0) // already flagged for deletion
             continue;
         iterator itr2 = itr;
         for (++itr2; itr2 != end(); ++itr2) {
-            if (itr2->beatInterval - itr->beatInterval > DEFAULT_BI)
+            if ((*itr2)->beatInterval - (*itr)->beatInterval > DEFAULT_BI)
                 break;
-            if (fabs(itr->beatTime - itr2->beatTime) > DEFAULT_BT)
+            if (fabs((*itr)->beatTime - (*itr2)->beatTime) > DEFAULT_BT)
                 continue;
-            if (itr->phaseScore < itr2->phaseScore) {
-                itr->phaseScore = -1.0;	// flag for deletion
-                if (itr2->topScoreTime < itr->topScoreTime)
-                    itr2->topScoreTime = itr->topScoreTime;
+            if ((*itr)->phaseScore < (*itr2)->phaseScore) {
+                (*itr)->phaseScore = -1.0;	// flag for deletion
+                if ((*itr2)->topScoreTime < (*itr)->topScoreTime)
+                    (*itr2)->topScoreTime = (*itr)->topScoreTime;
                 break;
             } else {
-                itr2->phaseScore = -1.0;	// flag for deletion
-                if (itr->topScoreTime < itr2->topScoreTime)
-                    itr->topScoreTime = itr2->topScoreTime;
+                (*itr2)->phaseScore = -1.0;	// flag for deletion
+                if ((*itr)->topScoreTime < (*itr2)->topScoreTime)
+                    (*itr)->topScoreTime = (*itr2)->topScoreTime;
             }
         }
     }
     int removed = 0;
     for (iterator itr = begin(); itr != end(); ) {
-        if (itr->phaseScore < 0.0) {
+        if ((*itr)->phaseScore < 0.0) {
             ++removed;
+            delete *itr;
             list.erase(itr);
         } else {
             ++itr;
@@ -59,7 +60,7 @@
     }
     int n = 0;
     for (Container::iterator i = list.begin(); i != list.end(); ++i) {
-        std::cerr << "agent " << n++ << ": time " << i->beatTime << std::endl;
+        std::cerr << "agent " << n++ << ": time " << (*i)->beatTime << std::endl;
     }
 #endif
 } // removeDuplicates()
@@ -68,7 +69,7 @@
 void AgentList::beatTrack(EventList el, double stop)
 {
     EventList::iterator ei = el.begin();
-    bool phaseGiven = !empty() && (begin()->beatTime >= 0); // if given for one, assume given for others
+    bool phaseGiven = !empty() && ((*begin())->beatTime >= 0); // if given for one, assume given for others
     while (ei != el.end()) {
         Event ev = *ei;
         ++ei;
@@ -85,22 +86,22 @@
         list.clear();
         for (Container::iterator ai = currentAgents.begin();
              ai != currentAgents.end(); ++ai) {
-            Agent currentAgent = *ai;
-            if (currentAgent.beatInterval != prevBeatInterval) {
+            Agent *currentAgent = *ai;
+            if (currentAgent->beatInterval != prevBeatInterval) {
                 if ((prevBeatInterval>=0) && !created && (ev.time<5.0)) {
 #ifdef DEBUG_BEATROOT
                     std::cerr << "Creating a new agent" << std::endl;
 #endif
                     // Create new agent with different phase
-                    Agent newAgent(prevBeatInterval);
+                    Agent *newAgent = new Agent(prevBeatInterval);
                     // This may add another agent to our list as well
-                    newAgent.considerAsBeat(ev, *this);
+                    newAgent->considerAsBeat(ev, *this);
                     add(newAgent);
                 }
-                prevBeatInterval = currentAgent.beatInterval;
+                prevBeatInterval = currentAgent->beatInterval;
                 created = phaseGiven;
             }
-            if (currentAgent.considerAsBeat(ev, *this))
+            if (currentAgent->considerAsBeat(ev, *this))
                 created = true;
             add(currentAgent);
         } // loop for each agent
@@ -113,12 +114,12 @@
     double best = -1.0;
     Agent *bestAg = 0;
     for (iterator itr = begin(); itr != end(); ++itr) {
-        if (itr->events.empty()) continue;
-        double startTime = itr->events.begin()->time;
-        double conf = (itr->phaseScore + itr->tempoScore) /
-            (useAverageSalience? (double)itr->beatCount: 1.0);
+        if ((*itr)->events.empty()) continue;
+        double startTime = (*itr)->events.begin()->time;
+        double conf = ((*itr)->phaseScore + (*itr)->tempoScore) /
+            (useAverageSalience? (double)(*itr)->beatCount: 1.0);
         if (conf > best) {
-            bestAg = &(*itr);
+            bestAg = *itr;
             best = conf;
         }
     }
--- a/AgentList.h	Wed Oct 12 10:55:52 2011 +0100
+++ b/AgentList.h	Wed Oct 12 17:01:57 2011 +0100
@@ -31,7 +31,7 @@
 class AgentList
 {
 public:
-    typedef std::vector<Agent> Container;
+    typedef std::vector<Agent *> Container;
     typedef Container::iterator iterator;
 
 protected:
@@ -44,10 +44,10 @@
     Container::iterator begin() { return list.begin(); }
     Container::iterator end() { return list.end(); }
     size_t size() { return list.size(); }
-    void push_back(const Agent &a) {
+    void push_back(Agent *a) {
         list.push_back(a);
 #ifdef DEBUG_BEATROOT
-        std::cerr << "  Added Ag#" << a.idNumber << ", have " << list.size() << " agent(s)" << std::endl;
+        std::cerr << "  Added Ag#" << a->idNumber << ", have " << list.size() << " agent(s)" << std::endl;
 #endif
     }
 
@@ -62,7 +62,7 @@
     static const double DEFAULT_BT;
 
     /** Inserts newAgent into the list in ascending order of beatInterval */
-    void add(Agent a) {
+    void add(Agent *a) {
 	add(a, true);
     } // add()/1
 
@@ -71,7 +71,7 @@
      *  @param newAgent The agent to be added to the list
      *  @param sort Flag indicating whether the list is sorted or not
      */
-    void add(Agent newAgent, bool sort){
+    void add(Agent *newAgent, bool sort){
         push_back(newAgent);
 	if (sort) this->sort();
     } // add()/2
--- a/Induction.cpp	Wed Oct 12 10:55:52 2011 +0100
+++ b/Induction.cpp	Wed Oct 12 17:01:57 2011 +0100
@@ -183,7 +183,7 @@
         while (beat > maxIBI)		// Minimum speed
             beat /= 2.0;
         if (beat >= minIBI) {
-            a.push_back(Agent(beat));
+            a.push_back(new Agent(beat));
         }
     }
 #ifdef DEBUG_BEATROOT