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.

101 lines
2.5 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file shuffle-def.h
  2. ** @brief Shuffle 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 shuffle-def.h
  12. @todo large array compatibility.
  13. **/
  14. #include "host.h"
  15. #include "random.h"
  16. #include <assert.h>
  17. #ifndef VL_SHUFFLE_prefix
  18. #error "VL_SHUFFLE_prefix must be defined"
  19. #endif
  20. #ifndef VL_SHUFFLE_array
  21. #ifndef VL_SHUFFLE_type
  22. #error "VL_SHUFFLE_type must be defined if VL_SHUFFLE_array is not"
  23. #endif
  24. #define VL_SHUFFLE_array VL_SHUFFLE_type*
  25. #endif
  26. #ifdef __DOXYGEN__
  27. #define VL_SHUFFLE_prefix ShufflePrefix /**< Prefix of the shuffle functions */
  28. #define VL_SHUFFLE_type ShuffleType /**< Data type of the shuffle elements */
  29. #define VL_SHUFFLE_array ShuffleType* /**< Data type of the shuffle container */
  30. #endif
  31. /* ---------------------------------------------------------------- */
  32. #if ! defined(VL_SHUFFLE_swap) || defined(__DOXYGEN__)
  33. #define VL_SHUFFLE_swap VL_XCAT(VL_SHUFFLE_prefix, _swap)
  34. /** @brief Swap two array elements
  35. ** @param array shuffle array.
  36. ** @param indexA index of the first element to swap.
  37. ** @param indexB index of the second element to swap.
  38. **
  39. ** The function swaps the two elements @a a and @ b. The function
  40. ** uses a temporary element of type ::VL_SHUFFLE_type
  41. ** and the copy operator @c =.
  42. **/
  43. VL_INLINE void
  44. VL_SHUFFLE_swap
  45. (VL_SHUFFLE_array array,
  46. vl_uindex indexA,
  47. vl_uindex indexB)
  48. {
  49. VL_SHUFFLE_type t = array [indexA] ;
  50. array [indexA] = array [indexB] ;
  51. array [indexB] = t ;
  52. }
  53. /* VL_SHUFFLE_swap */
  54. #endif
  55. /* ---------------------------------------------------------------- */
  56. #if ! defined(VL_SHUFFLE_shuffle) || defined(__DOXYGEN__)
  57. #define VL_SHUFFLE_shuffle VL_XCAT(VL_SHUFFLE_prefix, _shuffle)
  58. /** @brief Shuffle
  59. ** @param array (in/out) pointer to the array.
  60. ** @param size size of the array.
  61. ** @param rand random number generator to use.
  62. **
  63. ** The function randomly permutes the array.
  64. **/
  65. VL_INLINE void
  66. VL_SHUFFLE_shuffle
  67. (VL_SHUFFLE_array array, vl_size size, VlRand * rand)
  68. {
  69. vl_uindex n = size ;
  70. while (n > 1) {
  71. vl_uindex k = vl_rand_uindex (rand, n) ;
  72. n -- ;
  73. VL_SHUFFLE_swap (array, n, k) ;
  74. }
  75. }
  76. /* VL_SHUFFLE_shuffle */
  77. #endif
  78. #undef VL_SHUFFLE_prefix
  79. #undef VL_SHUFFLE_swap
  80. #undef VL_SHUFFLE_shuffle
  81. #undef VL_SHUFFLE_type
  82. #undef VL_SHUFFLE_array