|
SUMO - Simulation of Urban MObility
|
00001 /****************************************************************************/ 00007 // A simple vector of SUMOReals 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright (C) 2001-2011 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 VectorHelper_h 00020 #define VectorHelper_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 <vector> 00033 #include <algorithm> 00034 #include <iostream> 00035 00036 00037 // =========================================================================== 00038 // definitions 00039 // =========================================================================== 00040 typedef std::vector<bool> BoolVector; 00041 typedef std::vector<SUMOReal> DoubleVector; 00042 typedef std::vector<int> IntVector; 00043 00044 00045 // =========================================================================== 00046 // class definitions 00047 // =========================================================================== 00051 template<class T> 00052 class VectorHelper { 00053 public: 00054 static T sum(const std::vector<T> &v) { 00055 T sum = 0; 00056 for (typename std::vector<T>::const_iterator i=v.begin(); i!=v.end(); i++) { 00057 sum += *i; 00058 } 00059 return sum; 00060 } 00061 00062 static void normaliseSum(std::vector<T> &v, T msum=1.0) { 00063 if (msum==0) { 00064 // is an error; do nothing 00065 return; 00066 } 00067 T rsum = sum(v); 00068 if (rsum==0) { 00069 set(v, (T) 1.0*msum/(T) v.size()); 00070 return; 00071 } 00072 div(v, rsum/msum); 00073 } 00074 00075 static void div(std::vector<T> &v, T by) { 00076 for (typename std::vector<T>::iterator i=v.begin(); i!=v.end(); i++) { 00077 *i /= by; 00078 } 00079 } 00080 00081 static void removeDouble(std::vector<T> &v) { 00082 typename std::vector<T>::iterator i=v.begin(); 00083 while (i!=v.end()) { 00084 for (typename std::vector<T>::iterator j=i+1; j!=v.end();) { 00085 if (*i==*j) { 00086 j = v.erase(j); 00087 } else { 00088 j++; 00089 } 00090 } 00091 i++; 00092 } 00093 } 00094 00095 00096 static void set(std::vector<T> &v, T to) { 00097 for (typename std::vector<T>::iterator i=v.begin(); i!=v.end(); i++) { 00098 *i = to; 00099 } 00100 } 00101 00102 static T maxValue(const std::vector<T> &v) { 00103 SUMOReal m = *(v.begin()); 00104 for (typename std::vector<T>::const_iterator j=v.begin()+1; j!=v.end(); j++) { 00105 if ((*j)>m) { 00106 m = *j; 00107 } 00108 } 00109 return m; 00110 } 00111 00112 static T minValue(const std::vector<T> &v) { 00113 SUMOReal m = *(v.begin()); 00114 for (typename std::vector<T>::const_iterator j=v.begin()+1; j!=v.end(); j++) { 00115 if ((*j)<m) { 00116 m = *j; 00117 } 00118 } 00119 return m; 00120 } 00121 00122 static void remove_smaller_than(std::vector<T> &v, T swell) { 00123 for (typename std::vector<T>::iterator j=v.begin(); j!=v.end();) { 00124 if ((*j)<swell) { 00125 j = v.erase(j); 00126 } else { 00127 j++; 00128 } 00129 } 00130 } 00131 00132 static void remove_larger_than(std::vector<T> &v, T swell) { 00133 for (typename std::vector<T>::iterator j=v.begin(); j!=v.end();) { 00134 if ((*j)>swell) { 00135 j = v.erase(j); 00136 } else { 00137 j++; 00138 } 00139 } 00140 } 00141 00142 static void add2All(std::vector<T> &v, T what) { 00143 for (typename std::vector<T>::iterator j=v.begin(); j!=v.end(); j++) { 00144 (*j) += what; 00145 } 00146 } 00147 00149 static bool subSetExists(const std::vector<T> &v1, const std::vector<T> &v2) { 00150 for (typename std::vector<T>::const_iterator i=v1.begin(); i!=v1.end(); i++) { 00151 int val1 = (*i); 00152 if (find(v2.begin(), v2.end(), val1)!=v2.end()) { 00153 return true; 00154 } 00155 } 00156 return false; 00157 } 00158 00159 00160 00161 }; 00162 00163 template<class T> 00164 std::ostream& operator<<(std::ostream& os, const std::vector<T> &v) { 00165 for (typename std::vector<T>::const_iterator i=v.begin(); i!=v.end(); i++) { 00166 if (i!=v.begin()) { 00167 os << ", "; 00168 } 00169 os << (*i); 00170 } 00171 return os; 00172 } 00173 00174 00175 00176 #endif 00177 00178 /****************************************************************************/ 00179