-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
random
95 lines (81 loc) · 3.43 KB
/
random
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Copyright (C) 2018-2024 Geoffrey Daniels. https://gpdaniels.com/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef GTL_VISION_CONSENSUS_SAMPLE_RANDOM_HPP
#define GTL_VISION_CONSENSUS_SAMPLE_RANDOM_HPP
// Summary: Implementation of a random dataset sampler. [wip]
#ifndef NDEBUG
# if defined(_MSC_VER)
# define __builtin_trap() __debugbreak()
# endif
/// @brief A simple assert macro to break the program if the random is misused.
# define GTL_RANDOM_ASSERT(ASSERTION, MESSAGE) static_cast<void>((ASSERTION) || (__builtin_trap(), 0))
#else
/// @brief At release time the assert macro is implemented as a nop.
# define GTL_RANDOM_ASSERT(ASSERTION, MESSAGE) static_cast<void>(0)
#endif
#include <vision/consensus/sampler>
namespace {
using size_t = decltype(sizeof(0));
}
namespace gtl {
template <size_t sample_size>
class random final
: public sampler<sample_size> {
private:
class random_pcg final {
private:
unsigned long long int state = 0x853C49E6748FEA9Bull;
unsigned long long int increment = 0xDA3E39CB94B95BDBull;
public:
unsigned int get_random() {
unsigned long long int state_previous = this->state;
this->state = state_previous * 0x5851F42D4C957F2Dull + this->increment;
unsigned int state_shift_xor_shift = static_cast<unsigned int>(((state_previous >> 18u) ^ state_previous) >> 27u);
int rotation = state_previous >> 59u;
return (state_shift_xor_shift >> rotation) | (state_shift_xor_shift << ((-rotation) & 31));
}
};
private:
random_pcg rng;
size_t size = 0;
public:
virtual void prepare(
const size_t data_size
) override final {
GTL_RANDOM_ASSERT(data_size > 0, "Data size must be greater than zero.");
GTL_RANDOM_ASSERT(data_size >= sample_size, "Data size must be greater or equal to the sample size.");
this->size = data_size;
}
public:
virtual void sample(
size_t* const __restrict indices
) override final {
GTL_RANDOM_ASSERT(this->size > 0, "Data size must be greater than zero.");
GTL_RANDOM_ASSERT(this->size >= sample_size, "Data size must be greater or equal to the sample size.");
for (size_t i = 0; i < sample_size; ++i) {
// Get a random index.
indices[i] = static_cast<size_t>(this->rng.get_random()) % this->size;
// Ensure it is unique.
for (size_t j = 0; j < i; ++j) {
if (indices[j] == indices[i]) {
indices[i] = (indices[i] + 1) % this->size;
j = static_cast<size_t>(-1);
}
}
}
}
};
}
#undef GTL_RANDOM_ASSERT
#endif // GTL_VISION_CONSENSUS_SAMPLE_RANDOM_HPP