qsort-def.h
Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00008 All rights reserved.
00009 
00010 This file is part of the VLFeat library and is made available under
00011 the terms of the BSD license (see the COPYING file).
00012 */
00013 
00038 #include "host.h"
00039 #include <assert.h>
00040 
00041 #ifndef VL_QSORT_prefix
00042 #error "VL_QSORT_prefix must be defined"
00043 #endif
00044 
00045 #ifndef VL_QSORT_array
00046 #ifndef VL_QSORT_type
00047 #error "VL_QSORT_type must be defined if VL_QSORT_array is not"
00048 #endif
00049 #define VL_QSORT_array VL_QSORT_type*
00050 #define VL_QSORT_array_const VL_QSORT_type const*
00051 #endif
00052 
00053 #ifdef __DOXYGEN__
00054 #define VL_QSORT_prefix  QSortPrefix       
00055 #define VL_QSORT_type    QSortType         
00056 #define VL_QSORT_array   QSortType*        
00057 #endif
00058 
00059 /* ---------------------------------------------------------------- */
00060 
00061 #if ! defined(VL_QSORT_cmp) || defined(__DOXYGEN__)
00062 #define VL_QSORT_cmp VL_XCAT(VL_QSORT_prefix, _cmp)
00063 
00072 VL_INLINE VL_QSORT_type
00073 VL_QSORT_cmp
00074 (VL_QSORT_array_const array,
00075  vl_uindex indexA,
00076  vl_uindex indexB)
00077 {
00078   return array[indexA] - array[indexB] ;
00079 }
00080 
00081 /* VL_QSORT_cmp */
00082 #endif
00083 
00084 /* ---------------------------------------------------------------- */
00085 
00086 #if ! defined(VL_QSORT_swap) || defined(__DOXYGEN__)
00087 #define VL_QSORT_swap VL_XCAT(VL_QSORT_prefix, _swap)
00088 
00099 VL_INLINE void
00100 VL_QSORT_swap
00101 (VL_QSORT_array array,
00102  vl_uindex indexA,
00103  vl_uindex indexB)
00104 {
00105   VL_QSORT_type t = array [indexA] ;
00106   array [indexA] = array [indexB] ;
00107   array [indexB] = t ;
00108 }
00109 
00110 /* VL_QSORT_swap */
00111 #endif
00112 
00113 /* ---------------------------------------------------------------- */
00114 #if ! defined(VL_QSORT_sort_recursive) || defined(__DOXYGEN__)
00115 #define VL_QSORT_sort_recursive VL_XCAT(VL_QSORT_prefix, _sort_recursive)
00116 
00126 VL_INLINE void
00127 VL_QSORT_sort_recursive
00128 (VL_QSORT_array array, vl_uindex begin, vl_uindex end)
00129 {
00130   vl_uindex pivot = (end + begin) / 2 ;
00131   vl_uindex lowPart, i ;
00132 
00133   assert (begin <= end) ;
00134 
00135   /* swap pivot with last */
00136   VL_QSORT_swap (array, pivot, end) ;
00137   pivot = end ;
00138 
00139   /*
00140    Now scan from left to right, moving all element smaller
00141    or equal than the pivot to the low part
00142    array[0], array[1], ..., array[lowPart - 1].
00143    */
00144   lowPart = begin ;
00145   for (i = begin; i < end ; ++i) { /* one less */
00146     if (VL_QSORT_cmp (array, i, pivot) <= 0) {
00147       /* array[i] must be moved into the low part */
00148       VL_QSORT_swap (array, lowPart, i) ;
00149       lowPart ++ ;
00150     }
00151   }
00152 
00153   /* the pivot should also go into the low part */
00154   VL_QSORT_swap (array, lowPart, pivot) ;
00155   pivot = lowPart ;
00156 
00157   /* do recursion */
00158   if (pivot > begin) {
00159     /* note that pivot-1 stays non-negative */
00160     VL_QSORT_sort_recursive (array, begin, pivot - 1) ;
00161   }
00162   if (pivot < end) {
00163     VL_QSORT_sort_recursive (array, pivot + 1, end) ;
00164   }
00165 }
00166 
00167 /* VL_QSORT_sort_recursive */
00168 #endif
00169 
00170 /* ---------------------------------------------------------------- */
00171 
00172 #if ! defined(VL_QSORT_sort) || defined(__DOXYGEN__)
00173 #define VL_QSORT_sort VL_XCAT(VL_QSORT_prefix, _sort)
00174 
00182 VL_INLINE void
00183 VL_QSORT_sort
00184 (VL_QSORT_array array, vl_size size)
00185 {
00186   assert (size >= 1) ;
00187   VL_QSORT_sort_recursive (array, 0, size - 1) ;
00188 }
00189 
00190 /* VL_QSORT_qsort */
00191 #endif
00192 
00193 #undef VL_QSORT_prefix
00194 #undef VL_QSORT_swap
00195 #undef VL_QSORT_sort
00196 #undef VL_QSORT_sort_recursive
00197 #undef VL_QSORT_type
00198 #undef VL_QSORT_array
00199 #undef VL_QSORT_cmp
00200 


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51