SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00008 // Represents a generic random distribution 00009 /****************************************************************************/ 00010 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00011 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors 00012 /****************************************************************************/ 00013 // 00014 // This file is part of SUMO. 00015 // SUMO is free software: you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation, either version 3 of the License, or 00018 // (at your option) any later version. 00019 // 00020 /****************************************************************************/ 00021 #ifndef RandomDistributor_h 00022 #define RandomDistributor_h 00023 00024 00025 // =========================================================================== 00026 // included modules 00027 // =========================================================================== 00028 #ifdef _MSC_VER 00029 #include <windows_config.h> 00030 #else 00031 #include <config.h> 00032 #endif 00033 00034 #include <cassert> 00035 #include <utils/common/RandHelper.h> 00036 #include <utils/common/UtilExceptions.h> 00037 00038 00039 // =========================================================================== 00040 // class definitions 00041 // =========================================================================== 00053 template<class T> 00054 class RandomDistributor { 00055 public: 00057 RandomDistributor() : myProb(0) { } 00058 00060 ~RandomDistributor() { } 00061 00072 void add(SUMOReal prob, T val, bool checkDuplicates = true) { 00073 assert(prob >= 0); 00074 myProb += prob; 00075 if (checkDuplicates) { 00076 for (size_t i = 0; i < myVals.size(); i++) { 00077 if (val == myVals[i]) { 00078 myProbs[i] += prob; 00079 return; 00080 } 00081 } 00082 } 00083 myVals.push_back(val); 00084 myProbs.push_back(prob); 00085 } 00086 00093 T get() const { 00094 if (myProb == 0) { 00095 throw OutOfBoundsException(); 00096 } 00097 SUMOReal prob = RandHelper::rand(myProb); 00098 for (size_t i = 0; i < myVals.size(); i++) { 00099 if (prob < myProbs[i]) { 00100 return myVals[i]; 00101 } 00102 prob -= myProbs[i]; 00103 } 00104 return myVals.back(); 00105 } 00106 00113 SUMOReal getOverallProb() const { 00114 return myProb; 00115 } 00116 00118 void clear() { 00119 myProb = 0; 00120 myVals.clear(); 00121 myProbs.clear(); 00122 } 00123 00131 const std::vector<T> &getVals() const { 00132 return myVals; 00133 } 00134 00142 const std::vector<SUMOReal> &getProbs() const { 00143 return myProbs; 00144 } 00145 00146 private: 00148 SUMOReal myProb; 00150 std::vector<T> myVals; 00152 std::vector<SUMOReal> myProbs; 00153 00154 }; 00155 00156 00157 #endif 00158 00159 /****************************************************************************/