core/vbl/vbl_qsort.h
Go to the documentation of this file.
00001 // This is core/vbl/vbl_qsort.h
00002 #ifndef vbl_qsort_h_
00003 #define vbl_qsort_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Collection of common predicates for library sort routines
00010 // \author awf@robots.ox.ac.uk
00011 // \date   15 Mar 00
00012 //
00013 // \verbatim
00014 //  Modifications
00015 //   971119 AWF Initial version
00016 //   PDA (Manchester) 23/03/2001: Tidied up the documentation
00017 //   Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
00018 // \endverbatim
00019 
00020 
00021 #include <vxl_config.h> // VXL_STDLIB_HAS_QSORT
00022 
00023 #include <vcl_algorithm.h>
00024 #if VXL_STDLIB_HAS_QSORT
00025 # include <vcl_cstdlib.h>
00026 #else
00027 # include <vcl_functional.h>
00028 # include <vcl_iostream.h>
00029 #endif
00030 #include <vcl_vector.h>
00031 
00032 #include <vbl/vbl_sort.h>
00033 
00034 #define vbl_qsort_double_ascending  vbl_sort_double_ascending
00035 #define vbl_qsort_double_descending vbl_sort_double_descending
00036 #define vbl_qsort_int_ascending     vbl_sort_int_ascending
00037 #define vbl_qsort_int_descending    vbl_sort_int_descending
00038 #define vbl_qsort_helper            vbl_sort_helper
00039 
00040 typedef int (*vbl_qsort_compare_t)(const void* a, const void* b);
00041 
00042 //: Sort a C array into ascending order.
00043 //  Do this using the standard comparison operations for T,
00044 //  namely operator> and operator==.
00045 template <class T>
00046 inline
00047 void vbl_qsort_ascending(T* base, int n)
00048 {
00049 #if VXL_STDLIB_HAS_QSORT
00050   vcl_qsort(base, n, sizeof base[0], vbl_qsort_helper<T>::ascend);
00051 #else
00052   vcl_sort(base, base+n, vcl_less<T>());
00053 #endif
00054 }
00055 
00056 //: Sort a C array into descending order.
00057 //  Do this using the standard comparison operations for T,
00058 //  namely "operator>" and "operator==".
00059 template <class T>
00060 inline
00061 void vbl_qsort_descending(T* base, int n)
00062 {
00063 #if VXL_STDLIB_HAS_QSORT
00064   vcl_qsort(base, n, sizeof base[0], vbl_qsort_helper<T>::descend);
00065 #else
00066   vcl_sort(base, base+n, vcl_less<T>());
00067 #endif
00068 }
00069 
00070 //: Sort an STL vector into ascending order.
00071 // Do this using the standard comparison operations for T,
00072 // namely operator> and operator==.  I know STL has a sort,
00073 // but this is easier, and faster in the 21st century.
00074 template <class T>
00075 inline
00076 void vbl_qsort_ascending(vcl_vector<T>& v)
00077 {
00078 #if VXL_STDLIB_HAS_QSORT
00079   vcl_qsort(&v[0], v.size(), sizeof v[0], vbl_qsort_helper<T>::ascend);
00080 #else
00081   vcl_sort(v.begin(), v.end(), vcl_less<T>());
00082 #endif
00083 }
00084 
00085 //: Sort an STL vector into descending order.
00086 // Do this using the standard comparison operations for T,
00087 // namely "operator>" and "operator==".
00088 template <class T>
00089 inline
00090 void vbl_qsort_descending(vcl_vector<T>& v)
00091 {
00092 #if VXL_STDLIB_HAS_QSORT
00093   //vector<>::iterator
00094   vcl_qsort(&v[0], v.size(), sizeof v[0], vbl_qsort_helper<T>::descend);
00095 #else
00096   vcl_sort(v.begin(), v.end(), vcl_greater<T>());
00097 #endif
00098 }
00099 
00100 //: Sort STL vector.
00101 template <class T>
00102 inline
00103 void vbl_qsort(vcl_vector<T>& v, int (*compare)(T const& a, T const& b))
00104 {
00105 #if VXL_STDLIB_HAS_QSORT
00106   //vector<>::iterator
00107   vcl_qsort(&v[0], v.size(), sizeof v[0], (vbl_qsort_compare_t)compare);
00108 #else
00109   vcl_cerr << "Sorry, this type of qsort has not been implemented\n";
00110 #endif
00111 }
00112 
00113 #define VBL_QSORT_INSTANTIATE(T) \
00114 VCL_INSTANTIATE_INLINE(void vbl_qsort_ascending(T*,int));\
00115 VCL_INSTANTIATE_INLINE(void vbl_qsort_descending(T*,int))
00116 
00117 #define VBL_QSORT_INSTANTIATE_vector(T) \
00118 VCL_INSTANTIATE_INLINE(void vbl_qsort(vcl_vector<T >& v, \
00119                         int (*compare)(T const& a, T const& b)))
00120 
00121 #endif // vbl_qsort_h_