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.sourceforge.net/
11 // Copyright (C) 2001-2012 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>
37 
38 
39 // ===========================================================================
40 // class definitions
41 // ===========================================================================
53 template<class T>
55 public:
58 
61 
72  void add(SUMOReal prob, T val, bool checkDuplicates = true) {
73  assert(prob >= 0);
74  myProb += prob;
75  if (checkDuplicates) {
76  for (size_t i = 0; i < myVals.size(); i++) {
77  if (val == myVals[i]) {
78  myProbs[i] += prob;
79  return;
80  }
81  }
82  }
83  myVals.push_back(val);
84  myProbs.push_back(prob);
85  }
86 
93  T get() const {
94  if (myProb == 0) {
95  throw OutOfBoundsException();
96  }
98  for (size_t i = 0; i < myVals.size(); i++) {
99  if (prob < myProbs[i]) {
100  return myVals[i];
101  }
102  prob -= myProbs[i];
103  }
104  return myVals.back();
105  }
106 
114  return myProb;
115  }
116 
118  void clear() {
119  myProb = 0;
120  myVals.clear();
121  myProbs.clear();
122  }
123 
131  const std::vector<T> &getVals() const {
132  return myVals;
133  }
134 
142  const std::vector<SUMOReal> &getProbs() const {
143  return myProbs;
144  }
145 
146 private:
150  std::vector<T> myVals;
152  std::vector<SUMOReal> myProbs;
153 
154 };
155 
156 
157 #endif
158 
159 /****************************************************************************/