changeset 420:580f696c817c api-inversion

Split up accumulator.h into multiple files One benefit is the sanity-preserving side-effect of only one class per file; the main reason, though, is so that we can include accumulator.h (the abstract base class) in more than one project file.
author mas01cr
date Wed, 24 Dec 2008 10:54:55 +0000
parents f3b5d8aebd17
children 166312a124bc
files accumulator.h accumulator_test.cpp accumulators.h dbaccumulator.h nearestaccumulator.h pertrackaccumulator.h
diffstat 6 files changed, 233 insertions(+), 229 deletions(-) [+]
line wrap: on
line diff
--- a/accumulator.h	Wed Dec 24 10:54:51 2008 +0000
+++ b/accumulator.h	Wed Dec 24 10:54:55 2008 +0000
@@ -1,234 +1,6 @@
-#include <string>
-#include <set>
-#include <queue>
-#include <map>
-#include <functional>
-#include "audioDB.h"
-extern "C" {
-#include "audioDB_API.h"
-}
-#include "audioDB-internals.h"
-
-#ifndef ACCUMULATOR_H
-#define ACCUMULATOR_H
-
 class Accumulator {
 public:
   virtual ~Accumulator() {};
   virtual void add_point(adb_result_t *r) = 0;
   virtual adb_query_results_t *get_points() = 0;
 };
-
-template <class T> class DBAccumulator : public Accumulator {
-public:
-  DBAccumulator(unsigned int pointNN);
-  ~DBAccumulator();
-  void add_point(adb_result_t *r);
-  adb_query_results_t *get_points();
-private:
-  unsigned int pointNN;
-  std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *queue;
-  std::set< adb_result_t, adb_result_triple_lt > *set;
-};
-
-template <class T> DBAccumulator<T>::DBAccumulator(unsigned int pointNN)
-  : pointNN(pointNN), queue(0), set(0) {
-  queue = new std::priority_queue< adb_result_t, std::vector<adb_result_t>, T>;
-  set = new std::set<adb_result_t, adb_result_triple_lt>;
-}
-
-template <class T> DBAccumulator<T>::~DBAccumulator() {
-  if(queue) {
-    delete queue;
-  }
-  if(set) {
-    delete set;
-  }
-}
-
-template <class T> void DBAccumulator<T>::add_point(adb_result_t *r) {
-  if(!isnan(r->dist)) {
-    if(set->find(*r) == set->end()) {
-      set->insert(*r);
-      queue->push(*r);
-      if(queue->size() > pointNN) {
-        queue->pop();
-      }
-    }
-  }
-}
-
-template <class T> adb_query_results_t *DBAccumulator<T>::get_points() {
-  unsigned int size = queue->size();
-  adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
-  adb_result_t *rs = (adb_result_t *) calloc(size, sizeof(adb_result_t));
-  r->nresults = size;
-  r->results = rs;
-
-  for(unsigned int k = 0; k < size; k++) {
-    rs[k] = queue->top();
-    queue->pop();
-  }
-  return r;
-}
-
-template <class T> class PerTrackAccumulator : public Accumulator {
-public:
-  PerTrackAccumulator(unsigned int pointNN, unsigned int trackNN);
-  ~PerTrackAccumulator();
-  void add_point(adb_result_t *r);
-  adb_query_results_t *get_points();
-private:
-  unsigned int pointNN;
-  unsigned int trackNN;
-  std::map<adb_result_t, std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *, adb_result_key_lt> *queues;
-  std::set< adb_result_t, adb_result_triple_lt > *set;
-};
-
-template <class T> PerTrackAccumulator<T>::PerTrackAccumulator(unsigned int pointNN, unsigned int trackNN)
-  : pointNN(pointNN), trackNN(trackNN), queues(0), set(0) {
-  queues = new std::map<adb_result_t, std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *, adb_result_key_lt>;
-  set = new std::set< adb_result_t, adb_result_triple_lt >;
-}
-
-template <class T> PerTrackAccumulator<T>::~PerTrackAccumulator() {
-  if(queues) {
-    typename std::map< adb_result_t, std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *, adb_result_key_lt>::iterator it;
-    for(it = queues->begin(); it != queues->end(); it++) {
-      delete (*it).second;
-    }
-    delete queues;
-  }
-  if(set) {
-    delete set;
-  }
-}
-
-template <class T> void PerTrackAccumulator<T>::add_point(adb_result_t *r) {
-  if(!isnan(r->dist)) {
-    if(set->find(*r) == set->end()) {
-      set->insert(*r);
-
-      typename std::map< adb_result_t, std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *, adb_result_key_lt>::iterator it;
-      std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *queue;
-      it = queues->find(*r);
-      if(it == queues->end()) {
-        queue = new std::priority_queue< adb_result_t, std::vector< adb_result_t >, T >;
-        (*queues)[*r] = queue;
-      } else {
-        queue = (*it).second;
-      }
-
-      queue->push(*r);
-      if(queue->size() > pointNN) {
-        queue->pop();
-      }
-    }
-  }
-}
-
-template <class T> adb_query_results_t *PerTrackAccumulator<T>::get_points() {
-  typename std::map< adb_result_t, std::vector< adb_result_t >, adb_result_key_lt> points;
-  typename std::priority_queue< adb_result_t, std::vector< adb_result_t >, T> queue;
-  typename std::map< adb_result_t, std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *, adb_result_key_lt>::iterator it;
-
-  unsigned int size = 0;
-  for(it = queues->begin(); it != queues->end(); it++) {
-    unsigned int n = ((*it).second)->size();
-    std::vector<adb_result_t> v;
-    adb_result_t r;
-    double dist = 0;
-    for(unsigned int k = 0; k < n; k++) {
-      r = ((*it).second)->top();
-      dist += r.dist;
-      v.push_back(r);
-      ((*it).second)->pop();
-    }
-    points[r] = v;
-    dist /= n;
-    size += n;
-    r.dist = dist;
-    /* I will burn in hell */
-    r.ipos = n;
-    queue.push(r);
-    if(queue.size() > trackNN) {
-      size -= queue.top().ipos;
-      queue.pop();
-    }
-  }
-
-  adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
-  adb_result_t *rs = (adb_result_t *) calloc(size, sizeof(adb_result_t));
-  r->nresults = size;
-  r->results = rs;
-  
-  unsigned int k = 0;
-  while(queue.size() > 0) {
-    std::vector<adb_result_t> v = points[queue.top()];
-    queue.pop();
-    while(v.size() > 0) {
-      rs[k++] = v.back();
-      v.pop_back();
-    }
-  }
-  return r;
-}
-
-template <class T> class NearestAccumulator : public Accumulator {
-public:
-  NearestAccumulator();
-  ~NearestAccumulator();
-  void add_point(adb_result_t *r);
-  adb_query_results_t *get_points();
-private:
-  std::set< adb_result_t, adb_result_triple_lt > *set;
-  std::set< adb_result_t, adb_result_qpos_lt > *points;
-};
-
-template <class T> NearestAccumulator<T>::NearestAccumulator()
-  : set(0), points(0) {
-  set = new std::set< adb_result_t, adb_result_triple_lt >;
-  points = new std::set< adb_result_t, adb_result_qpos_lt >;
-}
-
-template <class T> NearestAccumulator<T>::~NearestAccumulator() {
-  if(set) {
-    delete set;
-  }
-  if(points) {
-    delete points;
-  }
-}
-
-template <class T> void NearestAccumulator<T>::add_point(adb_result_t *r) {
-  if(!isnan(r->dist)) {
-    if(set->find(*r) == set->end()) {
-      set->insert(*r);
-
-      std::set< adb_result_t, adb_result_qpos_lt >::iterator it;
-      it = points->find(*r);
-      if(it == points->end()) {
-        points->insert(*r);
-      } else if(T()(*(const adb_result_t *)r,(*it))) {
-        points->erase(it);
-        points->insert(*r);
-      }
-    }
-  }
-}
-
-template <class T> adb_query_results_t *NearestAccumulator<T>::get_points() {
-  unsigned int nresults = points->size();
-  adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
-  adb_result_t *rs = (adb_result_t *) calloc(nresults, sizeof(adb_result_t));
-  r->nresults = nresults;
-  r->results = rs;
-  std::set< adb_result_t, adb_result_qpos_lt >::iterator it;
-  unsigned int k = 0;
-  for(it = points->begin(); it != points->end(); it++) {
-    rs[k++] = *it;
-  }
-  return r;
-}
-
-#endif
--- a/accumulator_test.cpp	Wed Dec 24 10:54:51 2008 +0000
+++ b/accumulator_test.cpp	Wed Dec 24 10:54:55 2008 +0000
@@ -1,4 +1,4 @@
-#include "accumulator.h"
+#include "accumulators.h"
 
 static NearestAccumulator<adb_result_dist_lt> *foo = new NearestAccumulator<adb_result_dist_lt>();
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/accumulators.h	Wed Dec 24 10:54:55 2008 +0000
@@ -0,0 +1,21 @@
+#ifndef ACCUMULATORS_H
+#define ACCUMULATORS_H
+
+#include <string>
+#include <set>
+#include <queue>
+#include <map>
+#include <functional>
+
+#include "audioDB.h"
+extern "C" {
+#include "audioDB_API.h"
+}
+#include "audioDB-internals.h"
+
+#include "accumulator.h"
+#include "dbaccumulator.h"
+#include "pertrackaccumulator.h"
+#include "nearestaccumulator.h"
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dbaccumulator.h	Wed Dec 24 10:54:55 2008 +0000
@@ -0,0 +1,52 @@
+template <class T> class DBAccumulator : public Accumulator {
+public:
+  DBAccumulator(unsigned int pointNN);
+  ~DBAccumulator();
+  void add_point(adb_result_t *r);
+  adb_query_results_t *get_points();
+private:
+  unsigned int pointNN;
+  std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *queue;
+  std::set< adb_result_t, adb_result_triple_lt > *set;
+};
+
+template <class T> DBAccumulator<T>::DBAccumulator(unsigned int pointNN)
+  : pointNN(pointNN), queue(0), set(0) {
+  queue = new std::priority_queue< adb_result_t, std::vector<adb_result_t>, T>;
+  set = new std::set<adb_result_t, adb_result_triple_lt>;
+}
+
+template <class T> DBAccumulator<T>::~DBAccumulator() {
+  if(queue) {
+    delete queue;
+  }
+  if(set) {
+    delete set;
+  }
+}
+
+template <class T> void DBAccumulator<T>::add_point(adb_result_t *r) {
+  if(!isnan(r->dist)) {
+    if(set->find(*r) == set->end()) {
+      set->insert(*r);
+      queue->push(*r);
+      if(queue->size() > pointNN) {
+        queue->pop();
+      }
+    }
+  }
+}
+
+template <class T> adb_query_results_t *DBAccumulator<T>::get_points() {
+  unsigned int size = queue->size();
+  adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
+  adb_result_t *rs = (adb_result_t *) calloc(size, sizeof(adb_result_t));
+  r->nresults = size;
+  r->results = rs;
+
+  for(unsigned int k = 0; k < size; k++) {
+    rs[k] = queue->top();
+    queue->pop();
+  }
+  return r;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nearestaccumulator.h	Wed Dec 24 10:54:55 2008 +0000
@@ -0,0 +1,57 @@
+template <class T> class NearestAccumulator : public Accumulator {
+public:
+  NearestAccumulator();
+  ~NearestAccumulator();
+  void add_point(adb_result_t *r);
+  adb_query_results_t *get_points();
+private:
+  std::set< adb_result_t, adb_result_triple_lt > *set;
+  std::set< adb_result_t, adb_result_qpos_lt > *points;
+};
+
+template <class T> NearestAccumulator<T>::NearestAccumulator()
+  : set(0), points(0) {
+  set = new std::set< adb_result_t, adb_result_triple_lt >;
+  points = new std::set< adb_result_t, adb_result_qpos_lt >;
+}
+
+template <class T> NearestAccumulator<T>::~NearestAccumulator() {
+  if(set) {
+    delete set;
+  }
+  if(points) {
+    delete points;
+  }
+}
+
+template <class T> void NearestAccumulator<T>::add_point(adb_result_t *r) {
+  if(!isnan(r->dist)) {
+    if(set->find(*r) == set->end()) {
+      set->insert(*r);
+
+      std::set< adb_result_t, adb_result_qpos_lt >::iterator it;
+      it = points->find(*r);
+      if(it == points->end()) {
+        points->insert(*r);
+      } else if(T()(*(const adb_result_t *)r,(*it))) {
+        points->erase(it);
+        points->insert(*r);
+      }
+    }
+  }
+}
+
+template <class T> adb_query_results_t *NearestAccumulator<T>::get_points() {
+  unsigned int nresults = points->size();
+  adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
+  adb_result_t *rs = (adb_result_t *) calloc(nresults, sizeof(adb_result_t));
+  r->nresults = nresults;
+  r->results = rs;
+  std::set< adb_result_t, adb_result_qpos_lt >::iterator it;
+  unsigned int k = 0;
+  for(it = points->begin(); it != points->end(); it++) {
+    rs[k++] = *it;
+  }
+  return r;
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pertrackaccumulator.h	Wed Dec 24 10:54:55 2008 +0000
@@ -0,0 +1,102 @@
+template <class T> class PerTrackAccumulator : public Accumulator {
+public:
+  PerTrackAccumulator(unsigned int pointNN, unsigned int trackNN);
+  ~PerTrackAccumulator();
+  void add_point(adb_result_t *r);
+  adb_query_results_t *get_points();
+private:
+  unsigned int pointNN;
+  unsigned int trackNN;
+  std::map<adb_result_t, std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *, adb_result_key_lt> *queues;
+  std::set< adb_result_t, adb_result_triple_lt > *set;
+};
+
+template <class T> PerTrackAccumulator<T>::PerTrackAccumulator(unsigned int pointNN, unsigned int trackNN)
+  : pointNN(pointNN), trackNN(trackNN), queues(0), set(0) {
+  queues = new std::map<adb_result_t, std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *, adb_result_key_lt>;
+  set = new std::set< adb_result_t, adb_result_triple_lt >;
+}
+
+template <class T> PerTrackAccumulator<T>::~PerTrackAccumulator() {
+  if(queues) {
+    typename std::map< adb_result_t, std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *, adb_result_key_lt>::iterator it;
+    for(it = queues->begin(); it != queues->end(); it++) {
+      delete (*it).second;
+    }
+    delete queues;
+  }
+  if(set) {
+    delete set;
+  }
+}
+
+template <class T> void PerTrackAccumulator<T>::add_point(adb_result_t *r) {
+  if(!isnan(r->dist)) {
+    if(set->find(*r) == set->end()) {
+      set->insert(*r);
+
+      typename std::map< adb_result_t, std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *, adb_result_key_lt>::iterator it;
+      std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *queue;
+      it = queues->find(*r);
+      if(it == queues->end()) {
+        queue = new std::priority_queue< adb_result_t, std::vector< adb_result_t >, T >;
+        (*queues)[*r] = queue;
+      } else {
+        queue = (*it).second;
+      }
+
+      queue->push(*r);
+      if(queue->size() > pointNN) {
+        queue->pop();
+      }
+    }
+  }
+}
+
+template <class T> adb_query_results_t *PerTrackAccumulator<T>::get_points() {
+  typename std::map< adb_result_t, std::vector< adb_result_t >, adb_result_key_lt> points;
+  typename std::priority_queue< adb_result_t, std::vector< adb_result_t >, T> queue;
+  typename std::map< adb_result_t, std::priority_queue< adb_result_t, std::vector< adb_result_t >, T > *, adb_result_key_lt>::iterator it;
+
+  unsigned int size = 0;
+  for(it = queues->begin(); it != queues->end(); it++) {
+    unsigned int n = ((*it).second)->size();
+    std::vector<adb_result_t> v;
+    adb_result_t r;
+    double dist = 0;
+    for(unsigned int k = 0; k < n; k++) {
+      r = ((*it).second)->top();
+      dist += r.dist;
+      v.push_back(r);
+      ((*it).second)->pop();
+    }
+    points[r] = v;
+    dist /= n;
+    size += n;
+    r.dist = dist;
+    /* I will burn in hell */
+    r.ipos = n;
+    queue.push(r);
+    if(queue.size() > trackNN) {
+      size -= queue.top().ipos;
+      queue.pop();
+    }
+  }
+
+  adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
+  adb_result_t *rs = (adb_result_t *) calloc(size, sizeof(adb_result_t));
+  r->nresults = size;
+  r->results = rs;
+  
+  unsigned int k = 0;
+  while(queue.size() > 0) {
+    std::vector<adb_result_t> v = points[queue.top()];
+    queue.pop();
+    while(v.size() > 0) {
+      rs[k++] = v.back();
+      v.pop_back();
+    }
+  }
+  return r;
+}
+