SUMO - Simulation of Urban MObility
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
RandomDistributor.h
Go to the documentation of this file.
1
/****************************************************************************/
8
// Represents a generic random distribution
9
/****************************************************************************/
10
// SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
11
// Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
12
/****************************************************************************/
13
//
14
// This file is part of SUMO.
15
// SUMO is free software: you can redistribute it and/or modify
16
// it under the terms of the GNU General Public License as published by
17
// the Free Software Foundation, either version 3 of the License, or
18
// (at your option) any later version.
19
//
20
/****************************************************************************/
21
#ifndef RandomDistributor_h
22
#define RandomDistributor_h
23
24
25
// ===========================================================================
26
// included modules
27
// ===========================================================================
28
#ifdef _MSC_VER
29
#include <
windows_config.h
>
30
#else
31
#include <
config.h
>
32
#endif
33
34
#include <cassert>
35
#include <limits>
36
#include <
utils/common/RandHelper.h
>
37
#include <
utils/common/UtilExceptions.h
>
38
39
40
// ===========================================================================
41
// class definitions
42
// ===========================================================================
54
template
<
class
T>
55
class
RandomDistributor
{
56
public
:
57
typedef
void(*
Operation
)(
const
T);
58
static
void
doNothing
(
const
T) {}
59
64
RandomDistributor
(
unsigned
int
maximumSize =
std::numeric_limits<unsigned int>::max
(),
65
Operation
operation = &
doNothing
) :
66
myProb
(0),
67
myMaximumSize
(maximumSize),
68
myInsertionIndex
(0),
69
myOperation
(operation)
70
{}
71
73
~RandomDistributor
() { }
74
85
void
add
(
SUMOReal
prob, T val,
bool
checkDuplicates =
true
) {
86
assert(prob >= 0);
87
myProb
+= prob;
88
if
(checkDuplicates) {
89
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
90
if
(val ==
myVals
[i]) {
91
myProbs
[i] += prob;
92
return
;
93
}
94
}
95
}
96
if
(
myVals
.size() <
myMaximumSize
) {
97
myVals
.push_back(val);
98
myProbs
.push_back(prob);
99
}
else
{
100
myOperation
(
myVals
[
myInsertionIndex
]);
101
myVals
[
myInsertionIndex
] = val;
102
myProbs
[
myInsertionIndex
] = prob;
103
myInsertionIndex = (myInsertionIndex + 1) %
myMaximumSize
;
104
}
105
}
106
114
T
get
(
MTRand
* which = 0)
const
{
115
if
(
myProb
== 0) {
116
throw
OutOfBoundsException
();
117
}
118
SUMOReal
prob = which == 0 ?
RandHelper::rand
(
myProb
) : which->rand(
myProb
);
119
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
120
if
(prob <
myProbs
[i]) {
121
return
myVals
[i];
122
}
123
prob -=
myProbs
[i];
124
}
125
return
myVals
.back();
126
}
127
134
SUMOReal
getOverallProb
()
const
{
135
return
myProb
;
136
}
137
139
void
clear
() {
140
myProb
= 0;
141
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
142
myOperation
(
myVals
[i]);
143
}
144
myVals
.clear();
145
myProbs
.clear();
146
}
147
155
const
std::vector<T>&
getVals
()
const
{
156
return
myVals
;
157
}
158
166
const
std::vector<SUMOReal>&
getProbs
()
const
{
167
return
myProbs
;
168
}
169
170
private
:
172
SUMOReal
myProb
;
174
unsigned
int
myMaximumSize
;
176
unsigned
int
myInsertionIndex
;
178
Operation
myOperation
;
180
std::vector<T>
myVals
;
182
std::vector<SUMOReal>
myProbs
;
183
184
};
185
186
187
#endif
188
189
/****************************************************************************/
build
buildd
sumo-0.17.1~dfsg
src
utils
common
RandomDistributor.h
Generated on Sun Jun 16 2013 17:30:19 for SUMO - Simulation of Urban MObility by
1.8.3.1