You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.0 KiB

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