RandomDistributor.h

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

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6