/work/install-coverage/include/opencv4/opencv2/flann/random.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*********************************************************************** |
2 | | * Software License Agreement (BSD License) |
3 | | * |
4 | | * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved. |
5 | | * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved. |
6 | | * |
7 | | * THE BSD LICENSE |
8 | | * |
9 | | * Redistribution and use in source and binary forms, with or without |
10 | | * modification, are permitted provided that the following conditions |
11 | | * are met: |
12 | | * |
13 | | * 1. Redistributions of source code must retain the above copyright |
14 | | * notice, this list of conditions and the following disclaimer. |
15 | | * 2. Redistributions in binary form must reproduce the above copyright |
16 | | * notice, this list of conditions and the following disclaimer in the |
17 | | * documentation and/or other materials provided with the distribution. |
18 | | * |
19 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
20 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
21 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
22 | | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
23 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
24 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | | *************************************************************************/ |
30 | | |
31 | | #ifndef OPENCV_FLANN_RANDOM_H |
32 | | #define OPENCV_FLANN_RANDOM_H |
33 | | |
34 | | //! @cond IGNORED |
35 | | |
36 | | #include <algorithm> |
37 | | #include <cstdlib> |
38 | | #include <vector> |
39 | | |
40 | | namespace cvflann |
41 | | { |
42 | | |
43 | | inline int rand() |
44 | 0 | { |
45 | 0 | #ifndef OPENCV_FLANN_USE_STD_RAND |
46 | 0 | # if INT_MAX == RAND_MAX |
47 | 0 | int v = cv::theRNG().next() & INT_MAX; |
48 | 0 | # else |
49 | 0 | int v = cv::theRNG().uniform(0, RAND_MAX + 1); |
50 | 0 | # endif |
51 | 0 | #else |
52 | 0 | int v = std::rand(); |
53 | 0 | #endif // OPENCV_FLANN_USE_STD_RAND |
54 | 0 | return v; |
55 | 0 | } |
56 | | |
57 | | /** |
58 | | * Seeds the random number generator |
59 | | * @param seed Random seed |
60 | | */ |
61 | | inline void seed_random(unsigned int seed) |
62 | 0 | { |
63 | 0 | #ifndef OPENCV_FLANN_USE_STD_RAND |
64 | 0 | cv::theRNG() = cv::RNG(seed); |
65 | 0 | #else |
66 | 0 | std::srand(seed); |
67 | 0 | #endif |
68 | 0 | } |
69 | | |
70 | | /* |
71 | | * Generates a random double value. |
72 | | */ |
73 | | /** |
74 | | * Generates a random double value. |
75 | | * @param high Upper limit |
76 | | * @param low Lower limit |
77 | | * @return Random double value |
78 | | */ |
79 | | inline double rand_double(double high = 1.0, double low = 0) |
80 | 0 | { |
81 | 0 | return low + ((high-low) * (rand() / (RAND_MAX + 1.0))); |
82 | 0 | } |
83 | | |
84 | | /** |
85 | | * Generates a random integer value. |
86 | | * @param high Upper limit |
87 | | * @param low Lower limit |
88 | | * @return Random integer value |
89 | | */ |
90 | | inline int rand_int(int high = RAND_MAX, int low = 0) |
91 | 0 | { |
92 | 0 | return low + (int) ( double(high-low) * (rand() / (RAND_MAX + 1.0))); |
93 | 0 | } |
94 | | |
95 | | /** |
96 | | * Random number generator that returns a distinct number from |
97 | | * the [0,n) interval each time. |
98 | | */ |
99 | | class UniqueRandom |
100 | | { |
101 | | std::vector<int> vals_; |
102 | | int size_; |
103 | | int counter_; |
104 | | |
105 | | public: |
106 | | /** |
107 | | * Constructor. |
108 | | * @param n Size of the interval from which to generate |
109 | | * @return |
110 | | */ |
111 | | UniqueRandom(int n) |
112 | 0 | { |
113 | 0 | init(n); |
114 | 0 | } |
115 | | |
116 | | /** |
117 | | * Initializes the number generator. |
118 | | * @param n the size of the interval from which to generate random numbers. |
119 | | */ |
120 | | void init(int n) |
121 | 0 | { |
122 | 0 | // create and initialize an array of size n |
123 | 0 | vals_.resize(n); |
124 | 0 | size_ = n; |
125 | 0 | for (int i = 0; i < size_; ++i) vals_[i] = i; |
126 | 0 |
|
127 | 0 | // shuffle the elements in the array |
128 | 0 | #ifndef OPENCV_FLANN_USE_STD_RAND |
129 | 0 | cv::randShuffle(vals_); |
130 | 0 | #else |
131 | 0 | std::random_shuffle(vals_.begin(), vals_.end()); |
132 | 0 | #endif |
133 | 0 |
|
134 | 0 | counter_ = 0; |
135 | 0 | } |
136 | | |
137 | | /** |
138 | | * Return a distinct random integer in greater or equal to 0 and less |
139 | | * than 'n' on each call. It should be called maximum 'n' times. |
140 | | * Returns: a random integer |
141 | | */ |
142 | | int next() |
143 | 0 | { |
144 | 0 | if (counter_ == size_) { |
145 | 0 | return -1; |
146 | 0 | } |
147 | 0 | else { |
148 | 0 | return vals_[counter_++]; |
149 | 0 | } |
150 | 0 | } |
151 | | }; |
152 | | |
153 | | } |
154 | | |
155 | | //! @endcond |
156 | | |
157 | | #endif //OPENCV_FLANN_RANDOM_H |