Debian Codegraph @

src/thirdparty/VLFeat/shuffle-def.h

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