-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRngProvider.hpp
175 lines (149 loc) · 5.22 KB
/
RngProvider.hpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// The L-Band Digital Aeronautical Communications System (LDACS) Link Layer Glue Library provides interfaces and common classes necessary for the LDACS Air-Air Medium Access Control simulator.
// Copyright (C) 2023 Sebastian Lindner, Konrad Fuger, Musab Ahmed Eltayeb Ahmed, Andreas Timm-Giel, Institute of Communication Networks, Hamburg University of Technology, Hamburg, Germany
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 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 Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#ifndef INTAIRNET_LINKLAYER_GLUE_RNGPROVIDER_HPP
#define INTAIRNET_LINKLAYER_GLUE_RNGPROVIDER_HPP
#include <map>
#include <stdexcept>
#include <random>
#include <functional>
namespace TUHH_INTAIRNET_MCSOTDMA {
class IntRng {
public:
/**
* @param min Inclusive
* @param max Exclusive
* @return Random integer from [min, max).
*/
virtual int get(int min, int max) = 0;
virtual ~IntRng() = default;
};
/**
* C++-default random number generator for integer values.
*/
class IntegerUniformRng : public IntRng {
public:
IntegerUniformRng();
int get(int min, int max) override;
protected:
std::uniform_int_distribution<> dist;
std::random_device random_device;
std::mt19937 generator;
};
/**
* OMNeT++-provided random number generator for integer values.
*/
class OmnetIntegerUniformRng : public IntRng {
friend class RngProviderTests;
public:
/**
* @param k Index of the OMNeT++-RNG that should be associated with this instance.
*/
explicit OmnetIntegerUniformRng(int k) : k(k) {}
int get(int min, int max) override;
protected:
/** Association to the OMNeT++-provided RNG at index k. */
int k;
};
/** Interface that classes that wish to obtain a random number generator must implement. */
class IRng {
public:
/**
* Signs up to receive an integer RNG.
*/
IRng();
/**
* @param min Inclusive.
* @param max Exclusive.
* @return A random integer from [min, max).
*/
int getRandomInt(int min, int max);
};
/**
* Provides Random Number Generators (RNGs) to classes.
* By default, it provides C++-standard generators, but it can be configured to use simulator-provided generators instead.
* Implemented through a singleton instance inspired by https://stackoverflow.com/questions/1008019/c-singleton-design-pattern
*/
class RngProvider {
friend class RngProviderTests;
public:
/** Function that should be replaced by the OMNeT++ simulator. */
std::function<int (int min, int max, int k)> omnetGetInt = [] (int min, int max, int k) {throw std::runtime_error("not implemented"); return 0;};
// Make sure there can't be >1 instances of the RngProvider.
RngProvider(RngProvider const&) = delete;
void operator=(RngProvider const&) = delete;
/** Clears the <user, RNG> mapping. */
~RngProvider() {
reset();
}
static RngProvider& getInstance() {
static RngProvider instance; // Instantiated on first use.
return instance;
}
/**
* @param value Whether to use C++-default random number generators.
*/
void setUseDefaultRngs(bool value) {
this->use_default_rngs = value;
}
/**
* Called once by each class that wishes to obtain an integer random number generator.
* @param caller
*/
void signupInt(IRng* caller) {
if (int_rng_callers.find(caller) == int_rng_callers.end()) {
if (use_default_rngs)
int_rng_callers[caller] = new IntegerUniformRng();
else
int_rng_callers[caller] = new OmnetIntegerUniformRng((int) int_rng_callers.size());
}
}
/**
* Retrieves the RNG saved for the caller, and utilizes it to obtain a random integer.
* @param caller
* @param min Inclusive
* @param max Exclusive
* @return A random integer from [min, max).
*/
int getInt(IRng* caller, int min, int max) {
auto it = int_rng_callers.find(caller);
if (it == int_rng_callers.end())
throw std::invalid_argument("RngProvider::getRandomInt for unregistered caller.");
return it->second->get(min, max);
}
/**
* Clears the <user, RNG> mappings.
*/
void reset() {
// Delete just the RNGs, not the callers.
for (auto pair : int_rng_callers)
delete pair.second;
int_rng_callers.clear();
}
/**
* Sets the function that is called whenever an OMNeT++-based RNG should be used.
* @param func
*/
void setOmnetGetInt(std::function<int (int min, int max, int k)> func) {
omnetGetInt = func;
}
protected:
/** Maps RNG-user to RNG. */
std::map<IRng*, IntRng*> int_rng_callers;
/** Whether to use C++-default RNGs when true, or OMNeT++-provided RNGs when false. */
bool use_default_rngs = true;
private:
/** Private-hidden constructor as per singleton implementation pattern. */
RngProvider() {}
};
}
#endif //INTAIRNET_LINKLAYER_GLUE_RNGPROVIDER_HPP