Go to the documentation of this file.00001
00002 #ifndef vbl_qsort_h_
00003 #define vbl_qsort_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <vxl_config.h>
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
00043
00044
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
00057
00058
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
00071
00072
00073
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
00086
00087
00088 template <class T>
00089 inline
00090 void vbl_qsort_descending(vcl_vector<T>& v)
00091 {
00092 #if VXL_STDLIB_HAS_QSORT
00093
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
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
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_