source: trunk/src/Utils/sort.cc @ 190

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

Large commit. The addition of a new Statistics namespace & class, plus a number of changes to the code to make the cube-wide statistics calculation work. The FDR calculations are incorporated into the new class, and a number of functions have been made into templates to ease the calculations. Details follow:

  • New namespace and class (StatsContainer? -- templated) in Statistics.hh, that holds mean,median,stddev & madfm, and provide accessor and calculator functions for these. It also holds the threshold values for sigma-clipping and FDR methods, as well as the PValue evaluation functions
  • The correctionFactor incorporated into the namespace, and given a conversion function that other functions can call (eg. the atrous_Xd_reconstruct functions).
  • Templated the statistics functions in getStats.cc.
  • Templated the sort functions, and made swap an inline one defined in utils.hh.
  • A number of changes to cubes.cc and cubes.hh:
    • DataArray? gains a StatsContainer? object, to store stats info.
    • Image has lost its pValue array (not needed as we calculate on the fly) and mask array (not used).
    • Image has also lost all its stats members, but keeps minPix.
    • Functions to go are Image::maskObject, Image::findStats. Removed calls to the former. Latter never used.
    • Cube::setCubeStats does the cube-wide stats calculations, including setupFDR (now a Cube function).
    • Cube loses the specMean etc arrays.
  • The Search functions (ReconSearch? and CubicSearch?) changed to accommodate the exchange of StatsContainer? objects. This changed the prototypes as well.
  • The growObject function incorporates the new StatsContainer? object.
  • Minor change to Merger, in the preparation for calling growObject.
  • A new par introduced: flagUserThreshold -- set to true when the user enters a value for the threshold.
  • Removed thresholding_functions.cc and incorporated its functions into cubes.cc and cubes.hh.
File size: 3.5 KB
Line 
1#include <functional>
2#include <Utils/utils.hh>
3// #include <algorithm>
4// #include <iterator>
5// #include <iostream>
6
7template <class T> void sort(T *array, int begin, int end)
8{
9  T pivot = array[begin];
10  int i = begin + 1, j = end, k = end;
11  T t;
12 
13  while (i < j) {
14    if (array[i] < pivot) i++;
15    else if (array[i] > pivot) {
16      j--; k--;
17      t = array[i];
18      array[i] = array[j];
19      array[j] = array[k];
20      array[k] = t; }
21    else {
22      j--;
23      swap(array[i], array[j]);
24    }  }
25  i--;
26  swap(array[begin], array[i]);       
27  if (i - begin > 1)
28    sort(array, begin, i);
29  if (end - k   > 1)
30    sort(array, k, end);                     
31}
32template void sort<int>(int *array, int begin, int end);
33template void sort<long>(long *array, int begin, int end);
34template void sort<float>(float *array, int begin, int end);
35template void sort<double>(double *array, int begin, int end);
36//--------------------------------------------------------------------
37 
38template <class T1,class T2>
39void sort(T1 *array, T2 *matchingArray, int begin, int end)
40{
41  T1 pivot = array[begin];
42  int i = begin + 1, j = end, k = end;
43  T1 t;
44  T2 tt;
45
46  while (i < j) {
47    if (array[i] < pivot) i++;
48    else if (array[i] > pivot) {
49      j--; k--;
50      t = array[i];
51      array[i] = array[j];
52      array[j] = array[k];
53      array[k] = t;
54      tt = matchingArray[i];
55      matchingArray[i] = matchingArray[j];
56      matchingArray[j] = matchingArray[k];
57      matchingArray[k] = tt;
58    }
59    else {
60      j--;
61      swap(array[i], array[j]);
62      swap(matchingArray[i], matchingArray[j]);
63    } 
64  }
65  i--;
66  swap(array[begin], array[i]);       
67  swap(matchingArray[begin], matchingArray[i]);
68  if (i - begin > 1)
69    sort(array, matchingArray, begin, i);
70  if (end - k   > 1)
71    sort(array, matchingArray, k, end);                     
72}
73template void sort<int,int>(int *array, int *arrayB, int begin, int end);
74template void sort<int,float>(int *arrayA, float *arrayB, int begin, int end);
75template void sort<float,int>(float *array, int *arrayB, int begin, int end);
76template void sort<float,float>(float *array, float *arrayB, int begin, int end);
77//--------------------------------------------------------------------
78 
79
80
81
82// template< typename BidirectionalIterator, typename Compare >
83// void quick_sort( BidirectionalIterator first, BidirectionalIterator last, Compare cmp ) {
84//   if( first != last ) {
85//     BidirectionalIterator left  = first;
86//     BidirectionalIterator right = last;
87//     BidirectionalIterator pivot = left++;
88
89//     while( left != right ) {
90//       if( cmp( *left, *pivot ) ) {
91//          ++left;
92//       } else {
93//          while( (left != --right) && cmp( *pivot, *right ) )
94//            ;
95//          std::iter_swap( left, right );
96//       }
97//     }
98
99//     --left;
100//     std::iter_swap( first, left );
101
102//     quick_sort( first, left, cmp );
103//     quick_sort( right, last, cmp );
104//   }
105// }
106
107// template< typename BidirectionalIterator >
108// inline void quick_sort( BidirectionalIterator first, BidirectionalIterator last ) {
109//   quick_sort( first, last,
110//     std::less_equal< typename std::iterator_traits< BidirectionalIterator >::value_type >()
111//   );
112// }
113
114
115
116
117// void sort(float *arr, int beg, int end)
118// {
119//   if (end > beg + 1)
120//   {
121//     float piv = arr[beg];
122//     int l = beg + 1, r = end;
123//     while (l < r)
124//     {
125//       if (arr[l] <= piv)
126//         l++;
127//       else
128//         swap(arr[l], arr[--r]);
129//     }
130//     swap(arr[--l], arr[beg]);
131//     sort(arr, beg, l);
132//     sort(arr, r, end);
133//   }
134// }
135
Note: See TracBrowser for help on using the repository browser.