| 1 |
/** @file qsort-def.h |
| 2 |
** @brief QSort preprocessor metaprogram |
| 3 |
** @author Andrea Vedaldi |
| 4 |
**/ |
| 5 |
|
| 6 |
/* |
| 7 |
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. |
| 8 |
All rights reserved. |
| 9 |
|
| 10 |
This file is part of the VLFeat library and is made available under |
| 11 |
the terms of the BSD license (see the COPYING file). |
| 12 |
*/ |
| 13 |
|
| 14 |
/** @file qsort-def.h |
| 15 |
|
| 16 |
@section qsort-def-overview Overview |
| 17 |
|
| 18 |
@ref qsort-def.h is a metaprogram to define specialized instances |
| 19 |
of the quick-sort algorithm. |
| 20 |
|
| 21 |
@section qsort-def-usage Usage |
| 22 |
|
| 23 |
@ref qsort-def.h is used to define a specialization of the |
| 24 |
::VL_QSORT_sort function that operates |
| 25 |
on a given type of array. For instance the code |
| 26 |
|
| 27 |
@code |
| 28 |
#define VL_QSORT_type float |
| 29 |
#define VL_QSORT_prefix my_qsort |
| 30 |
#include <vl/qsort-def.h> |
| 31 |
@endcode |
| 32 |
|
| 33 |
defines a function @c my_qsort_sort that operates on an array of floats. |
| 34 |
|
| 35 |
@todo large array compatibility. |
| 36 |
**/ |
| 37 |
|
| 38 |
#include "host.h" |
| 39 |
#include <assert.h> |
| 40 |
|
| 41 |
#ifndef VL_QSORT_prefix |
| 42 |
#error "VL_QSORT_prefix must be defined" |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifndef VL_QSORT_array |
| 46 |
#ifndef VL_QSORT_type |
| 47 |
#error "VL_QSORT_type must be defined if VL_QSORT_array is not" |
| 48 |
#endif |
| 49 |
#define VL_QSORT_array VL_QSORT_type* |
| 50 |
#define VL_QSORT_array_const VL_QSORT_type const* |
| 51 |
#endif |
| 52 |
|
| 53 |
#ifdef __DOXYGEN__ |
| 54 |
#define VL_QSORT_prefix QSortPrefix /**< Prefix of the qsort functions */ |
| 55 |
#define VL_QSORT_type QSortType /**< Data type of the qsort elements */ |
| 56 |
#define VL_QSORT_array QSortType* /**< Data type of the qsort container */ |
| 57 |
#endif |
| 58 |
|
| 59 |
/* ---------------------------------------------------------------- */ |
| 60 |
|
| 61 |
#if ! defined(VL_QSORT_cmp) || defined(__DOXYGEN__) |
| 62 |
#define VL_QSORT_cmp VL_XCAT(VL_QSORT_prefix, _cmp) |
| 63 |
|
| 64 |
/** @brief Compare two array elements |
| 65 |
** @param array qsort array. |
| 66 |
** @param indexA index of the first element @c A to compare. |
| 67 |
** @param indexB index of the second element @c B to comapre. |
| 68 |
** @return a negative number if @c A<B, 0 if @c A==B, and |
| 69 |
** a positive number if if @c A>B. |
| 70 |
**/ |
| 71 |
|
| 72 |
VL_INLINE VL_QSORT_type |
| 73 |
VL_QSORT_cmp |
| 74 |
(VL_QSORT_array_const array, |
| 75 |
vl_uindex indexA, |
| 76 |
vl_uindex indexB) |
| 77 |
{ |
| 78 |
return array[indexA] - array[indexB] ; |
| 79 |
} |
| 80 |
|
| 81 |
/* VL_QSORT_cmp */ |
| 82 |
#endif |
| 83 |
|
| 84 |
/* ---------------------------------------------------------------- */ |
| 85 |
|
| 86 |
#if ! defined(VL_QSORT_swap) || defined(__DOXYGEN__) |
| 87 |
#define VL_QSORT_swap VL_XCAT(VL_QSORT_prefix, _swap) |
| 88 |
|
| 89 |
/** @brief Swap two array elements |
| 90 |
** @param array qsort array. |
| 91 |
** @param indexA index of the first element to swap. |
| 92 |
** @param indexB index of the second element to swap. |
| 93 |
** |
| 94 |
** The function swaps the two elements @a a and @ b. The function |
| 95 |
** uses a temporary element of type ::VL_QSORT_type |
| 96 |
** and the copy operator @c =. |
| 97 |
**/ |
| 98 |
|
| 99 |
VL_INLINE void |
| 100 |
VL_QSORT_swap |
| 101 |
(VL_QSORT_array array, |
| 102 |
vl_uindex indexA, |
| 103 |
vl_uindex indexB) |
| 104 |
{ |
| 105 |
VL_QSORT_type t = array [indexA] ; |
| 106 |
array [indexA] = array [indexB] ; |
| 107 |
array [indexB] = t ; |
| 108 |
} |
| 109 |
|
| 110 |
/* VL_QSORT_swap */ |
| 111 |
#endif |
| 112 |
|
| 113 |
/* ---------------------------------------------------------------- */ |
| 114 |
#if ! defined(VL_QSORT_sort_recursive) || defined(__DOXYGEN__) |
| 115 |
#define VL_QSORT_sort_recursive VL_XCAT(VL_QSORT_prefix, _sort_recursive) |
| 116 |
|
| 117 |
/** @brief Sort portion of an array using quicksort |
| 118 |
** @param array (in/out) pointer to the array. |
| 119 |
** @param begin first element of the array portion. |
| 120 |
** @param end last element of the array portion. |
| 121 |
** |
| 122 |
** The function sorts the array using quick-sort. Note that |
| 123 |
** @c begin must be not larger than @c end. |
| 124 |
**/ |
| 125 |
|
| 126 |
VL_INLINE void |
| 127 |
VL_QSORT_sort_recursive |
| 128 |
(VL_QSORT_array array, vl_uindex begin, vl_uindex end) |
| 129 |
{ |
| 130 |
vl_uindex pivot = (end + begin) / 2 ; |
| 131 |
vl_uindex lowPart, i ; |
| 132 |
|
| 133 |
assert (begin <= end) ; |
| 134 |
|
| 135 |
/* swap pivot with last */ |
| 136 |
VL_QSORT_swap (array, pivot, end) ; |
| 137 |
pivot = end ; |
| 138 |
|
| 139 |
/* |
| 140 |
Now scan from left to right, moving all element smaller |
| 141 |
or equal than the pivot to the low part |
| 142 |
array[0], array[1], ..., array[lowPart - 1]. |
| 143 |
*/ |
| 144 |
lowPart = begin ; |
| 145 |
for (i = begin; i < end ; ++i) { /* one less */ |
| 146 |
if (VL_QSORT_cmp (array, i, pivot) <= 0) { |
| 147 |
/* array[i] must be moved into the low part */ |
| 148 |
VL_QSORT_swap (array, lowPart, i) ; |
| 149 |
lowPart ++ ; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/* the pivot should also go into the low part */ |
| 154 |
VL_QSORT_swap (array, lowPart, pivot) ; |
| 155 |
pivot = lowPart ; |
| 156 |
|
| 157 |
/* do recursion */ |
| 158 |
if (pivot > begin) { |
| 159 |
/* note that pivot-1 stays non-negative */ |
| 160 |
VL_QSORT_sort_recursive (array, begin, pivot - 1) ; |
| 161 |
} |
| 162 |
if (pivot < end) { |
| 163 |
VL_QSORT_sort_recursive (array, pivot + 1, end) ; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
/* VL_QSORT_sort_recursive */ |
| 168 |
#endif |
| 169 |
|
| 170 |
/* ---------------------------------------------------------------- */ |
| 171 |
|
| 172 |
#if ! defined(VL_QSORT_sort) || defined(__DOXYGEN__) |
| 173 |
#define VL_QSORT_sort VL_XCAT(VL_QSORT_prefix, _sort) |
| 174 |
|
| 175 |
/** @brief Sort array using quicksort |
| 176 |
** @param array (in/out) pointer to the array. |
| 177 |
** @param size size of the array. |
| 178 |
** |
| 179 |
** The function sorts the array using quick-sort. |
| 180 |
**/ |
| 181 |
|
| 182 |
VL_INLINE void |
| 183 |
VL_QSORT_sort |
| 184 |
(VL_QSORT_array array, vl_size size) |
| 185 |
{ |
| 186 |
assert (size >= 1) ; |
| 187 |
VL_QSORT_sort_recursive (array, 0, size - 1) ; |
| 188 |
} |
| 189 |
|
| 190 |
/* VL_QSORT_qsort */ |
| 191 |
#endif |
| 192 |
|
| 193 |
#undef VL_QSORT_prefix |
| 194 |
#undef VL_QSORT_swap |
| 195 |
#undef VL_QSORT_sort |
| 196 |
#undef VL_QSORT_sort_recursive |
| 197 |
#undef VL_QSORT_type |
| 198 |
#undef VL_QSORT_array |
| 199 |
#undef VL_QSORT_cmp |
| 200 |
|