vbl_qsort.h
Go to the documentation of this file.
1 // This is core/vbl/vbl_qsort.h
2 #ifndef vbl_qsort_h_
3 #define vbl_qsort_h_
4 //:
5 // \file
6 // \brief Collection of common predicates for library sort routines
7 // \author awf@robots.ox.ac.uk
8 // \date 15 Mar 00
9 //
10 // \verbatim
11 // Modifications
12 // 971119 AWF Initial version
13 // PDA (Manchester) 23/03/2001: Tidied up the documentation
14 // Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
15 // \endverbatim
16 
17 
18 #include <algorithm>
19 #include <vector>
20 #include <cstdlib>
21 
22 #ifdef _MSC_VER
23 # include <vcl_msvc_warnings.h>
24 #endif
25 #include <vbl/vbl_sort.h>
26 
27 #define vbl_qsort_double_ascending vbl_sort_double_ascending
28 #define vbl_qsort_double_descending vbl_sort_double_descending
29 #define vbl_qsort_int_ascending vbl_sort_int_ascending
30 #define vbl_qsort_int_descending vbl_sort_int_descending
31 #define vbl_qsort_helper vbl_sort_helper
32 
33 typedef int (*vbl_qsort_compare_t)(const void* a, const void* b);
34 
35 //: Sort a C array into ascending order.
36 // Do this using the standard comparison operations for T,
37 // namely operator> and operator==.
38 template <class T>
39 inline
40 void vbl_qsort_ascending(T* base, int n)
41 {
42  std::qsort(base, n, sizeof base[0], vbl_qsort_helper<T>::ascend);
43 }
44 
45 //: Sort a C array into descending order.
46 // Do this using the standard comparison operations for T,
47 // namely "operator>" and "operator==".
48 template <class T>
49 inline
50 void vbl_qsort_descending(T* base, int n)
51 {
52  std::qsort(base, n, sizeof base[0], vbl_qsort_helper<T>::descend);
53 }
54 
55 //: Sort an STL vector into ascending order.
56 // Do this using the standard comparison operations for T,
57 // namely operator> and operator==. I know STL has a sort,
58 // but this is easier, and faster in the 21st century.
59 template <class T>
60 inline
61 void vbl_qsort_ascending(std::vector<T>& v)
62 {
63  std::qsort(&v[0], v.size(), sizeof v[0], vbl_qsort_helper<T>::ascend);
64 }
65 
66 //: Sort an STL vector into descending order.
67 // Do this using the standard comparison operations for T,
68 // namely "operator>" and "operator==".
69 template <class T>
70 inline
71 void vbl_qsort_descending(std::vector<T>& v)
72 {
73  std::qsort(&v[0], v.size(), sizeof v[0], vbl_qsort_helper<T>::descend);
74 }
75 
76 //: Sort STL vector.
77 template <class T>
78 inline
79 void vbl_qsort(std::vector<T>& v, int (*compare)(T const& a, T const& b))
80 {
81  std::qsort(&v[0], v.size(), sizeof v[0], (vbl_qsort_compare_t)compare);
82 }
83 
84 #define VBL_QSORT_INSTANTIATE(T) \
85 /*template void vbl_qsort_ascending((T)*,int); */\
86 /*template void vbl_qsort_descending((T)*,int) */
87 
88 #define VBL_QSORT_INSTANTIATE_vector(T) \
89 /* template void vbl_qsort(std::vector<T >& v, \
90  int (*compare)(T const& a, T const& b)) */
91 
92 #endif // vbl_qsort_h_
void vbl_qsort_descending(T *base, int n)
Sort a C array into descending order.
Definition: vbl_qsort.h:50
Collection of common predicates for sorting.
int(* vbl_qsort_compare_t)(const void *a, const void *b)
Definition: vbl_qsort.h:33
void vbl_qsort(std::vector< T > &v, int(*compare)(T const &a, T const &b))
Sort STL vector.
Definition: vbl_qsort.h:79
void vbl_qsort_ascending(T *base, int n)
Sort a C array into ascending order.
Definition: vbl_qsort.h:40