source: tags/release-1.0.7/src/Detection/sorting.cc @ 1441

Last change on this file since 1441 was 212, checked in by Matthew Whiting, 18 years ago

Improved the setCubeStats function and changed the sorting function over to the STL sort and stable_sort function. Summary:

  • Utils/getStats.cc : changed calls to sort to std::sort
  • Utils/utils.hh : made swap function an inline one.
  • Utils/zscale.cc : changed calls to sort to std::sort
  • ATrous/ReconSearch.cc : minor change from setCubeStats testing
  • Cubes/cubes.cc : Some tweaking to improve performance, and changing calls to sort to std::sort.
  • Detection/sorting.cc : Define a new class "Pair" to sort pairs of arrays in the same sense -- removes need for second sort function in sort.cc. Make use of stable_sort to preserve order of matching elements.
File size: 2.8 KB
Line 
1#include <vector>
2#include <algorithm>
3#include <Detection/detection.hh>
4
5using std::vector;
6
7class Pair
8{
9public:
10  Pair(){};
11  virtual ~Pair(){};
12  friend bool operator< (const Pair& lhs, const Pair& rhs){
13    return (lhs.primary < rhs.primary);
14  };
15  void define(float p, float m){primary=p; matching=m;};
16  float get1(){return primary;};
17  float get2(){return matching;};
18private:
19  float primary;
20  float matching;
21};
22
23void Detection::SortByZ()
24{
25  /**
26   * Detection::SortByZ():
27   *   A Function that takes a Detection and
28   *   sorts the pixels by z-pixel
29   *   Upon return, the inputList is sorted.
30   *   We use stable_sort, so that the order of objects with the same
31   *    z-value is preserved.
32   */
33
34  long size = this->pix.size();
35  Pair *info = new Pair[size];
36
37  for(int i=0;i<size;i++) info[i].define(this->pix[i].getZ(),float(i));
38
39  std::stable_sort(info,info+size);
40 
41  vector <Voxel> sorted(size);
42  for(int i=0;i<size;i++) sorted[i] = this->pix[int(info[i].get2())] ;
43
44  delete [] info;
45
46  for(int i=0;i<size;i++){
47    this->pix.erase(this->pix.begin()+i);
48    this->pix.insert(this->pix.begin()+i, sorted[i]);
49  }
50
51  sorted.clear();
52 
53}
54
55
56void SortByZ(vector <Detection> &inputList)
57{
58  /**
59   * SortByZ(vector <Detection> &):
60   *   A Function that takes a list of Detections and
61   *   sorts them in order of increasing z-pixel value.
62   *   Upon return, the inputList is sorted.
63   */
64
65  long size = inputList.size();
66  Pair *info = new Pair[size];
67 
68  for(int i=0;i<size;i++) info[i].define(inputList[i].getZcentre(), float(i));
69
70  std::stable_sort(info,info+size);
71 
72  vector <Detection> sorted;
73  for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] );
74
75  delete [] info;
76
77  inputList.clear();
78  for(int i=0;i<size;i++) inputList.push_back( sorted[i] );
79  sorted.clear();
80 
81}
82
83void SortByVel(vector <Detection> &inputList)
84{
85  /**
86   * SortByVel(vector <Detection> &):
87   *   A Function that takes a list of Detections and
88   *   sorts them in order of increasing velocity.
89   *   Every member of the vector needs to have WCS defined, (and if so,
90   *     then vel is assumed to be defined for all), otherwise no sorting
91   *     is done.
92   *   Upon return (if all WCS are good), the inputList is sorted.
93   */
94
95  bool isGood = true;
96  for(int i=0;i<inputList.size();i++) isGood = isGood && inputList[i].isWCS();
97
98  if(isGood){
99
100    long size = inputList.size();
101    Pair *info = new Pair[size];
102   
103    for(int i=0;i<size;i++) info[i].define(inputList[i].getVel(), float(i));
104
105    std::stable_sort(info, info+size);
106 
107    vector <Detection> sorted;
108    for(int i=0;i<size;i++) sorted.push_back( inputList[int(info[i].get2())] );
109
110    delete [] info;
111
112    inputList.clear();
113    for(int i=0;i<size;i++) inputList.push_back( sorted[i] );
114    sorted.clear();
115 
116  }
117
118
Note: See TracBrowser for help on using the repository browser.