comparison nearestaccumulator.h @ 498:342822c2d49a

Merge api-inversion branch (-r656:771, but I don't expect to return to that branch) into the trunk. I expect there to be minor performance regressions (e.g. in the SOAP server index cacheing, which I have forcibly removed) and minor unplugged memory leaks (e.g. in audioDB::query(), where I don't free up the datum). I hope that these leaks and performance regressions can be plugged in short order. I also expect that some (but maybe not all) of the issues currently addressed in the memory-leaks branch are superseded or fixed by this merge. There remains much work to be done; go forth and do it.
author mas01cr
date Sat, 10 Jan 2009 16:47:57 +0000
parents
children e21a3db643af
comparison
equal deleted inserted replaced
476:a7193678280b 498:342822c2d49a
1 template <class T> class NearestAccumulator : public Accumulator {
2 public:
3 NearestAccumulator();
4 ~NearestAccumulator();
5 void add_point(adb_result_t *r);
6 adb_query_results_t *get_points();
7 private:
8 std::set< adb_result_t, adb_result_triple_lt > *set;
9 std::set< adb_result_t, adb_result_qpos_lt > *points;
10 };
11
12 template <class T> NearestAccumulator<T>::NearestAccumulator()
13 : set(0), points(0) {
14 set = new std::set< adb_result_t, adb_result_triple_lt >;
15 points = new std::set< adb_result_t, adb_result_qpos_lt >;
16 }
17
18 template <class T> NearestAccumulator<T>::~NearestAccumulator() {
19 if(set) {
20 delete set;
21 }
22 if(points) {
23 delete points;
24 }
25 }
26
27 template <class T> void NearestAccumulator<T>::add_point(adb_result_t *r) {
28 if(!isnan(r->dist)) {
29 if(set->find(*r) == set->end()) {
30 set->insert(*r);
31
32 std::set< adb_result_t, adb_result_qpos_lt >::iterator it;
33 it = points->find(*r);
34 if(it == points->end()) {
35 points->insert(*r);
36 } else if(T()(*(const adb_result_t *)r,(*it))) {
37 points->erase(it);
38 points->insert(*r);
39 }
40 }
41 }
42 }
43
44 template <class T> adb_query_results_t *NearestAccumulator<T>::get_points() {
45 unsigned int nresults = points->size();
46 adb_query_results_t *r = (adb_query_results_t *) malloc(sizeof(adb_query_results_t));
47 adb_result_t *rs = (adb_result_t *) calloc(nresults, sizeof(adb_result_t));
48 r->nresults = nresults;
49 r->results = rs;
50 std::set< adb_result_t, adb_result_qpos_lt >::iterator it;
51 unsigned int k = 0;
52 for(it = points->begin(); it != points->end(); it++) {
53 rs[k++] = *it;
54 }
55 return r;
56 }
57