Mercurial > hg > audiodb
comparison dbaccumulator.h @ 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 | |
children | e21a3db643af |
comparison
equal
deleted
inserted
replaced
419:f3b5d8aebd17 | 420:580f696c817c |
---|---|
1 template <class T> class DBAccumulator : public Accumulator { | |
2 public: | |
3 DBAccumulator(unsigned int pointNN); | |
4 ~DBAccumulator(); | |
5 void add_point(adb_result_t *r); | |
6 adb_query_results_t *get_points(); | |
7 private: | |
8 unsigned int pointNN; | |
9 std::priority_queue< adb_result_t, std::vector<adb_result_t>, T > *queue; | |
10 std::set< adb_result_t, adb_result_triple_lt > *set; | |
11 }; | |
12 | |
13 template <class T> DBAccumulator<T>::DBAccumulator(unsigned int pointNN) | |
14 : pointNN(pointNN), queue(0), set(0) { | |
15 queue = new std::priority_queue< adb_result_t, std::vector<adb_result_t>, T>; | |
16 set = new std::set<adb_result_t, adb_result_triple_lt>; | |
17 } | |
18 | |
19 template <class T> DBAccumulator<T>::~DBAccumulator() { | |
20 if(queue) { | |
21 delete queue; | |
22 } | |
23 if(set) { | |
24 delete set; | |
25 } | |
26 } | |
27 | |
28 template <class T> void DBAccumulator<T>::add_point(adb_result_t *r) { | |
29 if(!isnan(r->dist)) { | |
30 if(set->find(*r) == set->end()) { | |
31 set->insert(*r); | |
32 queue->push(*r); | |
33 if(queue->size() > pointNN) { | |
34 queue->pop(); | |
35 } | |
36 } | |
37 } | |
38 } | |
39 | |
40 template <class T> adb_query_results_t *DBAccumulator<T>::get_points() { | |
41 unsigned int size = queue->size(); | |
42 adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t)); | |
43 adb_result_t *rs = (adb_result_t *) calloc(size, sizeof(adb_result_t)); | |
44 r->nresults = size; | |
45 r->results = rs; | |
46 | |
47 for(unsigned int k = 0; k < size; k++) { | |
48 rs[k] = queue->top(); | |
49 queue->pop(); | |
50 } | |
51 return r; | |
52 } |