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
/****************************************************************************/
9
// Represents a generic random distribution
10
/****************************************************************************/
11
// SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12
// Copyright (C) 2005-2014 DLR (http://www.dlr.de/) and contributors
13
/****************************************************************************/
14
//
15
// This file is part of SUMO.
16
// SUMO is free software: you can redistribute it and/or modify
17
// it under the terms of the GNU General Public License as published by
18
// the Free Software Foundation, either version 3 of the License, or
19
// (at your option) any later version.
20
//
21
/****************************************************************************/
22
#ifndef RandomDistributor_h
23
#define RandomDistributor_h
24
25
26
// ===========================================================================
27
// included modules
28
// ===========================================================================
29
#ifdef _MSC_VER
30
#include <
windows_config.h
>
31
#else
32
#include <
config.h
>
33
#endif
34
35
#include <cassert>
36
#include <limits>
37
#include <
utils/common/RandHelper.h
>
38
#include <
utils/common/UtilExceptions.h
>
39
40
41
// ===========================================================================
42
// class definitions
43
// ===========================================================================
55
template
<
class
T>
56
class
RandomDistributor
{
57
public
:
62
RandomDistributor
() :
63
myProb
(0)
64
{}
65
67
~RandomDistributor
() { }
68
80
bool
add
(
SUMOReal
prob, T val,
bool
checkDuplicates =
true
) {
81
assert(prob >= 0);
82
myProb
+= prob;
83
if
(checkDuplicates) {
84
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
85
if
(val ==
myVals
[i]) {
86
myProbs
[i] += prob;
87
return
false
;
88
}
89
}
90
}
91
myVals
.push_back(val);
92
myProbs
.push_back(prob);
93
return
true
;
94
}
95
103
T
get
(
MTRand
* which = 0)
const
{
104
if
(
myProb
== 0) {
105
throw
OutOfBoundsException
();
106
}
107
SUMOReal
prob = which == 0 ?
RandHelper::rand
(
myProb
) : which->rand(
myProb
);
108
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
109
if
(prob <
myProbs
[i]) {
110
return
myVals
[i];
111
}
112
prob -=
myProbs
[i];
113
}
114
return
myVals
.back();
115
}
116
123
SUMOReal
getOverallProb
()
const
{
124
return
myProb
;
125
}
126
128
void
clear
() {
129
myProb
= 0;
130
myVals
.clear();
131
myProbs
.clear();
132
}
133
141
const
std::vector<T>&
getVals
()
const
{
142
return
myVals
;
143
}
144
152
const
std::vector<SUMOReal>&
getProbs
()
const
{
153
return
myProbs
;
154
}
155
156
private
:
158
SUMOReal
myProb
;
160
std::vector<T>
myVals
;
162
std::vector<SUMOReal>
myProbs
;
163
164
};
165
166
167
#endif
168
169
/****************************************************************************/
tmp
buildd
sumo-0.21.0+dfsg
src
utils
common
RandomDistributor.h
Generated on Thu Nov 20 2014 19:49:58 for SUMO - Simulation of Urban MObility by
1.8.1.2