casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenSort.h
Go to the documentation of this file.
1 //# GenSort.h: General sort functions
2 //# Copyright (C) 1993,1994,1995,1996,1997,1999
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_GENSORT_H
29 #define CASA_GENSORT_H
30 
31 #include <casacore/casa/aips.h>
33 
34 namespace casacore { //# NAMESPACE CASACORE - BEGIN
35 
36 //# Forward declarations.
37 template<class T> class Array;
38 template<class T> class Vector;
39 template<class T> class Block;
40 
41 // <summary> General in-place sort functions </summary>
42 // <use visibility=local>
43 // <reviewed reviewer="Friso Olnon" date="1995/03/16" tests="tGenSort" demos="">
44 
45 // <synopsis>
46 //
47 // The static member functions of this templated class are highly optimized
48 // sort functions. They do an in-place sort of an array of values. The
49 // functions are templated, so they can in principle be used with any
50 // data type. However, if used with non-builtin data types, their
51 // class must provide certain functions (see <em>Template Type Argument
52 // Requirements</em>).
53 //
54 // If it is impossible or too expensive to define these functions, the
55 // <linkto class=Sort>Sort</linkto> class can be used instead. This sorts
56 // indirectly using an index array. Instead of the functions mentioned
57 // above it requires a comparison routine.
58 //
59 // The <src>GenSort</src> functions can sort:
60 // <ul>
61 // <li> C-arrays of values;
62 // <li> <src>Array</src>s of values -- the array can have any shape
63 // and the increment can be >1;
64 // <li> <src>Block</src>s of values -- there is a special function to
65 // sort less elements than the size of the <src>Block</src>.
66 // </ul>
67 //
68 // The sort order can be specified in the order field:
69 // <dl>
70 // <dt> <src>Sort::Ascending</src> (default),
71 // <dt> <src>Sort::Descending</src>.
72 // </dl>
73 //
74 // Previously the sort algorithm to use could be given in the options field.
75 // <dl>
76 // <dt> <src>Sort::QuickSort</src> (default)
77 // <dd> is the fastest. It is about 4-6 times faster
78 // than the qsort function on the SUN. No worst case has been
79 // found, even not for cases where qsort is terribly slow.
80 // <dt> <src>Sort::HeapSort</src>
81 // <dd> is about twice as slow as quicksort.
82 // It has the advantage that the worst case is always o(n*log(n)),
83 // while quicksort can have hypothetical inputs with o(n*n).
84 // <dt> <src>Sort::InsSort</src>
85 // <dd> is o(n*n) for random inputs. It is, however, the
86 // only stable sort (i.e. equal values remain in the original order).
87 // </dl>
88 // However, these options are not used anymore because the sort now always
89 // uses a merge sort that is equally fast for random input and much faster for
90 // degenerated cases like an already ordered or reversely ordered array.
91 // Furthermore, merge sort is always stable and will be parallelized if OpenMP
92 // support is enabled giving a 6-fold speedup on 8 cores.
93 // <br><src>Sort::NoDuplicates</src> in the options field indicates that
94 // duplicate values will be removed (only the first occurrance is kept).
95 // <br>The previous sort functionality is still available through the functions
96 // quickSort, heapSort, and insSort.
97 // <p>
98 // All the sort functions return the number of values sorted as their
99 // function value; when duplicate values have been removed, the number of
100 // unique valuess will be returned.
101 // <p>
102 // The class also provides a function to find the k-th largest value
103 // in an array of values. This uses a stripped-down version of quicksort
104 // and is at least 6 times faster than a full quicksort.
105 // </synopsis>
106 
107 // <templating arg=T>
108 // <li> <src>operator=</src> to assign when swapping elements
109 // <li> <src>operator<</src>, <src>operator></src> and
110 // <src>operator==</src> to compare elements
111 // <li> default constructor to allocate a temporary
112 // </templating>
113 
114 template<class T> class GenSort
115 {
116 public:
117 
118  // Sort a C-array containing <src>nr</src> <src>T</src>-type objects.
119  // The sort is done in place and is always stable (thus equal keys keep
120  // their original order). It returns the number of values, which
121  // can be different if a NoDuplicates sort is done.
122  // <br>Insertion sort is used for short arrays (<50 elements). Otherwise,
123  // a merge sort is used which will be parallelized if casacore is built
124  // with OpenMP support.
125  // <group>
126  static uInt sort (T*, uInt nr, Sort::Order = Sort::Ascending,
127  int options = 0);
128 
130  int options = 0);
131 
133  int options = 0);
134  // <group>
135 
136  // Find the k-th largest value.
137  // <br>Note: it does a partial quicksort, thus the data array gets changed.
138  static T kthLargest (T* data, uInt nr, uInt k);
139 
140  // Sort C-array using quicksort.
141  static uInt quickSort (T*, uInt nr, Sort::Order = Sort::Ascending,
142  int options = 0);
143  // Sort C-array using heapsort.
144  static uInt heapSort (T*, uInt nr, Sort::Order = Sort::Ascending,
145  int options = 0);
146  // Sort C-array using insertion sort.
147  static uInt insSort (T*, uInt nr, Sort::Order = Sort::Ascending,
148  int options = 0);
149  // Sort C-array using parallel merge sort (using OpenMP).
150  // By default OpenMP determines the number of threads that can be used.
151  static uInt parSort (T*, uInt nr, Sort::Order = Sort::Ascending,
152  int options = 0, int nthread = 0);
153 
154  // Swap 2 elements in array.
155  static inline void swap (T&, T&);
156 
157  // Reverse the elements in <src>res</src> and put them into <src>data</src>.
158  // Care is taken if both pointers reference the same data.
159  static void reverse (T* data, const T* res, uInt nrrec);
160 
161 private:
162  // The<src>data</src> buffer is divided in <src>nparts</src> parts.
163  // In each part the values are in ascending order.
164  // The index tells the nr of elements in each part.
165  // Recursively each two subsequent parts are merged until only part is left
166  // (giving the sorted array). Alternately <src>data</src> and <src>tmp</src>
167  // are used for the merge result. The pointer containing the final result
168  // is returned.
169  // <br>If possible, merging the parts is done in parallel (using OpenMP).
170  static T* merge (T* data, T* tmp, uInt nrrec, uInt* index,
171  uInt nparts);
172 
173  // Quicksort in ascending order.
174  static void quickSortAsc (T*, Int, Bool multiThread=False, Int rec_lim=128);
175 
176  // Heapsort in ascending order.
177  static void heapSortAsc (T*, Int);
178  // Helper function for ascending heapsort.
179  static void heapAscSiftDown (Int, Int, T*);
180 
181  // Insertion sort in ascending order.
182  static uInt insSortAsc (T*, Int, int option);
183  // Insertion sort in ascending order allowing duplicates.
184  // This is also used by quicksort for its last steps.
185  static uInt insSortAscDup (T*, Int);
186  // Insertion sort in ascending order allowing no duplicates.
187  // This is also used by the other sort algorithms to skip duplicates.
188  static uInt insSortAscNoDup (T*, Int);
189 };
190 
191 
192 // <summary> General indirect sort functions </summary>
193 // <use visibility=local>
194 // <reviewed reviewer="" date="" tests="" demos="">
195 
196 // <synopsis>
197 // This class is similar to <linkto class=GenSort>GenSort</linkto>.
198 // The only difference is that the functions in this class sort
199 // indirectly instead of in-place.
200 // They return the result of the sort as an sorted vector of indices
201 // This is slower, because an extra indirection is involved in each
202 // comparison. However, this sort allows to sort const data.
203 // Another advantage is that this sort is always stable (i.e. equal
204 // values are kept in their original order).
205 
206 template<class T> class GenSortIndirect
207 {
208 public:
209 
210  // Sort a C-array containing <src>nr</src> <src>T</src>-type objects.
211  // The resulting index vector gives the sorted indices.
212  static uInt sort (Vector<uInt>& indexVector, const T* data, uInt nr,
214  int options = Sort::QuickSort);
215 
216  // Sort a C-array containing <src>nr</src> <src>T</src>-type objects.
217  // The resulting index vector gives the sorted indices.
218  static uInt sort (Vector<uInt>& indexVector, const Array<T>& data,
220  int options = Sort::QuickSort);
221 
222  // Sort a C-array containing <src>nr</src> <src>T</src>-type objects.
223  // The resulting index vector gives the sorted indices.
224  static uInt sort (Vector<uInt>& indexVector, const Block<T>& data, uInt nr,
226  int options = Sort::QuickSort);
227 
228  // Find the index of the k-th largest value.
229  static uInt kthLargest (T* data, uInt nr, uInt k);
230 
231  // Sort container using quicksort.
232  // The argument <src>inx</src> gives the index defining the order of the
233  // values in the data array. Its length must be at least <src>nr</src>
234  // and it must be filled with the index values of the data.
235  // Usually this is 0..nr, but it could contain a selection of the data.
236  static uInt quickSort (uInt* inx, const T* data,
237  uInt nr, Sort::Order, int options);
238  // Sort container using heapsort.
239  static uInt heapSort (uInt* inx, const T* data,
240  uInt nr, Sort::Order, int options);
241  // Sort container using insertion sort.
242  static uInt insSort (uInt* inx, const T* data,
243  uInt nr, Sort::Order, int options);
244  // Sort container using parallel merge sort (using OpenMP).
245  // By default the maximum number of threads is used.
246  static uInt parSort (uInt* inx, const T* data,
247  uInt nr, Sort::Order, int options, int nthreads=0);
248 
249 private:
250  // Swap 2 indices.
251  static inline void swapInx (uInt& index1, uInt& index2);
252 
253  // The<src>data</src> buffer is divided in <src>nparts</src> parts.
254  // In each part the values are in ascending order.
255  // The index tells the nr of elements in each part.
256  // Recursively each two subsequent parts are merged until only part is left
257  // (giving the sorted array). Alternately <src>data</src> and <src>tmp</src>
258  // are used for the merge result. The pointer containing the final result
259  // is returned.
260  // <br>If possible, merging the parts is done in parallel (using OpenMP).
261  static uInt* merge (const T* data, uInt* inx, uInt* tmp, uInt nrrec,
262  uInt* index, uInt nparts);
263 
264  // Check if 2 values are in ascending order.
265  // When equal, the order is correct if index1<index2.
266  static inline int isAscending (const T* data, Int index1, Int index2);
267 
268 
269  // Quicksort in ascending order.
270  static void quickSortAsc (uInt* inx, const T*, Int nr,
271  Bool multiThread=False, Int rec_lim=128);
272 
273  // Heapsort in ascending order.
274  static void heapSortAsc (uInt* inx, const T*, Int nr);
275  // Helper function for ascending heapsort.
276  static void heapAscSiftDown (uInt* inx, Int, Int, const T*);
277 
278  // Insertion sort in ascending order.
279  static uInt insSortAsc (uInt* inx, const T*, Int nr, int option);
280  // Insertion sort in ascending order allowing duplicates.
281  // This is also used by quicksort for its last steps.
282  static uInt insSortAscDup (uInt* inx, const T*, Int nr);
283  // Insertion sort in ascending order allowing no duplicates.
284  // This is also used by the other sort algorithms to skip duplicates.
285  static uInt insSortAscNoDup (uInt* inx, const T*, Int nr);
286 };
287 
288 
289 // <summary> Global in-place sort functions </summary>
290 
291 // The following global functions are easier to use than the static
292 // <linkto class=GenSort>GenSort</linkto> member functions.
293 // They do an in-place sort of data, thus the data themselves are moved
294 // ending up in the requested order.
295 // <p>
296 // The default sorting method is QuickSort, which is the fastest.
297 // However, there are pathological cases where it can be slow.
298 // HeapSort is about twice a slow, but its speed is guaranteed.
299 // InsSort (insertion sort) can be very, very slow, but it is the only
300 // stable sort method (i.e. equal values are kept in their original order).
301 // However, <linkto name=genSortIndirect> indirect sorting methods </linkto>
302 // are available to make QuickSort and HeapSort stable.
303 // <p>
304 // All sort methods have an option to skip duplicate values. This is the
305 // only case where the returned number of values can be less than the
306 // original number of values.
307 
308 // <group name=genSortInPlace>
309 
310 template<class T>
311 inline
313  Sort::Order order = Sort::Ascending, int options=0)
314  { return GenSort<T>::sort (data, nr, order, options); }
315 
316 template<class T>
317 inline
319  Sort::Order order = Sort::Ascending, int options=0)
320  { return GenSort<T>::sort (data, order, options); }
321 
322 template<class T>
323 inline
325  Sort::Order order = Sort::Ascending, int options=0)
326  { return GenSort<T>::sort (data, data.nelements(), order, options); }
327 
328 template<class T>
329 inline
331  Sort::Order order = Sort::Ascending, int options=0)
332  { return GenSort<T>::sort (data, nr, order, options); }
333 // </group>
334 
335 
336 // <summary> Global indirect sort functions </summary>
337 
338 // The following global functions easier to use than the static
339 // <linkto class=GenSortIndirect>GenSortIndirect</linkto> member functions.
340 // They do an indirect sort of data, thus the data themselves are not moved.
341 // Rather an index vector is returned giving the sorted data indices.
342 // <p>
343 // The sorting method used is merge sort, which is always stable.
344 // It is the fastest, especially if it can use multiple threads.
345 // <p>
346 // Unlike the <linkto name=genSortInPlace> in-place sorting methods
347 // </linkto>, all indirect sorting methods are stable (i.e. equal
348 // values are left in their original order).
349 // <p>
350 // All sort methods have an option to skip duplicate values. This is the
351 // only case where the returned number of values can be less than the
352 // original number of values.
353 
354 // <group name=genSortIndirect>
355 
356 template<class T>
357 inline
358 uInt genSort (Vector<uInt>& indexVector, const T* data, uInt nr,
359  Sort::Order order = Sort::Ascending, int options=0)
360  { return GenSortIndirect<T>::sort (indexVector, data, nr, order, options); }
361 
362 template<class T>
363 inline
364 uInt genSort (Vector<uInt>& indexVector, const Array<T>& data,
365  Sort::Order order = Sort::Ascending, int options=0)
366  { return GenSortIndirect<T>::sort (indexVector, data, order, options); }
367 
368 template<class T>
369 inline
370 uInt genSort (Vector<uInt>& indexVector, const Block<T>& data,
371  Sort::Order order = Sort::Ascending, int options=0)
372  { return GenSortIndirect<T>::sort (indexVector, data, data.nelements(),
373  order, options); }
374 
375 template<class T>
376 inline
377 uInt genSort (Vector<uInt>& indexVector, const Block<T>& data, uInt nr,
378  Sort::Order order = Sort::Ascending, int options=0)
379  { return GenSortIndirect<T>::sort (indexVector, data, nr, order, options); }
380 // </group>
381 
382 
383 
384 // Implement inline member functions.
385 
386 template<class T>
387 inline void GenSort<T>::swap (T& l, T& r)
388 {
389  T t = l;
390  l = r;
391  r = t;
392 }
393 template<class T>
395 {
396  uInt t = i;
397  i = j;
398  j = t;
399 }
400 template<class T>
401 inline int GenSortIndirect<T>::isAscending (const T* data, Int i, Int j)
402 {
403  return (data[i] > data[j] || (data[i] == data[j] && i > j));
404 }
405 
406 
407 
408 } //# NAMESPACE CASACORE - END
409 
410 #ifndef CASACORE_NO_AUTO_TEMPLATES
411 #include <casacore/casa/Utilities/GenSort.tcc>
412 #endif //# CASACORE_NO_AUTO_TEMPLATES
413 #endif
static uInt insSortAscNoDup(uInt *inx, const T *, Int nr)
Insertion sort in ascending order allowing no duplicates.
int Int
Definition: aipstype.h:50
std::vector< double > Vector
Definition: ds9context.h:24
static uInt quickSort(uInt *inx, const T *data, uInt nr, Sort::Order, int options)
Sort container using quicksort.
static T * merge(T *data, T *tmp, uInt nrrec, uInt *index, uInt nparts)
Thedata buffer is divided in nparts parts.
static uInt sort(Vector< uInt > &indexVector, const T *data, uInt nr, Sort::Order=Sort::Ascending, int options=Sort::QuickSort)
Sort a C-array containing nr T-type objects.
static void heapSortAsc(T *, Int)
Heapsort in ascending order.
Order
Enumerate the sort order:
Definition: Sort.h:260
static uInt * merge(const T *data, uInt *inx, uInt *tmp, uInt nrrec, uInt *index, uInt nparts)
Thedata buffer is divided in nparts parts.
static void heapAscSiftDown(Int, Int, T *)
Helper function for ascending heapsort.
size_t nelements() const
The number of elements contained in this Block&lt;T&gt;.
Definition: Block.h:611
static uInt insSortAscNoDup(T *, Int)
Insertion sort in ascending order allowing no duplicates.
static uInt parSort(T *, uInt nr, Sort::Order=Sort::Ascending, int options=0, int nthread=0)
Sort C-array using parallel merge sort (using OpenMP).
static uInt heapSort(uInt *inx, const T *data, uInt nr, Sort::Order, int options)
Sort container using heapsort.
static void heapAscSiftDown(uInt *inx, Int, Int, const T *)
Helper function for ascending heapsort.
ABSTRACT CLASSES Deliberately vague to be general enough to allow for many different types of data
Definition: PlotData.h:48
Options options
static void reverse(T *data, const T *res, uInt nrrec)
Reverse the elements in res and put them into data.
uInt genSort(T *data, uInt nr, Sort::Order order=Sort::Ascending, int options=0)
Global in-place sort functions The following global functions are easier to use than the static GenSo...
Definition: GenSort.h:312
static uInt insSortAsc(T *, Int, int option)
Insertion sort in ascending order.
static void swapInx(uInt &index1, uInt &index2)
Swap 2 indices.
Definition: GenSort.h:394
General in-place sort functions.
Definition: GenSort.h:114
static int isAscending(const T *data, Int index1, Int index2)
Check if 2 values are in ascending order.
Definition: GenSort.h:401
static uInt parSort(uInt *inx, const T *data, uInt nr, Sort::Order, int options, int nthreads=0)
Sort container using parallel merge sort (using OpenMP).
static uInt heapSort(T *, uInt nr, Sort::Order=Sort::Ascending, int options=0)
Sort C-array using heapsort.
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
static uInt insSort(uInt *inx, const T *data, uInt nr, Sort::Order, int options)
Sort container using insertion sort.
const Bool False
Definition: aipstype.h:44
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
static void swap(T &, T &)
Swap 2 elements in array.
Definition: GenSort.h:387
simple 1-D array
static uInt quickSort(T *, uInt nr, Sort::Order=Sort::Ascending, int options=0)
Sort C-array using quicksort.
static T kthLargest(T *data, uInt nr, uInt k)
Find the k-th largest value.
static void heapSortAsc(uInt *inx, const T *, Int nr)
Heapsort in ascending order.
static uInt insSortAsc(uInt *inx, const T *, Int nr, int option)
Insertion sort in ascending order.
static uInt insSortAscDup(uInt *inx, const T *, Int nr)
Insertion sort in ascending order allowing duplicates.
General indirect sort functions.
Definition: GenSort.h:206
static uInt sort(T *, uInt nr, Sort::Order=Sort::Ascending, int options=0)
Sort a C-array containing nr T-type objects.
static uInt kthLargest(T *data, uInt nr, uInt k)
Find the index of the k-th largest value.
static void quickSortAsc(T *, Int, Bool multiThread=False, Int rec_lim=128)
Quicksort in ascending order.
static uInt insSortAscDup(T *, Int)
Insertion sort in ascending order allowing duplicates.
static uInt insSort(T *, uInt nr, Sort::Order=Sort::Ascending, int options=0)
Sort C-array using insertion sort.
static void quickSortAsc(uInt *inx, const T *, Int nr, Bool multiThread=False, Int rec_lim=128)
Quicksort in ascending order.
unsigned int uInt
Definition: aipstype.h:51
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42