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-sim.org/
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
:
61
RandomDistributor
() :
62
myProb
(0)
63
{}
64
66
~RandomDistributor
() { }
67
79
bool
add
(
SUMOReal
prob, T val,
bool
checkDuplicates =
true
) {
80
assert(prob >= 0);
81
myProb
+= prob;
82
if
(checkDuplicates) {
83
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
84
if
(val ==
myVals
[i]) {
85
myProbs
[i] += prob;
86
return
false
;
87
}
88
}
89
}
90
myVals
.push_back(val);
91
myProbs
.push_back(prob);
92
return
true
;
93
}
94
102
T
get
(
MTRand
* which = 0)
const
{
103
if
(
myProb
== 0) {
104
throw
OutOfBoundsException
();
105
}
106
SUMOReal
prob = which == 0 ?
RandHelper::rand
(
myProb
) : which->rand(
myProb
);
107
for
(
size_t
i = 0; i <
myVals
.size(); i++) {
108
if
(prob <
myProbs
[i]) {
109
return
myVals
[i];
110
}
111
prob -=
myProbs
[i];
112
}
113
return
myVals
.back();
114
}
115
122
SUMOReal
getOverallProb
()
const
{
123
return
myProb
;
124
}
125
127
void
clear
() {
128
myProb
= 0;
129
myVals
.clear();
130
myProbs
.clear();
131
}
132
140
const
std::vector<T>&
getVals
()
const
{
141
return
myVals
;
142
}
143
151
const
std::vector<SUMOReal>&
getProbs
()
const
{
152
return
myProbs
;
153
}
154
155
private
:
157
SUMOReal
myProb
;
159
std::vector<T>
myVals
;
161
std::vector<SUMOReal>
myProbs
;
162
163
};
164
165
166
#endif
167
168
/****************************************************************************/
home
delphi
sumo
src
utils
common
RandomDistributor.h
Generated by
1.8.2