SUMO - Simulation of Urban MObility
 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>
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
54 template<class T>
56 public:
57  typedef void(*Operation)(const T);
58  static void doNothing(const T) {}
59 
65  Operation operation = &doNothing) :
66  myProb(0),
67  myMaximumSize(maximumSize),
69  myOperation(operation)
70  {}
71 
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 {
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 
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:
174  unsigned int myMaximumSize;
176  unsigned int myInsertionIndex;
180  std::vector<T> myVals;
182  std::vector<SUMOReal> myProbs;
183 
184 };
185 
186 
187 #endif
188 
189 /****************************************************************************/
RandomDistributor(unsigned int maximumSize=std::numeric_limits< unsigned int >::max(), Operation operation=&doNothing)
Constructor for an empty distribution.
unsigned int myInsertionIndex
the index at which the next element shall be inserted if maximumSize is exceeded
const std::vector< SUMOReal > & getProbs() const
Returns the probabilities assigned to the members of the distribution.
Represents a generic random distribution.
void(* Operation)(const T)
std::vector< T > myVals
the members (acts as a ring buffer if myMaximumSize is reached)
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
const std::vector< T > & getVals() const
Returns the members of the distribution.
#define max(a, b)
Definition: polyfonts.c:61
SUMOReal myProb
the total probability
~RandomDistributor()
Destructor.
Operation myOperation
the operation to perform with replaced elements
std::vector< SUMOReal > myProbs
the corresponding probabilities (acts as a ring buffer if myMaximumSize is reached) ...
unsigned int myMaximumSize
the maximumSize of the distribution that shall be maintained
void clear()
Clears the distribution.
SUMOReal getOverallProb() const
Return the sum of the probabilites assigned to the members.
static void doNothing(const T)
#define SUMOReal
Definition: config.h:221
void add(SUMOReal prob, T val, bool checkDuplicates=true)
Adds a value with an assigned probability to the distribution.